Unverified Commit d056188b authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[App Shells] Track whether shell prefetch used session data (#94484)

When we send a `next-router-prefetch: 3` request, we now track if
unblocking session data produced new content. Exposed as `u?:
Promise<boolean>` on the navigation response.
parent fd2669ea
...@@ -604,6 +604,7 @@ async function generateDynamicRSCPayload( ...@@ -604,6 +604,7 @@ async function generateDynamicRSCPayload(
staleTimeIterable?: AsyncIterable<number> staleTimeIterable?: AsyncIterable<number>
staticStageByteLengthPromise?: Promise<number> staticStageByteLengthPromise?: Promise<number>
shellByteLengthPromise?: Promise<number | null> shellByteLengthPromise?: Promise<number | null>
shellUsedSessionDataPromise?: Promise<boolean>
runtimePrefetchStream?: ReadableStream<Uint8Array> runtimePrefetchStream?: ReadableStream<Uint8Array>
} }
): Promise<RSCPayload> { ): Promise<RSCPayload> {
...@@ -752,6 +753,9 @@ async function generateDynamicRSCPayload( ...@@ -752,6 +753,9 @@ async function generateDynamicRSCPayload(
if (options?.shellByteLengthPromise !== undefined) { if (options?.shellByteLengthPromise !== undefined) {
baseResponse.a = options.shellByteLengthPromise baseResponse.a = options.shellByteLengthPromise
} }
if (options?.shellUsedSessionDataPromise !== undefined) {
baseResponse.u = options.shellUsedSessionDataPromise
}
if (options?.runtimePrefetchStream !== undefined) { if (options?.runtimePrefetchStream !== undefined) {
baseResponse.p = options.runtimePrefetchStream baseResponse.p = options.runtimePrefetchStream
...@@ -1098,6 +1102,7 @@ async function spawnRuntimePrefetchWithFilledCaches( ...@@ -1098,6 +1102,7 @@ async function spawnRuntimePrefetchWithFilledCaches(
? // If appShells is on, we want to be able to rewind the result to a session shell. ? // If appShells is on, we want to be able to rewind the result to a session shell.
{ {
type: 'rewindable-session-shell', type: 'rewindable-session-shell',
shellUsedSessionDataDeferred: createPromiseWithResolvers(),
shellByteLengthDeferred: createPromiseWithResolvers(), shellByteLengthDeferred: createPromiseWithResolvers(),
} }
: // Otherwise, render everything without considering shells. : // Otherwise, render everything without considering shells.
...@@ -1112,6 +1117,10 @@ async function spawnRuntimePrefetchWithFilledCaches( ...@@ -1112,6 +1117,10 @@ async function spawnRuntimePrefetchWithFilledCaches(
mode.type === 'rewindable-session-shell' mode.type === 'rewindable-session-shell'
? mode.shellByteLengthDeferred.promise ? mode.shellByteLengthDeferred.promise
: undefined, : undefined,
shellUsedSessionDataPromise:
mode.type !== 'runtime-only'
? mode.shellUsedSessionDataDeferred.promise
: undefined,
}), }),
prerenderResumeDataCache, prerenderResumeDataCache,
rootParams, rootParams,
...@@ -1440,9 +1449,13 @@ async function generateRuntimePrefetchResult( ...@@ -1440,9 +1449,13 @@ async function generateRuntimePrefetchResult(
const mode: RuntimePrerenderMode = appShells const mode: RuntimePrerenderMode = appShells
? isShellPrefetch ? 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(),
} }
: { type: 'runtime-only' } : { type: 'runtime-only' }
...@@ -1456,6 +1469,10 @@ async function generateRuntimePrefetchResult( ...@@ -1456,6 +1469,10 @@ async function generateRuntimePrefetchResult(
mode.type === 'rewindable-session-shell' mode.type === 'rewindable-session-shell'
? mode.shellByteLengthDeferred.promise ? mode.shellByteLengthDeferred.promise
: undefined, : undefined,
shellUsedSessionDataPromise:
mode.type !== 'runtime-only'
? mode.shellUsedSessionDataDeferred.promise
: undefined,
}), }),
prerenderResumeDataCache, prerenderResumeDataCache,
rootParams, rootParams,
...@@ -1629,9 +1646,13 @@ function prependIsPartialByteToChunks( ...@@ -1629,9 +1646,13 @@ function prependIsPartialByteToChunks(
type RuntimePrerenderMode = type RuntimePrerenderMode =
| { type: 'runtime-only' } | { type: 'runtime-only' }
| { 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>
} }
...@@ -1710,7 +1731,10 @@ async function finalRuntimeServerPrerender( ...@@ -1710,7 +1731,10 @@ async function finalRuntimeServerPrerender(
const streamState = createStreamPendingState() const streamState = createStreamPendingState()
const collectedChunks = createPrerenderChunksAccumulator() const collectedChunks = createPrerenderChunksAccumulator()
const stageByteLengths = const stageByteLengths =
mode.type === 'rewindable-session-shell' ? createStageByteLengths() : null mode.type === 'rewindable-session-shell' ||
mode.type === 'session-shell-only'
? createStageByteLengths()
: null
await runInSequentialTasks( await runInSequentialTasks(
async () => { async () => {
...@@ -1809,6 +1833,20 @@ async function finalRuntimeServerPrerender( ...@@ -1809,6 +1833,20 @@ async function finalRuntimeServerPrerender(
return return
} }
// Check if session data unblocked new content in the shell.
if (
(mode.type === 'rewindable-session-shell' ||
mode.type === 'session-shell-only') &&
stageByteLengths
) {
const didSessionDataUnblockNewContent =
stageByteLengths[RenderStage.ShellRuntime] >
stageByteLengths[RenderStage.Static]
mode.shellUsedSessionDataDeferred.resolve(
didSessionDataUnblockNewContent
)
}
if (mode.type === 'rewindable-session-shell' && stageByteLengths) { if (mode.type === 'rewindable-session-shell' && stageByteLengths) {
// If advancing to the runtime stage didn't unblock new content, // If advancing to the runtime 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`). // then the result does not depend on link data and can be used as a shell (indicated via `null`).
......
...@@ -449,6 +449,13 @@ export type NavigationFlightResponse = { ...@@ -449,6 +449,13 @@ export type NavigationFlightResponse = {
* If it resolves to null, then the shell is the same as the main response. * If it resolves to null, then the shell is the same as the main response.
* */ * */
a?: Promise<number | null> a?: Promise<number | null>
/**
* shellUsedSessionData - true if resolving session data
* unblocked new content in the shell.
* NOTE: only use this in runtime/session prefetch requests
* where we have a proper session shell.
* */
u?: Promise<boolean>
/** headVaryParams */ /** headVaryParams */
h: VaryParamsThenable | null h: VaryParamsThenable | null
/** runtimePrefetchStream — Embedded runtime prefetch Flight stream. */ /** runtimePrefetchStream — Embedded runtime prefetch Flight stream. */
......
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