Skip to content

Commit

Permalink
refactor: remove internal queries, move to request metadata (#74100)
Browse files Browse the repository at this point in the history
In order to simplify internal state management, this migrates internal
use of query parameters over to storing details on the request metadata.
Future PR's might be able to pull items off the metadata entirely and
use prop drilling and the new `PageRenderContext`, `PageSharedContext`,
and app equivalents.
  • Loading branch information
wyattjoh authored Dec 20, 2024
1 parent 29aa6d1 commit 2e46a18
Show file tree
Hide file tree
Showing 29 changed files with 417 additions and 294 deletions.
16 changes: 7 additions & 9 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2758,12 +2758,12 @@ export default async function build(
if (i18n) {
defaultMap[`/${i18n.defaultLocale}${page}`] = {
page,
query: { __nextFallback: 'true' },
_pagesFallback: true,
}
} else {
defaultMap[page] = {
page,
query: { __nextFallback: 'true' },
_pagesFallback: true,
}
}
} else {
Expand All @@ -2780,7 +2780,7 @@ export default async function build(
routes.forEach((route) => {
defaultMap[route.pathname] = {
page,
query: { __nextSsgPath: route.encodedPathname },
_ssgPath: route.encodedPathname,
}
})
})
Expand Down Expand Up @@ -2820,7 +2820,7 @@ export default async function build(

defaultMap[route.pathname] = {
page: originalAppPath,
query: { __nextSsgPath: route.encodedPathname },
_ssgPath: route.encodedPathname,
_fallbackRouteParams: route.fallbackRouteParams,
_isDynamicError: isDynamicError,
_isAppDir: true,
Expand All @@ -2839,7 +2839,7 @@ export default async function build(
} of prospectiveRenders.values()) {
defaultMap[page] = {
page: originalAppPath,
query: { __nextSsgPath: page },
_ssgPath: page,
_fallbackRouteParams: getParamKeys(page),
// Prospective renders are only enabled for app pages.
_isAppDir: true,
Expand Down Expand Up @@ -2869,10 +2869,8 @@ export default async function build(

defaultMap[outputPath] = {
page: defaultMap[page]?.page || page,
query: {
__nextLocale: locale,
__nextFallback: isFallback ? 'true' : undefined,
},
_locale: locale,
_pagesFallback: isFallback,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/static-paths/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ export async function buildAppStaticPaths({
waitUntil: afterRunner.context.waitUntil,
onClose: afterRunner.context.onClose,
onAfterTaskError: afterRunner.context.onTaskError,
buildId,
},
buildId,
})

const routeParams = await ComponentMod.workAsyncStorage.run(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function getRender({

const server = new WebServer({
dev,
buildId,
conf: config,
minimalMode: true,
webServerConfig: {
Expand All @@ -93,7 +94,6 @@ export function getRender({
pagesType,
interceptionRouteRewrites,
extendRenderOpts: {
buildId,
runtime: SERVER_RUNTIME.experimentalEdge,
supportsDynamicResponse: true,
disableOptimizedLoading: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ async function exportAppImpl(
// Start the rendering process
const renderOpts: WorkerRenderOptsPartial = {
previewProps: prerenderManifest?.preview,
buildId,
nextExport: true,
assetPrefix: nextConfig.assetPrefix.replace(/\/$/, ''),
distDir,
Expand Down Expand Up @@ -543,6 +542,7 @@ async function exportAppImpl(
await Promise.all(
chunks.map((paths) =>
worker.exportPages({
buildId,
paths,
exportPathMap,
parentSpanId: span.getId(),
Expand Down
13 changes: 9 additions & 4 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { WorkStore } from '../../server/app-render/work-async-storage.exter
import type { FallbackRouteParams } from '../../server/request/fallback-params'
import { AfterRunner } from '../../server/after/run-with-after'
import type { RequestLifecycleOpts } from '../../server/base-server'
import type { AppSharedContext } from '../../server/app-render/app-render'

export const enum ExportedAppPageFiles {
HTML = 'HTML',
Expand All @@ -44,7 +45,8 @@ export async function prospectiveRenderAppPage(
pathname: string,
query: NextParsedUrlQuery,
fallbackRouteParams: FallbackRouteParams | null,
partialRenderOpts: Omit<RenderOpts, keyof RequestLifecycleOpts>
partialRenderOpts: Omit<RenderOpts, keyof RequestLifecycleOpts>,
sharedContext: AppSharedContext
): Promise<undefined> {
const afterRunner = new AfterRunner()

Expand All @@ -68,7 +70,8 @@ export async function prospectiveRenderAppPage(
onAfterTaskError: afterRunner.context.onTaskError,
},
undefined,
false
false,
sharedContext
)

// TODO(after): if we abort a prerender because of an error in an after-callback
Expand Down Expand Up @@ -102,7 +105,8 @@ export async function exportAppPage(
htmlFilepath: string,
debugOutput: boolean,
isDynamicError: boolean,
fileWriter: FileWriter
fileWriter: FileWriter,
sharedContext: AppSharedContext
): Promise<ExportRouteResult> {
const afterRunner = new AfterRunner()

Expand Down Expand Up @@ -130,7 +134,8 @@ export async function exportAppPage(
fallbackRouteParams,
renderOpts,
undefined,
false
false,
sharedContext
)

const html = result.toUnchunkedString()
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/export/routes/app-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export async function exportAppRoute(
onClose: afterRunner.context.onClose,
onAfterTaskError: afterRunner.context.onTaskError,
cacheLifeProfiles,
},
sharedContext: {
buildId,
},
}
Expand Down
16 changes: 13 additions & 3 deletions packages/next/src/export/routes/pages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { ExportRouteResult, FileWriter } from '../types'
import type { RenderOpts } from '../../server/render'
import type {
PagesRenderContext,
PagesSharedContext,
RenderOpts,
} from '../../server/render'
import type { LoadComponentsReturnType } from '../../server/load-components'
import type { AmpValidation } from '../types'
import type { NextParsedUrlQuery } from '../../server/request-meta'
Expand Down Expand Up @@ -47,6 +51,8 @@ export async function exportPagesPage(
pagesDataDir: string,
buildExport: boolean,
isDynamic: boolean,
sharedContext: PagesSharedContext,
renderContext: PagesRenderContext,
hasOrigQueryValues: boolean,
renderOpts: RenderOpts,
components: LoadComponentsReturnType,
Expand Down Expand Up @@ -117,7 +123,9 @@ export async function exportPagesPage(
res,
page,
searchAndDynamicParams,
renderOpts
renderOpts,
sharedContext,
renderContext
)
} catch (err) {
if (!isBailoutToCSRError(err)) throw err
Expand Down Expand Up @@ -173,7 +181,9 @@ export async function exportPagesPage(
res,
page,
{ ...searchAndDynamicParams, amp: '1' },
renderOpts
renderOpts,
sharedContext,
renderContext
)
} catch (err) {
if (!isBailoutToCSRError(err)) throw err
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type FileWriter = (
type PathMap = ExportPathMap[keyof ExportPathMap]

export interface ExportPagesInput {
buildId: string
paths: string[]
exportPathMap: ExportPathMap
parentSpanId: number
Expand All @@ -55,6 +56,7 @@ export interface ExportPagesInput {
}

export interface ExportPageInput {
buildId: string
path: string
pathMap: PathMap
distDir: string
Expand Down
36 changes: 28 additions & 8 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import {
import { needsExperimentalReact } from '../lib/needs-experimental-react'
import type { AppRouteRouteModule } from '../server/route-modules/app-route/module.compiled'
import { isStaticGenBailoutError } from '../client/components/static-generation-bailout'
import type { PagesRenderContext, PagesSharedContext } from '../server/render'
import type { AppSharedContext } from '../server/app-render/app-render'

const envConfig = require('../shared/lib/runtime-config.external')

Expand Down Expand Up @@ -124,11 +126,8 @@ async function exportPageImpl(
const ampPath = `${filePath}.amp`
let renderAmpPath = ampPath

let updatedPath = query.__nextSsgPath || path
delete query.__nextSsgPath

let locale = query.__nextLocale || input.renderOpts.locale
delete query.__nextLocale
let updatedPath = pathMap._ssgPath || path
let locale = pathMap._locale || input.renderOpts.locale

if (input.renderOpts.locale) {
const localePathResult = normalizeLocalePath(path, input.renderOpts.locales)
Expand Down Expand Up @@ -251,7 +250,7 @@ async function exportPageImpl(
htmlFilepath,
fileWriter,
input.renderOpts.experimental,
input.renderOpts.buildId
input.buildId
)
}

Expand All @@ -276,6 +275,10 @@ async function exportPageImpl(

// Handle App Pages
if (isAppDir) {
const sharedContext: AppSharedContext = {
buildId: input.buildId,
}

// If this is a prospective render, don't return any metrics or revalidate
// timings as we aren't persisting this render (it was only to error).
if (isProspectiveRender) {
Expand All @@ -286,7 +289,8 @@ async function exportPageImpl(
pathname,
query,
fallbackRouteParams,
renderOpts
renderOpts,
sharedContext
)
}

Expand All @@ -302,10 +306,23 @@ async function exportPageImpl(
htmlFilepath,
debugOutput,
isDynamicError,
fileWriter
fileWriter,
sharedContext
)
}

const sharedContext: PagesSharedContext = {
buildId: input.buildId,
deploymentId: input.renderOpts.deploymentId,
customServer: undefined,
}

const renderContext: PagesRenderContext = {
isFallback: pathMap._pagesFallback ?? false,
isDraftMode: false,
developmentNotFoundSourcePage: undefined,
}

return exportPagesPage(
req,
res,
Expand All @@ -322,6 +339,8 @@ async function exportPageImpl(
pagesDataDir,
buildExport,
isDynamic,
sharedContext,
renderContext,
hasOrigQueryValues,
renderOpts,
components,
Expand Down Expand Up @@ -399,6 +418,7 @@ export async function exportPages(
debugOutput: options.debugOutput,
enableExperimentalReact: needsExperimentalReact(nextConfig),
sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm),
buildId: input.buildId,
}),
// If exporting the page takes longer than the timeout, reject the promise.
new Promise((_, reject) => {
Expand Down
Loading

0 comments on commit 2e46a18

Please sign in to comment.