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

refactor: split out validateStaticShell (#95106)

This is a pure refactor, split from #94595 to reduce the diff
- Move static shell validation code into a separate function,
`validateStaticShell`
- Rename `spawnStaticShellValidationInDev` to `runValidationInDev`. This
is outdated: it no longert spawns anything, and also does more than just
static shell validation

Also includes a drive-by cleanup in
`renderWithWarmCachesForValidationInDev` that was not worth a separate
PR : we can drop an unnecessary `accumulatedChunksPromise` wrapper,
because it's immediately awaited.
parent 792f7463
...@@ -3020,7 +3020,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -3020,7 +3020,7 @@ async function renderToHTMLOrFlightImpl(
// duplicate the entry in the dev overlay. // duplicate the entry in the dev overlay.
// //
// The cacheComponents paths forward this themselves via // The cacheComponents paths forward this themselves via
// `spawnStaticShellValidationInDev` and the validation-skipped fallback in // `runValidationInDev` and the validation-skipped fallback in
// `generateDynamicFlightRenderResultWithStagesInDev`. Here we cover the // `generateDynamicFlightRenderResultWithStagesInDev`. Here we cover the
// non-cacheComponents dev path where neither runs. // non-cacheComponents dev path where neither runs.
if ( if (
...@@ -4313,7 +4313,7 @@ interface StagedDevRenderArtifacts { ...@@ -4313,7 +4313,7 @@ interface StagedDevRenderArtifacts {
} }
/** /**
* Everything `spawnStaticShellValidationInDev` needs to validate a render. * Everything `runValidationInDev` needs to validate a render.
* These are sourced from whichever render is prod-representative: the streamed * These are sourced from whichever render is prod-representative: the streamed
* render when it neither missed caches nor hit sync IO, otherwise a validation * render when it neither missed caches nor hit sync IO, otherwise a validation
* render. * render.
...@@ -4504,7 +4504,7 @@ function runDevValidationInBackground( ...@@ -4504,7 +4504,7 @@ function runDevValidationInBackground(
case 'validate': case 'validate':
// The streamed render is prod-representative; validate its own // The streamed render is prod-representative; validate its own
// chunks. // chunks.
return spawnStaticShellValidationInDev( return runValidationInDev(
plan.inputs, plan.inputs,
ctx, ctx,
fallbackRouteParams, fallbackRouteParams,
...@@ -4537,7 +4537,7 @@ function runDevValidationInBackground( ...@@ -4537,7 +4537,7 @@ function runDevValidationInBackground(
return return
} }
return spawnStaticShellValidationInDev( return runValidationInDev(
inputs, inputs,
ctx, ctx,
fallbackRouteParams, fallbackRouteParams,
...@@ -4935,10 +4935,10 @@ async function renderWithWarmCachesForValidationInDev( ...@@ -4935,10 +4935,10 @@ async function renderWithWarmCachesForValidationInDev(
const environmentName = () => const environmentName = () =>
getEnvironmentNameForStage(stageController.currentStage) getEnvironmentNameForStage(stageController.currentStage)
let startTime = -Infinity
const rscPayload = await getPayload(requestStore) const rscPayload = await getPayload(requestStore)
const { accumulatedChunksPromise } = await runInSequentialTasks( let startTime = -Infinity
const accumulatedChunks = await runInSequentialTasks(
() => { () => {
stageController.advanceStage(RenderStage.ShellEarlyStatic) stageController.advanceStage(RenderStage.ShellEarlyStatic)
startTime = performance.now() + performance.timeOrigin startTime = performance.now() + performance.timeOrigin
...@@ -4958,13 +4958,7 @@ async function renderWithWarmCachesForValidationInDev( ...@@ -4958,13 +4958,7 @@ async function renderWithWarmCachesForValidationInDev(
} }
) as Readable ) as Readable
return { return accumulateStreamChunks(sourceStream, stageController, null)
accumulatedChunksPromise: accumulateStreamChunks(
sourceStream,
stageController,
null
),
}
}, },
() => stageController.advanceStage(RenderStage.ShellStatic), () => stageController.advanceStage(RenderStage.ShellStatic),
() => stageController.advanceStage(RenderStage.EarlyStatic), () => stageController.advanceStage(RenderStage.EarlyStatic),
...@@ -4976,11 +4970,6 @@ async function renderWithWarmCachesForValidationInDev( ...@@ -4976,11 +4970,6 @@ async function renderWithWarmCachesForValidationInDev(
() => stageController.advanceStage(RenderStage.Dynamic) () => stageController.advanceStage(RenderStage.Dynamic)
) )
// The render isn't complete until its stream has finished; reading the
// accumulation here (rather than handing back a promise) keeps a late
// `syncInterruptReason` from the dynamic stage final.
const accumulatedChunks = await accumulatedChunksPromise
return { return {
accumulatedChunks, accumulatedChunks,
syncInterruptReason: stageController.getSyncInterruptReason(), syncInterruptReason: stageController.getSyncInterruptReason(),
...@@ -5470,8 +5459,8 @@ function logValidationSkipped(ctx: AppRenderContext) { ...@@ -5470,8 +5459,8 @@ function logValidationSkipped(ctx: AppRenderContext) {
} }
} }
async function spawnStaticShellValidationInDev( async function runValidationInDev(
...args: Parameters<typeof spawnStaticShellValidationInDevImpl> ...args: Parameters<typeof runValidationInDevImpl>
) { ) {
if (process.env.__NEXT_TEST_MODE && process.env.NEXT_TEST_LOG_VALIDATION) { if (process.env.__NEXT_TEST_MODE && process.env.NEXT_TEST_LOG_VALIDATION) {
const ctx: AppRenderContext = args[1] const ctx: AppRenderContext = args[1]
...@@ -5483,7 +5472,7 @@ async function spawnStaticShellValidationInDev( ...@@ -5483,7 +5472,7 @@ async function spawnStaticShellValidationInDev(
'</VALIDATION_MESSAGE>' '</VALIDATION_MESSAGE>'
) )
try { try {
return await spawnStaticShellValidationInDevImpl(...args) return await runValidationInDevImpl(...args)
} finally { } finally {
console.log( console.log(
'<VALIDATION_MESSAGE>' + '<VALIDATION_MESSAGE>' +
...@@ -5492,7 +5481,7 @@ async function spawnStaticShellValidationInDev( ...@@ -5492,7 +5481,7 @@ async function spawnStaticShellValidationInDev(
) )
} }
} else { } else {
return await spawnStaticShellValidationInDevImpl(...args) return await runValidationInDevImpl(...args)
} }
} }
...@@ -5502,59 +5491,20 @@ async function spawnStaticShellValidationInDev( ...@@ -5502,59 +5491,20 @@ async function spawnStaticShellValidationInDev(
* prerender semantics to prerenderToStream and should update it * prerender semantics to prerenderToStream and should update it
* in conjunction with any changes to that function. * in conjunction with any changes to that function.
*/ */
async function spawnStaticShellValidationInDevImpl( async function runValidationInDevImpl(
inputs: DevValidationInputs, inputs: DevValidationInputs,
ctx: AppRenderContext, ctx: AppRenderContext,
fallbackRouteParams: OpaqueFallbackRouteParams | null, fallbackRouteParams: OpaqueFallbackRouteParams | null,
devRenderDidError: boolean devRenderDidError: boolean
): Promise<void> { ): Promise<void> {
const debug = if (inputs.syncInterruptReason) {
process.env.NEXT_PRIVATE_DEBUG_VALIDATION === '1' ? console.log : undefined return logMessagesAndSendErrorsToBrowser([inputs.syncInterruptReason], ctx)
}
const {
componentMod: ComponentMod,
getDynamicParamFromSegment,
renderOpts,
} = ctx
const { componentMod: ComponentMod, getDynamicParamFromSegment } = ctx
const loaderTree = ComponentMod.routeModule.userland.loaderTree const loaderTree = ComponentMod.routeModule.userland.loaderTree
const allowEmptyStaticShell =
(renderOpts.allowEmptyStaticShell ?? false) ||
(await isPageAllowedToBlock(loaderTree))
const rootParams = getRootParams(loaderTree, getDynamicParamFromSegment) const rootParams = getRootParams(loaderTree, getDynamicParamFromSegment)
// The inputs come from whichever render is prod-representative: the streamed
// render, or a validation render produced once caches were filled.
const {
accumulatedChunks,
syncInterruptReason,
startTime,
staticStageEndTime,
runtimeStageEndTime,
requestStore,
debugChannelClient,
} = inputs
const hmrRefreshHash = getHmrRefreshHash(requestStore)
if (syncInterruptReason) {
return logMessagesAndSendErrorsToBrowser([syncInterruptReason], ctx)
}
let debugChunks: Uint8Array[] | null = null
if (debugChannelClient) {
debugChunks = []
;(async () => {
for await (const c of debugChannelClient) {
debugChunks.push(c)
}
})()
}
const { staticChunks, runtimeChunks, dynamicChunks } = accumulatedChunks
const needsInstantValidation = const needsInstantValidation =
await anySegmentNeedsInstantValidationInDev(loaderTree) await anySegmentNeedsInstantValidationInDev(loaderTree)
...@@ -5562,6 +5512,9 @@ async function spawnStaticShellValidationInDevImpl( ...@@ -5562,6 +5512,9 @@ async function spawnStaticShellValidationInDevImpl(
const validationSamples = null const validationSamples = null
const validationSampleTracking = null const validationSampleTracking = null
{
const { runtimeChunks, dynamicChunks } = inputs.accumulatedChunks
// First we warmup SSR with the runtime chunks. This ensures that when we do // First we warmup SSR with the runtime chunks. This ensures that when we do
// the full prerender pass with dynamic tracking module loading won't // the full prerender pass with dynamic tracking module loading won't
// interrupt the prerender and can properly observe the entire content // interrupt the prerender and can properly observe the entire content
...@@ -5578,9 +5531,82 @@ async function spawnStaticShellValidationInDevImpl( ...@@ -5578,9 +5531,82 @@ async function spawnStaticShellValidationInDevImpl(
validationSamples, validationSamples,
validationSampleTracking validationSampleTracking
) )
}
const debugChunks = await collectDebugChunksFromClientChannel(
inputs.debugChannelClient
)
const hmrRefreshHash = getHmrRefreshHash(inputs.requestStore)
const result = await validateStaticShell(
inputs,
ctx,
rootParams,
fallbackRouteParams,
debugChunks,
hmrRefreshHash
)
if (result.length > 0) {
return logMessagesAndSendErrorsToBrowser(result, ctx)
}
if (needsInstantValidation) {
const instantConfigsResult = await validateInstantConfigs(
inputs.accumulatedChunks,
debugChunks,
inputs.startTime,
rootParams,
fallbackRouteParams,
ctx,
hmrRefreshHash,
validationSamples,
devRenderDidError
)
if (instantConfigsResult.length > 0) {
return logMessagesAndSendErrorsToBrowser(instantConfigsResult, ctx)
}
}
}
async function collectDebugChunksFromClientChannel(
debugChannel: AnyStream | undefined
) {
if (!debugChannel) {
return null
}
const debugChunks: Uint8Array[] = []
for await (const c of debugChannel) {
debugChunks.push(c)
}
return debugChunks
}
async function validateStaticShell(
inputs: DevValidationInputs,
ctx: AppRenderContext,
rootParams: Params,
fallbackRouteParams: OpaqueFallbackRouteParams | null,
debugChunks: Uint8Array[] | null,
hmrRefreshHash: string | undefined
): Promise<unknown[]> {
const debug =
process.env.NEXT_PRIVATE_DEBUG_VALIDATION === '1' ? console.log : undefined
debug?.(`Starting static shell validation...`) debug?.(`Starting static shell validation...`)
const { componentMod: ComponentMod, renderOpts } = ctx
const loaderTree = ComponentMod.routeModule.userland.loaderTree
const { accumulatedChunks, staticStageEndTime, runtimeStageEndTime } = inputs
const { staticChunks, runtimeChunks, dynamicChunks } = accumulatedChunks
const allowEmptyStaticShell =
(renderOpts.allowEmptyStaticShell ?? false) ||
(await isPageAllowedToBlock(loaderTree))
const runtimeResult = await validateStagedShell( const runtimeResult = await validateStagedShell(
runtimeChunks, runtimeChunks,
dynamicChunks, dynamicChunks,
...@@ -5598,7 +5624,7 @@ async function spawnStaticShellValidationInDevImpl( ...@@ -5598,7 +5624,7 @@ async function spawnStaticShellValidationInDevImpl(
debug?.(`❌ Failed - ${runtimeResult.length} errors from runtime stage`) debug?.(`❌ Failed - ${runtimeResult.length} errors from runtime stage`)
// We have something to report from the runtime validation // We have something to report from the runtime validation
// We can skip the rest // We can skip the rest
return logMessagesAndSendErrorsToBrowser(runtimeResult, ctx) return runtimeResult
} }
const staticResult = await validateStagedShell( const staticResult = await validateStagedShell(
...@@ -5618,27 +5644,10 @@ async function spawnStaticShellValidationInDevImpl( ...@@ -5618,27 +5644,10 @@ async function spawnStaticShellValidationInDevImpl(
debug?.(`❌ Failed - ${staticResult.length} errors from static stage`) debug?.(`❌ Failed - ${staticResult.length} errors from static stage`)
// We have something to report from the static validation // We have something to report from the static validation
// We can skip the rest // We can skip the rest
return logMessagesAndSendErrorsToBrowser(staticResult, ctx) return staticResult
} }
debug?.(`✅ Passed`) debug?.(`✅ Passed`)
return []
if (needsInstantValidation) {
const instantConfigsResult = await validateInstantConfigs(
accumulatedChunks,
debugChunks,
startTime,
rootParams,
fallbackRouteParams,
ctx,
hmrRefreshHash,
validationSamples,
devRenderDidError
)
if (instantConfigsResult.length > 0) {
return logMessagesAndSendErrorsToBrowser(instantConfigsResult, ctx)
}
}
} }
async function warmupClientModulesForStagedValidation( async function warmupClientModulesForStagedValidation(
......
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