Skip to content

Commit

Permalink
Refactor: move url to app context to avoid passing requestStore (#72212)
Browse files Browse the repository at this point in the history
requestStore is set on the app context but this store doens't make sense
to scope for all renders, in particular prerenders so we need to extract
the necessary information that is actually globally available and carry
that around on the app context instead. This change updates app context
to carry the url object and it updates reads of the url to use app
context instead of requestStore. Now nothing is reading requestStore
from the context so I removed it.

This update is part of a larger refactor to eliminate the requestStore
that shadows prerendering. It should not alter any program semantics.

stacked on #72211
  • Loading branch information
gnoff authored Nov 5, 2024
1 parent 3170124 commit 1516572
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ export async function handleAction({
// We want the render to see any cookie writes that we performed during the action,
// so we need to update the immutable cookies to reflect the changes.
synchronizeMutableCookies(requestStore)
requestStore.phase = 'render'
return generateFlight(...args)
}

Expand Down Expand Up @@ -994,6 +995,7 @@ export async function handleAction({
// swallow error, it's gonna be handled on the client
}

requestStore.phase = 'render'
return {
type: 'done',
result: await generateFlight(req, ctx, {
Expand Down
20 changes: 11 additions & 9 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export type GenerateFlight = typeof generateDynamicFlightRenderResult

export type AppRenderContext = {
workStore: WorkStore
requestStore: RequestStore
url: ReturnType<typeof parseRelativeUrl>
componentMod: AppPageModule
renderOpts: RenderOpts
parsedRequestHeaders: ParsedRequestHeaders
Expand Down Expand Up @@ -399,7 +399,6 @@ async function generateDynamicRSCPayload(
skipFlight: boolean
}
): Promise<RSCPayload> {
ctx.requestStore.phase = 'render'
// Flight data that is going to be passed to the browser.
// Currently a single item array but in the future multiple patches might be combined in a single request.

Expand All @@ -420,11 +419,11 @@ async function generateDynamicRSCPayload(
},
getDynamicParamFromSegment,
appUsingSizeAdjustment,
requestStore: { url },
query,
requestId,
flightRouterState,
workStore,
url,
} = ctx

if (!options?.skipFlight) {
Expand Down Expand Up @@ -584,6 +583,7 @@ async function generateDynamicFlightRenderResult(
async function warmupDevRender(
req: BaseNextRequest,
ctx: AppRenderContext,
requestStore: RequestStore,
options?: {
actionResult: ActionResult
skipFlight: boolean
Expand Down Expand Up @@ -616,7 +616,6 @@ async function warmupDevRender(

// Attach this to the request store so that it can be used during the
// render.
const { requestStore } = ctx
requestStore.devWarmupPrerenderResumeDataCache =
devWarmupPrerenderResumeDataCache

Expand Down Expand Up @@ -699,9 +698,10 @@ async function getRSCPayload(
MetadataBoundary,
ViewportBoundary,
},
requestStore: { url },
url,
workStore,
} = ctx

const initialTree = createFlightRouterStateFromLoaderTree(
tree,
getDynamicParamFromSegment,
Expand Down Expand Up @@ -804,7 +804,7 @@ async function getErrorRSCPayload(
MetadataBoundary,
ViewportBoundary,
},
requestStore: { url },
url,
requestId,
workStore,
} = ctx
Expand Down Expand Up @@ -985,6 +985,7 @@ export type BinaryStreamOf<T> = ReadableStream<Uint8Array>
async function renderToHTMLOrFlightImpl(
req: BaseNextRequest,
res: BaseNextResponse,
url: ReturnType<typeof parseRelativeUrl>,
pagePath: string,
query: NextParsedUrlQuery,
renderOpts: RenderOpts,
Expand Down Expand Up @@ -1161,8 +1162,8 @@ async function renderToHTMLOrFlightImpl(

const ctx: AppRenderContext = {
componentMod: ComponentMod,
url,
renderOpts,
requestStore,
workStore,
parsedRequestHeaders,
getDynamicParamFromSegment,
Expand Down Expand Up @@ -1287,7 +1288,7 @@ async function renderToHTMLOrFlightImpl(
} else {
// We're rendering dynamically
if (isDevWarmupRequest) {
return warmupDevRender(req, ctx)
return warmupDevRender(req, ctx, requestStore)
} else if (isRSCRequest) {
return generateDynamicFlightRenderResult(req, ctx)
}
Expand Down Expand Up @@ -1483,6 +1484,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
return renderToHTMLOrFlightImpl(
req,
res,
url,
pagePath,
query,
renderOpts,
Expand Down Expand Up @@ -1845,7 +1847,7 @@ async function renderToStream(
// If there were mutable cookies set, we need to set them on the
// response.
const headers = new Headers()
if (appendMutableCookies(headers, ctx.requestStore.mutableCookies)) {
if (appendMutableCookies(headers, requestStore.mutableCookies)) {
setHeader('set-cookie', Array.from(headers.values()))
}

Expand Down

0 comments on commit 1516572

Please sign in to comment.