Skip to content

Commit 2e46a18

Browse files
authored
refactor: remove internal queries, move to request metadata (vercel#74100)
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.
1 parent 29aa6d1 commit 2e46a18

File tree

29 files changed

+417
-294
lines changed

29 files changed

+417
-294
lines changed

packages/next/src/build/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,12 +2758,12 @@ export default async function build(
27582758
if (i18n) {
27592759
defaultMap[`/${i18n.defaultLocale}${page}`] = {
27602760
page,
2761-
query: { __nextFallback: 'true' },
2761+
_pagesFallback: true,
27622762
}
27632763
} else {
27642764
defaultMap[page] = {
27652765
page,
2766-
query: { __nextFallback: 'true' },
2766+
_pagesFallback: true,
27672767
}
27682768
}
27692769
} else {
@@ -2780,7 +2780,7 @@ export default async function build(
27802780
routes.forEach((route) => {
27812781
defaultMap[route.pathname] = {
27822782
page,
2783-
query: { __nextSsgPath: route.encodedPathname },
2783+
_ssgPath: route.encodedPathname,
27842784
}
27852785
})
27862786
})
@@ -2820,7 +2820,7 @@ export default async function build(
28202820

28212821
defaultMap[route.pathname] = {
28222822
page: originalAppPath,
2823-
query: { __nextSsgPath: route.encodedPathname },
2823+
_ssgPath: route.encodedPathname,
28242824
_fallbackRouteParams: route.fallbackRouteParams,
28252825
_isDynamicError: isDynamicError,
28262826
_isAppDir: true,
@@ -2839,7 +2839,7 @@ export default async function build(
28392839
} of prospectiveRenders.values()) {
28402840
defaultMap[page] = {
28412841
page: originalAppPath,
2842-
query: { __nextSsgPath: page },
2842+
_ssgPath: page,
28432843
_fallbackRouteParams: getParamKeys(page),
28442844
// Prospective renders are only enabled for app pages.
28452845
_isAppDir: true,
@@ -2869,10 +2869,8 @@ export default async function build(
28692869

28702870
defaultMap[outputPath] = {
28712871
page: defaultMap[page]?.page || page,
2872-
query: {
2873-
__nextLocale: locale,
2874-
__nextFallback: isFallback ? 'true' : undefined,
2875-
},
2872+
_locale: locale,
2873+
_pagesFallback: isFallback,
28762874
}
28772875
}
28782876

packages/next/src/build/static-paths/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ export async function buildAppStaticPaths({
353353
waitUntil: afterRunner.context.waitUntil,
354354
onClose: afterRunner.context.onClose,
355355
onAfterTaskError: afterRunner.context.onTaskError,
356-
buildId,
357356
},
357+
buildId,
358358
})
359359

360360
const routeParams = await ComponentMod.workAsyncStorage.run(

packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export function getRender({
8585

8686
const server = new WebServer({
8787
dev,
88+
buildId,
8889
conf: config,
8990
minimalMode: true,
9091
webServerConfig: {
@@ -93,7 +94,6 @@ export function getRender({
9394
pagesType,
9495
interceptionRouteRewrites,
9596
extendRenderOpts: {
96-
buildId,
9797
runtime: SERVER_RUNTIME.experimentalEdge,
9898
supportsDynamicResponse: true,
9999
disableOptimizedLoading: true,

packages/next/src/export/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ async function exportAppImpl(
317317
// Start the rendering process
318318
const renderOpts: WorkerRenderOptsPartial = {
319319
previewProps: prerenderManifest?.preview,
320-
buildId,
321320
nextExport: true,
322321
assetPrefix: nextConfig.assetPrefix.replace(/\/$/, ''),
323322
distDir,
@@ -543,6 +542,7 @@ async function exportAppImpl(
543542
await Promise.all(
544543
chunks.map((paths) =>
545544
worker.exportPages({
545+
buildId,
546546
paths,
547547
exportPathMap,
548548
parentSpanId: span.getId(),

packages/next/src/export/routes/app-page.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type { WorkStore } from '../../server/app-render/work-async-storage.exter
2727
import type { FallbackRouteParams } from '../../server/request/fallback-params'
2828
import { AfterRunner } from '../../server/after/run-with-after'
2929
import type { RequestLifecycleOpts } from '../../server/base-server'
30+
import type { AppSharedContext } from '../../server/app-render/app-render'
3031

3132
export const enum ExportedAppPageFiles {
3233
HTML = 'HTML',
@@ -44,7 +45,8 @@ export async function prospectiveRenderAppPage(
4445
pathname: string,
4546
query: NextParsedUrlQuery,
4647
fallbackRouteParams: FallbackRouteParams | null,
47-
partialRenderOpts: Omit<RenderOpts, keyof RequestLifecycleOpts>
48+
partialRenderOpts: Omit<RenderOpts, keyof RequestLifecycleOpts>,
49+
sharedContext: AppSharedContext
4850
): Promise<undefined> {
4951
const afterRunner = new AfterRunner()
5052

@@ -68,7 +70,8 @@ export async function prospectiveRenderAppPage(
6870
onAfterTaskError: afterRunner.context.onTaskError,
6971
},
7072
undefined,
71-
false
73+
false,
74+
sharedContext
7275
)
7376

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

@@ -130,7 +134,8 @@ export async function exportAppPage(
130134
fallbackRouteParams,
131135
renderOpts,
132136
undefined,
133-
false
137+
false,
138+
sharedContext
134139
)
135140

136141
const html = result.toUnchunkedString()

packages/next/src/export/routes/app-route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export async function exportAppRoute(
8787
onClose: afterRunner.context.onClose,
8888
onAfterTaskError: afterRunner.context.onTaskError,
8989
cacheLifeProfiles,
90+
},
91+
sharedContext: {
9092
buildId,
9193
},
9294
}

packages/next/src/export/routes/pages.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { ExportRouteResult, FileWriter } from '../types'
2-
import type { RenderOpts } from '../../server/render'
2+
import type {
3+
PagesRenderContext,
4+
PagesSharedContext,
5+
RenderOpts,
6+
} from '../../server/render'
37
import type { LoadComponentsReturnType } from '../../server/load-components'
48
import type { AmpValidation } from '../types'
59
import type { NextParsedUrlQuery } from '../../server/request-meta'
@@ -47,6 +51,8 @@ export async function exportPagesPage(
4751
pagesDataDir: string,
4852
buildExport: boolean,
4953
isDynamic: boolean,
54+
sharedContext: PagesSharedContext,
55+
renderContext: PagesRenderContext,
5056
hasOrigQueryValues: boolean,
5157
renderOpts: RenderOpts,
5258
components: LoadComponentsReturnType,
@@ -117,7 +123,9 @@ export async function exportPagesPage(
117123
res,
118124
page,
119125
searchAndDynamicParams,
120-
renderOpts
126+
renderOpts,
127+
sharedContext,
128+
renderContext
121129
)
122130
} catch (err) {
123131
if (!isBailoutToCSRError(err)) throw err
@@ -173,7 +181,9 @@ export async function exportPagesPage(
173181
res,
174182
page,
175183
{ ...searchAndDynamicParams, amp: '1' },
176-
renderOpts
184+
renderOpts,
185+
sharedContext,
186+
renderContext
177187
)
178188
} catch (err) {
179189
if (!isBailoutToCSRError(err)) throw err

packages/next/src/export/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type FileWriter = (
3838
type PathMap = ExportPathMap[keyof ExportPathMap]
3939

4040
export interface ExportPagesInput {
41+
buildId: string
4142
paths: string[]
4243
exportPathMap: ExportPathMap
4344
parentSpanId: number
@@ -55,6 +56,7 @@ export interface ExportPagesInput {
5556
}
5657

5758
export interface ExportPageInput {
59+
buildId: string
5860
path: string
5961
pathMap: PathMap
6062
distDir: string

packages/next/src/export/worker.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import {
4848
import { needsExperimentalReact } from '../lib/needs-experimental-react'
4949
import type { AppRouteRouteModule } from '../server/route-modules/app-route/module.compiled'
5050
import { isStaticGenBailoutError } from '../client/components/static-generation-bailout'
51+
import type { PagesRenderContext, PagesSharedContext } from '../server/render'
52+
import type { AppSharedContext } from '../server/app-render/app-render'
5153

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

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

127-
let updatedPath = query.__nextSsgPath || path
128-
delete query.__nextSsgPath
129-
130-
let locale = query.__nextLocale || input.renderOpts.locale
131-
delete query.__nextLocale
129+
let updatedPath = pathMap._ssgPath || path
130+
let locale = pathMap._locale || input.renderOpts.locale
132131

133132
if (input.renderOpts.locale) {
134133
const localePathResult = normalizeLocalePath(path, input.renderOpts.locales)
@@ -251,7 +250,7 @@ async function exportPageImpl(
251250
htmlFilepath,
252251
fileWriter,
253252
input.renderOpts.experimental,
254-
input.renderOpts.buildId
253+
input.buildId
255254
)
256255
}
257256

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

277276
// Handle App Pages
278277
if (isAppDir) {
278+
const sharedContext: AppSharedContext = {
279+
buildId: input.buildId,
280+
}
281+
279282
// If this is a prospective render, don't return any metrics or revalidate
280283
// timings as we aren't persisting this render (it was only to error).
281284
if (isProspectiveRender) {
@@ -286,7 +289,8 @@ async function exportPageImpl(
286289
pathname,
287290
query,
288291
fallbackRouteParams,
289-
renderOpts
292+
renderOpts,
293+
sharedContext
290294
)
291295
}
292296

@@ -302,10 +306,23 @@ async function exportPageImpl(
302306
htmlFilepath,
303307
debugOutput,
304308
isDynamicError,
305-
fileWriter
309+
fileWriter,
310+
sharedContext
306311
)
307312
}
308313

314+
const sharedContext: PagesSharedContext = {
315+
buildId: input.buildId,
316+
deploymentId: input.renderOpts.deploymentId,
317+
customServer: undefined,
318+
}
319+
320+
const renderContext: PagesRenderContext = {
321+
isFallback: pathMap._pagesFallback ?? false,
322+
isDraftMode: false,
323+
developmentNotFoundSourcePage: undefined,
324+
}
325+
309326
return exportPagesPage(
310327
req,
311328
res,
@@ -322,6 +339,8 @@ async function exportPageImpl(
322339
pagesDataDir,
323340
buildExport,
324341
isDynamic,
342+
sharedContext,
343+
renderContext,
325344
hasOrigQueryValues,
326345
renderOpts,
327346
components,
@@ -399,6 +418,7 @@ export async function exportPages(
399418
debugOutput: options.debugOutput,
400419
enableExperimentalReact: needsExperimentalReact(nextConfig),
401420
sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm),
421+
buildId: input.buildId,
402422
}),
403423
// If exporting the page takes longer than the timeout, reject the promise.
404424
new Promise((_, reject) => {

0 commit comments

Comments
 (0)