Unverified Commit 00f25a86 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Partial Prefetching] Small optimizations and fixes (#99695)

Fixes a few oversights in the Partial Prefetching implementation that I
discovered while working on a larger refactor. I've moved these into a
separate PR so they can be reviewed independently of the upcoming
restructuring.

These can be reviewed commit-by-commit.
parent 8f761662
...@@ -592,7 +592,6 @@ async function generateDynamicRSCPayload( ...@@ -592,7 +592,6 @@ async function generateDynamicRSCPayload(
skipPageRendering?: boolean skipPageRendering?: boolean
staleTimeIterable?: AsyncIterable<number> staleTimeIterable?: AsyncIterable<number>
shellByteLengthPromise?: Promise<number | null> shellByteLengthPromise?: Promise<number | null>
shellUsedSessionDataPromise?: Promise<boolean>
runtimePrefetchStream?: ReadableStream<Uint8Array> runtimePrefetchStream?: ReadableStream<Uint8Array>
} }
): Promise<RSCPayload> { ): Promise<RSCPayload> {
...@@ -748,9 +747,6 @@ async function generateDynamicRSCPayload( ...@@ -748,9 +747,6 @@ async function generateDynamicRSCPayload(
if (options?.shellByteLengthPromise !== undefined) { if (options?.shellByteLengthPromise !== undefined) {
baseResponse.a = options.shellByteLengthPromise baseResponse.a = options.shellByteLengthPromise
} }
if (options?.shellUsedSessionDataPromise !== undefined) {
baseResponse.w = options.shellUsedSessionDataPromise
}
if (options?.runtimePrefetchStream !== undefined) { if (options?.runtimePrefetchStream !== undefined) {
baseResponse.p = options.runtimePrefetchStream baseResponse.p = options.runtimePrefetchStream
...@@ -1142,7 +1138,6 @@ async function spawnRuntimePrefetchWithFilledCaches( ...@@ -1142,7 +1138,6 @@ async function spawnRuntimePrefetchWithFilledCaches(
// We want to be able to rewind the result to a session shell. // We want to be able to rewind the result to a session shell.
const mode: RuntimePrerenderMode & { type: 'navigation' } = { const mode: RuntimePrerenderMode & { type: 'navigation' } = {
type: 'navigation', type: 'navigation',
shellUsedSessionDataDeferred: createPromiseWithResolvers(),
shellByteLengthDeferred: createPromiseWithResolvers(), shellByteLengthDeferred: createPromiseWithResolvers(),
} }
...@@ -1152,7 +1147,6 @@ async function spawnRuntimePrefetchWithFilledCaches( ...@@ -1152,7 +1147,6 @@ async function spawnRuntimePrefetchWithFilledCaches(
generateDynamicRSCPayload.bind(null, ctx, { generateDynamicRSCPayload.bind(null, ctx, {
staleTimeIterable, staleTimeIterable,
shellByteLengthPromise: mode.shellByteLengthDeferred.promise, shellByteLengthPromise: mode.shellByteLengthDeferred.promise,
shellUsedSessionDataPromise: mode.shellUsedSessionDataDeferred.promise,
}), }),
prerenderResumeDataCache, prerenderResumeDataCache,
rootParams, rootParams,
...@@ -1527,13 +1521,9 @@ async function generateRuntimePrefetchResult( ...@@ -1527,13 +1521,9 @@ async function generateRuntimePrefetchResult(
) )
const mode: RuntimePrerenderMode = isShellPrefetch const mode: RuntimePrerenderMode = isShellPrefetch
? { ? { type: 'session-shell-only' }
type: 'session-shell-only',
shellUsedSessionDataDeferred: createPromiseWithResolvers(),
}
: { : {
type: 'rewindable-session-shell', type: 'rewindable-session-shell',
shellUsedSessionDataDeferred: createPromiseWithResolvers(),
shellByteLengthDeferred: createPromiseWithResolvers(), shellByteLengthDeferred: createPromiseWithResolvers(),
} }
...@@ -1564,7 +1554,6 @@ async function generateRuntimePrefetchResult( ...@@ -1564,7 +1554,6 @@ async function generateRuntimePrefetchResult(
mode.type === 'rewindable-session-shell' mode.type === 'rewindable-session-shell'
? mode.shellByteLengthDeferred.promise ? mode.shellByteLengthDeferred.promise
: undefined, : undefined,
shellUsedSessionDataPromise: mode.shellUsedSessionDataDeferred.promise,
}), }),
prerenderResumeDataCache, prerenderResumeDataCache,
rootParams, rootParams,
...@@ -1753,18 +1742,13 @@ function prependIsPartialByteToChunks( ...@@ -1753,18 +1742,13 @@ function prependIsPartialByteToChunks(
} }
type RuntimePrerenderMode = type RuntimePrerenderMode =
| { | { type: 'session-shell-only' }
type: 'session-shell-only'
shellUsedSessionDataDeferred: PromiseWithResolvers<boolean>
}
| { | {
type: 'rewindable-session-shell' type: 'rewindable-session-shell'
shellUsedSessionDataDeferred: PromiseWithResolvers<boolean>
shellByteLengthDeferred: PromiseWithResolvers<number | null> shellByteLengthDeferred: PromiseWithResolvers<number | null>
} }
| { | {
type: 'navigation' type: 'navigation'
shellUsedSessionDataDeferred: PromiseWithResolvers<boolean>
shellByteLengthDeferred: PromiseWithResolvers<number | null> shellByteLengthDeferred: PromiseWithResolvers<number | null>
} }
...@@ -1899,6 +1883,10 @@ async function finalRuntimeServerPrerender( ...@@ -1899,6 +1883,10 @@ async function finalRuntimeServerPrerender(
} }
} }
// Whether resolving link data unblocked new content. We check this right
// after the PrefetchRuntime stage, before anything later is flushed.
let didLinkDataUnblockNewContent = false
await runInSequentialTasks( await runInSequentialTasks(
async () => { async () => {
stageController.advanceStage(RenderStage.ShellStatic) stageController.advanceStage(RenderStage.ShellStatic)
...@@ -1955,25 +1943,9 @@ async function finalRuntimeServerPrerender( ...@@ -1955,25 +1943,9 @@ async function finalRuntimeServerPrerender(
() => { () => {
if (checkUnexpectedAbort()) return if (checkUnexpectedAbort()) return
// Check if session data unblocked new content in the shell. didLinkDataUnblockNewContent =
const didSessionDataUnblockNewContent = stageByteLengths[RenderStage.PrefetchRuntime] >
stageByteLengths[RenderStage.ShellRuntime] > stageByteLengths[RenderStage.ShellRuntime]
stageByteLengths[RenderStage.Static]
mode.shellUsedSessionDataDeferred.resolve(didSessionDataUnblockNewContent)
if ('shellByteLengthDeferred' in mode) {
// If advancing to the PrefetchRuntime stage didn't unblock new content,
// then the result does not depend on link data and can be used as a shell (indicated via `null`).
// Otherwise, send a byte length to indicate where the shell content ends.
const didLinkDataUnblockNewContent =
stageByteLengths[RenderStage.PrefetchRuntime] >
stageByteLengths[RenderStage.ShellRuntime]
mode.shellByteLengthDeferred.resolve(
didLinkDataUnblockNewContent
? stageByteLengths[RenderStage.ShellRuntime]
: null
)
}
}, },
() => { () => {
if (checkUnexpectedAbort()) return if (checkUnexpectedAbort()) return
...@@ -1988,6 +1960,28 @@ async function finalRuntimeServerPrerender( ...@@ -1988,6 +1960,28 @@ async function finalRuntimeServerPrerender(
// which is scheduled in a (fast) immediate, so we do this in a separate task // which is scheduled in a (fast) immediate, so we do this in a separate task
// (fast immediates will be drained at the end of the task, so in the next task we know we're done flushing) // (fast immediates will be drained at the end of the task, so in the next task we know we're done flushing)
// An aborted render emits no more chunks, so there's no offset to send.
if (
'shellByteLengthDeferred' in mode &&
!finalServerController.signal.aborted
) {
// If neither link data nor `navigation()` unblocked new content, then
// the result can be used as a shell (indicated via `null`). Otherwise,
// send a byte length to indicate where the shell content ends.
// NOTE: we must capture this *before* closing the accumulators below,
// which emit new rows. If the render stopped before the
// NavigationRuntime stage, that stage's count is the same as
// PrefetchRuntime's.
const didNavigationUnblockNewContent =
stageByteLengths[RenderStage.NavigationRuntime] >
stageByteLengths[RenderStage.PrefetchRuntime]
mode.shellByteLengthDeferred.resolve(
didLinkDataUnblockNewContent || didNavigationUnblockNewContent
? stageByteLengths[RenderStage.ShellRuntime]
: null
)
}
staleTimeIterable.close() staleTimeIterable.close()
finishAccumulatingVaryParams(varyParamsAccumulator) finishAccumulatingVaryParams(varyParamsAccumulator)
}, },
......
...@@ -336,7 +336,12 @@ function createStaticPrerenderParams( ...@@ -336,7 +336,12 @@ function createStaticPrerenderParams(
) )
} }
if (isEmptyParams(underlyingParams)) { // An optional catch-all with no value isn't a property of the params
// object, but reading it is still a param read, so the route has params.
if (
isEmptyParams(underlyingParams) &&
optionalCatchAllParamName === null
) {
// This route has no params. // This route has no params.
return makeUntrackedParams(userspaceParams) return makeUntrackedParams(userspaceParams)
} }
...@@ -356,6 +361,7 @@ function createStaticPrerenderParams( ...@@ -356,6 +361,7 @@ function createStaticPrerenderParams(
// by delaying them to the static stage. However, root params are allowed in shells, // by delaying them to the static stage. However, root params are allowed in shells,
// so if all the params are root params, they can be included as well. // so if all the params are root params, they can be included as well.
if ( if (
optionalCatchAllParamName !== null ||
!allParamsAreRootParams(underlyingParams, prerenderStore.rootParams) !allParamsAreRootParams(underlyingParams, prerenderStore.rootParams)
) { ) {
const staticParamsStage = const staticParamsStage =
...@@ -423,13 +429,18 @@ function createRuntimePrerenderParams( ...@@ -423,13 +429,18 @@ function createRuntimePrerenderParams(
) )
} }
if (isEmptyParams(underlyingParams)) { // An absent optional catch-all is still a param (see
// createStaticPrerenderParams).
if (isEmptyParams(underlyingParams) && optionalCatchAllParamName === null) {
// This route has no params. // This route has no params.
return makeUntrackedParams(userspaceParams) return makeUntrackedParams(userspaceParams)
} }
// Root params are allowed in shells, so we allow them to resolve without a delay. // Root params are allowed in shells, so we allow them to resolve without a delay.
if (allParamsAreRootParams(underlyingParams, workUnitStore.rootParams)) { if (
optionalCatchAllParamName === null &&
allParamsAreRootParams(underlyingParams, workUnitStore.rootParams)
) {
return makeUntrackedParams(userspaceParams) return makeUntrackedParams(userspaceParams)
} }
...@@ -496,6 +507,7 @@ function createRenderParamsForPage( ...@@ -496,6 +507,7 @@ function createRenderParamsForPage(
stagedRendering, stagedRendering,
asyncApiPromises, asyncApiPromises,
underlyingParams, underlyingParams,
optionalCatchAllParamName,
userspaceParams userspaceParams
) )
} }
...@@ -521,6 +533,7 @@ function createStagedRenderParams( ...@@ -521,6 +533,7 @@ function createStagedRenderParams(
stagedRendering: NonNullable<RequestStore['stagedRendering']>, stagedRendering: NonNullable<RequestStore['stagedRendering']>,
asyncApiPromises: NonNullable<RequestStore['asyncApiPromises']>, asyncApiPromises: NonNullable<RequestStore['asyncApiPromises']>,
underlyingParams: Params, underlyingParams: Params,
optionalCatchAllParamName: string | null,
userspaceParams: Params userspaceParams: Params
) { ) {
const promise = createStagedRenderParamsImpl( const promise = createStagedRenderParamsImpl(
...@@ -528,6 +541,7 @@ function createStagedRenderParams( ...@@ -528,6 +541,7 @@ function createStagedRenderParams(
stagedRendering, stagedRendering,
asyncApiPromises, asyncApiPromises,
underlyingParams, underlyingParams,
optionalCatchAllParamName,
userspaceParams userspaceParams
) )
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
...@@ -547,11 +561,13 @@ function createStagedRenderParamsImpl( ...@@ -547,11 +561,13 @@ function createStagedRenderParamsImpl(
asyncApiPromises: NonNullable<RequestStore['asyncApiPromises']>, asyncApiPromises: NonNullable<RequestStore['asyncApiPromises']>,
/** The actual param values, without any instrumentation */ /** The actual param values, without any instrumentation */
underlyingParams: Params, underlyingParams: Params,
/** An optional catch-all param with no value, which is still a param */
optionalCatchAllParamName: string | null,
/** The params object to return to userspace, possibly wrapped in a proxy */ /** The params object to return to userspace, possibly wrapped in a proxy */
userspaceParams: Params userspaceParams: Params
) { ) {
// If the route has no params, they should resolve immediately. // If the route has no params, they should resolve immediately.
if (isEmptyParams(underlyingParams)) { if (isEmptyParams(underlyingParams) && optionalCatchAllParamName === null) {
return makeUntrackedParams(userspaceParams) return makeUntrackedParams(userspaceParams)
} }
...@@ -570,7 +586,10 @@ function createStagedRenderParamsImpl( ...@@ -570,7 +586,10 @@ function createStagedRenderParamsImpl(
// If we're rendering with shells, even static params must be delayed to exclude them from the shell. // If we're rendering with shells, even static params must be delayed to exclude them from the shell.
// However, root params are allowed in shells, so if all the params are root params, they can be included as well. // However, root params are allowed in shells, so if all the params are root params, they can be included as well.
if (!allParamsAreRootParams(underlyingParams, workUnitStore.rootParams)) { if (
optionalCatchAllParamName !== null ||
!allParamsAreRootParams(underlyingParams, workUnitStore.rootParams)
) {
// For a dynamic request we generally want to recover a static shell, // For a dynamic request we generally want to recover a static shell,
// so static params can resolve in the static stage, because session // so static params can resolve in the static stage, because session
// shells are handled with a separate render. // shells are handled with a separate render.
......
...@@ -573,14 +573,6 @@ type NavigationFlightResponseBase = { ...@@ -573,14 +573,6 @@ type NavigationFlightResponseBase = {
* response. * response.
*/ */
u?: Promise<boolean> u?: Promise<boolean>
/**
* shellUsedSessionData — present only in runtime-shell prefetch
* responses: true if resolving session data unblocked new content in the
* shell. Only meaningful where there's a proper session shell.
* Promise-encoded so the answer rewinds with a truncated decode.
* Currently has no client consumer; emitted for forward compatibility.
*/
w?: Promise<boolean>
/** /**
* isUpgradeableISRFallback — present (true) iff this is a per-segment * isUpgradeableISRFallback — present (true) iff this is a per-segment
* prefetch response generated from a fallback shell render (i.e. the page * prefetch response generated from a fallback shell render (i.e. the page
......
...@@ -20,18 +20,14 @@ describe('segment cache - static shell vary params regression', () => { ...@@ -20,18 +20,14 @@ describe('segment cache - static shell vary params regression', () => {
// which replaces every non-root param with Fallback. That's only correct // which replaces every non-root param with Fallback. That's only correct
// when the payload really is the param-independent shell. // when the payload really is the param-independent shell.
// //
// When a route renders with no dynamic hole, the response has no shell/full // On an optional catch-all, the index (empty slug) is the case that hit
// split — the "shell" payload IS the concrete render for the params that // this: prefetching /docs poisoned /docs/alpha and /docs/beta, so later
// were requested. Keying it at the wildcard path published one param's page // navigations rendered the index page and skipped the network entirely.
// as the answer for every other param value, and recorded it at the highest
// tier, so later navigations rendered the wrong page and skipped the network
// entirely.
// //
// On an optional catch-all, the index (empty slug) is the case that hits // The fix keys entries by the vary params the server reports, so we only
// this: prefetching /docs poisoned /docs/alpha and /docs/beta. // use the shell path when the segment really doesn't depend on the params.
// // The server also keeps the index's absent slug out of the shell, like any
// The fix makes the server-reported vary params authoritative, so the shell // other param, so the shared shell has no slug-dependent content.
// path is used exactly when the segment really is param-independent.
it('does not serve the catch-all index page for a different slug', async () => { it('does not serve the catch-all index page for a different slug', async () => {
let page: Playwright.Page let page: Playwright.Page
const browser = await next.browser('/', { const browser = await next.browser('/', {
...@@ -41,9 +37,8 @@ describe('segment cache - static shell vary params regression', () => { ...@@ -41,9 +37,8 @@ describe('segment cache - static shell vary params regression', () => {
}) })
const act = createRouterAct(page, { includeAppShellRequests: true }) const act = createRouterAct(page, { includeAppShellRequests: true })
// Prefetch the fully static index of the optional catch-all route. Its // Prefetch the fully static index of the optional catch-all route. This is
// response has no shell/full split, so this is the write that used to land // the payload that used to be stored at the shell vary path.
// in the param-wildcard slot.
await act( await act(
async () => { async () => {
await browser.elementByCss('input[data-link-accordion="/docs"]').click() await browser.elementByCss('input[data-link-accordion="/docs"]').click()
...@@ -59,24 +54,26 @@ describe('segment cache - static shell vary params regression', () => { ...@@ -59,24 +54,26 @@ describe('segment cache - static shell vary params regression', () => {
) )
}, 'no-requests') }, 'no-requests')
// Now prefetch a different slug on the same route. Before the fix the // Now prefetch a different slug on the same route. The shared shell is
// index's page segment was sitting in the wildcard slot marked complete, // already cached, and a shell link only needs the shell, so nothing is
// so this fired no request at all for the page content and this // requested.
// expectation would time out. await act(async () => {
await browser
.elementByCss('input[data-link-accordion="/docs/alpha"]')
.click()
}, 'no-requests')
// The navigation fetches alpha's page. Before the fix, the index's page
// segment was stored at the shell vary path and marked complete, so this
// rendered the index page without a request.
await act( await act(
async () => { async () => {
await browser await browser.elementByCss('a[href="/docs/alpha"]').click()
.elementByCss('input[data-link-accordion="/docs/alpha"]')
.click()
}, },
{ includes: 'Docs: alpha' } { includes: 'Docs: alpha' }
) )
expect(await browser.elementById('docs-page-alpha').text()).toBe(
await act(async () => { 'Docs: alpha'
await browser.elementByCss('a[href="/docs/alpha"]').click() )
expect(await browser.elementById('docs-page-alpha').text()).toBe(
'Docs: alpha'
)
}, 'no-requests')
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment