Address review feedback

parent d37b6cdf
...@@ -849,7 +849,7 @@ async function generateDynamicFlightRenderResult( ...@@ -849,7 +849,7 @@ async function generateDynamicFlightRenderResult(
// response-close to abort the discarded render. This is the // response-close to abort the discarded render. This is the
// non-Cache-Components dev RSC path; unlike the Cache Components staged path // non-Cache-Components dev RSC path; unlike the Cache Components staged path
// it has no detached validation, so the render is the only work to cancel. // it has no detached validation, so the render is the only work to cancel.
const abortSignal = const requestAbortSignal =
process.env.__NEXT_DEV_SERVER && process.env.__NEXT_DEV_SERVER &&
renderOpts.experimental.serverComponentsHmrCancellation === true && renderOpts.experimental.serverComponentsHmrCancellation === true &&
requestStore.isHmrRefresh === true && requestStore.isHmrRefresh === true &&
...@@ -884,7 +884,7 @@ async function generateDynamicFlightRenderResult( ...@@ -884,7 +884,7 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences, temporaryReferences: options?.temporaryReferences,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
signal: abortSignal, signal: requestAbortSignal,
} }
) )
...@@ -920,7 +920,7 @@ async function generateDynamicFlightRenderResult( ...@@ -920,7 +920,7 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences, temporaryReferences: options?.temporaryReferences,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
signal: abortSignal, signal: requestAbortSignal,
} }
) )
...@@ -1415,7 +1415,7 @@ async function generateDynamicFlightRenderResultWithStagesInDev( ...@@ -1415,7 +1415,7 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
// cancel today. The response-close signal itself is general, so this could // cancel today. The response-close signal itself is general, so this could
// later be relaxed to also cover a browser stop or a devtools "cancel // later be relaxed to also cover a browser stop or a devtools "cancel
// render" button. // render" button.
const abortSignal = const requestAbortSignal =
renderOpts.experimental.serverComponentsHmrCancellation === true && renderOpts.experimental.serverComponentsHmrCancellation === true &&
initialRequestStore.isHmrRefresh === true && initialRequestStore.isHmrRefresh === true &&
isNodeNextResponse(ctx.res) isNodeNextResponse(ctx.res)
...@@ -1436,7 +1436,7 @@ async function generateDynamicFlightRenderResultWithStagesInDev( ...@@ -1436,7 +1436,7 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
type: 'prefetched-client', type: 'prefetched-client',
prefetchStage, prefetchStage,
}, },
abortSignal, requestAbortSignal,
}) })
stream = result.stream stream = result.stream
debugChannel = result.debugChannel debugChannel = result.debugChannel
...@@ -3520,8 +3520,8 @@ async function renderToStream( ...@@ -3520,8 +3520,8 @@ async function renderToStream(
navigationKind: { navigationKind: {
type: 'initial-load', type: 'initial-load',
}, },
// The initial load has no request-abort signal to tear it down. // We currently only abort HMR refresh requests.
abortSignal: undefined, requestAbortSignal: undefined,
}) })
reactServerResult = new ReactServerResult(serverStream) reactServerResult = new ReactServerResult(serverStream)
...@@ -4461,7 +4461,7 @@ function runDevValidationInBackground( ...@@ -4461,7 +4461,7 @@ function runDevValidationInBackground(
createRequestStore: () => RequestStore, createRequestStore: () => RequestStore,
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void, onError: (error: unknown) => void,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): void { ): void {
void consoleAsyncStorage void consoleAsyncStorage
.run({ dim: true }, async () => { .run({ dim: true }, async () => {
...@@ -4472,7 +4472,7 @@ function runDevValidationInBackground( ...@@ -4472,7 +4472,7 @@ function runDevValidationInBackground(
// re-rendering and analyzing it would only waste server work. In-flight // re-rendering and analyzing it would only waste server work. In-flight
// `'use cache'` fills are left running (we don't await `cacheReady` // `'use cache'` fills are left running (we don't await `cacheReady`
// below). // below).
if (abortSignal?.aborted) { if (requestAbortSignal?.aborted) {
logValidationAborted(ctx) logValidationAborted(ctx)
return return
} }
...@@ -4511,7 +4511,7 @@ function runDevValidationInBackground( ...@@ -4511,7 +4511,7 @@ function runDevValidationInBackground(
createRequestStore, createRequestStore,
getPayload, getPayload,
onError, onError,
abortSignal requestAbortSignal
) )
// If we need to do multiple renders, do them in parallel. // If we need to do multiple renders, do them in parallel.
...@@ -4536,7 +4536,7 @@ function runDevValidationInBackground( ...@@ -4536,7 +4536,7 @@ function runDevValidationInBackground(
// The request may have been aborted while we prepared the validation // The request may have been aborted while we prepared the validation
// inputs above (which can itself render). Skip validation before it // inputs above (which can itself render). Skip validation before it
// begins rather than run it for a discarded request. // begins rather than run it for a discarded request.
if (abortSignal?.aborted) { if (requestAbortSignal?.aborted) {
logValidationAborted(ctx) logValidationAborted(ctx)
return return
} }
...@@ -4548,7 +4548,7 @@ function runDevValidationInBackground( ...@@ -4548,7 +4548,7 @@ function runDevValidationInBackground(
ctx, ctx,
fallbackRouteParams, fallbackRouteParams,
devRenderDidError, devRenderDidError,
abortSignal requestAbortSignal
) )
}) })
// The catch keeps a failed render, or anything thrown inside validation, // The catch keeps a failed render, or anything thrown inside validation,
...@@ -4557,7 +4557,7 @@ function runDevValidationInBackground( ...@@ -4557,7 +4557,7 @@ function runDevValidationInBackground(
// If this request was aborted, its render and detached validation are // If this request was aborted, its render and detached validation are
// being torn down and their result discarded. We don't want to log these // being torn down and their result discarded. We don't want to log these
// errors (including the abort signal reason itself). // errors (including the abort signal reason itself).
if (abortSignal?.aborted) { if (requestAbortSignal?.aborted) {
return return
} }
console.error( console.error(
...@@ -4624,7 +4624,7 @@ async function prepareValidationInputs( ...@@ -4624,7 +4624,7 @@ async function prepareValidationInputs(
createRequestStore: () => RequestStore, createRequestStore: () => RequestStore,
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void, onError: (error: unknown) => void,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): Promise<PrepareValidationInputsResult> { ): Promise<PrepareValidationInputsResult> {
// Check if we can re-use the main render for validation. // Check if we can re-use the main render for validation.
let inputsFromNavigation: DevValidationInputs | null let inputsFromNavigation: DevValidationInputs | null
...@@ -4654,7 +4654,7 @@ async function prepareValidationInputs( ...@@ -4654,7 +4654,7 @@ async function prepareValidationInputs(
getPayload, getPayload,
onError, onError,
inputsFromNavigation, inputsFromNavigation,
abortSignal requestAbortSignal
) )
} else { } else {
return prepareValidationInputsInLegacyPrefetching( return prepareValidationInputsInLegacyPrefetching(
...@@ -4664,7 +4664,7 @@ async function prepareValidationInputs( ...@@ -4664,7 +4664,7 @@ async function prepareValidationInputs(
getPayload, getPayload,
onError, onError,
inputsFromNavigation, inputsFromNavigation,
abortSignal requestAbortSignal
) )
} }
} }
...@@ -4678,7 +4678,7 @@ async function prepareValidationInputsInPartialPrefetching( ...@@ -4678,7 +4678,7 @@ async function prepareValidationInputsInPartialPrefetching(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void, onError: (error: unknown) => void,
inputsFromNavigation: DevValidationInputs | null, inputsFromNavigation: DevValidationInputs | null,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): Promise<PrepareValidationInputsResult> { ): Promise<PrepareValidationInputsResult> {
const loaderTree = ctx.componentMod.routeModule.userland.loaderTree const loaderTree = ctx.componentMod.routeModule.userland.loaderTree
const needsInstantValidation = const needsInstantValidation =
...@@ -4701,7 +4701,7 @@ async function prepareValidationInputsInPartialPrefetching( ...@@ -4701,7 +4701,7 @@ async function prepareValidationInputsInPartialPrefetching(
onError, onError,
prerenderResumeDataCache, prerenderResumeDataCache,
shouldRenderWithAppShell, shouldRenderWithAppShell,
abortSignal requestAbortSignal
) )
if (forwardErrorsFromWarmRender(inputs, ctx)) { if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT return VALIDATION_BAILOUT
...@@ -4716,7 +4716,7 @@ async function prepareValidationInputsInPartialPrefetching( ...@@ -4716,7 +4716,7 @@ async function prepareValidationInputsInPartialPrefetching(
getPayload, getPayload,
onError, onError,
prerenderResumeDataCache, prerenderResumeDataCache,
abortSignal requestAbortSignal
) )
if (forwardErrorsFromWarmRender(inputs, ctx)) { if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT return VALIDATION_BAILOUT
...@@ -4770,7 +4770,7 @@ async function prepareValidationInputsInLegacyPrefetching( ...@@ -4770,7 +4770,7 @@ async function prepareValidationInputsInLegacyPrefetching(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void, onError: (error: unknown) => void,
inputsFromNavigation: DevValidationInputs | null, inputsFromNavigation: DevValidationInputs | null,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): Promise<PrepareValidationInputsResult> { ): Promise<PrepareValidationInputsResult> {
const loaderTree = ctx.componentMod.routeModule.userland.loaderTree const loaderTree = ctx.componentMod.routeModule.userland.loaderTree
const needsInstantValidation = const needsInstantValidation =
...@@ -4794,7 +4794,7 @@ async function prepareValidationInputsInLegacyPrefetching( ...@@ -4794,7 +4794,7 @@ async function prepareValidationInputsInLegacyPrefetching(
onError, onError,
prerenderResumeDataCache, prerenderResumeDataCache,
shouldRenderWithAppShell, shouldRenderWithAppShell,
abortSignal requestAbortSignal
) )
if (forwardErrorsFromWarmRender(inputs, ctx)) { if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT return VALIDATION_BAILOUT
...@@ -4809,7 +4809,7 @@ async function prepareValidationInputsInLegacyPrefetching( ...@@ -4809,7 +4809,7 @@ async function prepareValidationInputsInLegacyPrefetching(
getPayload, getPayload,
onError, onError,
prerenderResumeDataCache, prerenderResumeDataCache,
abortSignal requestAbortSignal
) )
if (forwardErrorsFromWarmRender(inputs, ctx)) { if (forwardErrorsFromWarmRender(inputs, ctx)) {
return VALIDATION_BAILOUT return VALIDATION_BAILOUT
...@@ -4988,7 +4988,7 @@ interface StagedDevRenderOptions { ...@@ -4988,7 +4988,7 @@ interface StagedDevRenderOptions {
// When this aborts, the staged dev render is torn down (its in-flight `'use // When this aborts, the staged dev render is torn down (its in-flight `'use
// cache'` fills keep running) and the detached validation is skipped. // cache'` fills keep running) and the detached validation is skipped.
// `undefined` runs both to completion. // `undefined` runs both to completion.
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
} }
type DevNavigationKind = type DevNavigationKind =
...@@ -5040,7 +5040,7 @@ async function streamStagedRenderInDev({ ...@@ -5040,7 +5040,7 @@ async function streamStagedRenderInDev({
onError, onError,
debugChannel, debugChannel,
navigationKind, navigationKind,
abortSignal, requestAbortSignal,
}: StreamStagedRenderInDevOptions): Promise<{ }: StreamStagedRenderInDevOptions): Promise<{
stream: Readable stream: Readable
resultPromise: Promise<StagedDevRenderResult> resultPromise: Promise<StagedDevRenderResult>
...@@ -5149,15 +5149,6 @@ async function streamStagedRenderInDev({ ...@@ -5149,15 +5149,6 @@ async function streamStagedRenderInDev({
} }
} }
// The render runs to completion unless `abortSignal` fires, in which case
// React aborts the Flight render and the accumulation tears down. The first
// task starts the render in the `ShellEarlyStatic` stage and creates the
// stream (one replay for the response, one to accumulate the chunks). The
// later tasks advance the stages, settle `hadCacheMiss`, and reveal the shell
// – as soon as a cache miss is seen, or once the render reaches
// `revealAfterStage` – then advance into the dynamic stage a task later. The
// replayable stays local: the response is the only reader outside this
// function.
const stagesAdvanced = runInSequentialTasks( const stagesAdvanced = runInSequentialTasks(
() => { () => {
stageController.advanceStage(RenderStage.ShellEarlyStatic) stageController.advanceStage(RenderStage.ShellEarlyStatic)
...@@ -5176,7 +5167,7 @@ async function streamStagedRenderInDev({ ...@@ -5176,7 +5167,7 @@ async function streamStagedRenderInDev({
startTime, startTime,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
signal: abortSignal, signal: requestAbortSignal,
} }
) as Readable ) as Readable
) )
...@@ -5186,10 +5177,9 @@ async function streamStagedRenderInDev({ ...@@ -5186,10 +5177,9 @@ async function streamStagedRenderInDev({
accumulatedChunksPromise: accumulateStreamChunks( accumulatedChunksPromise: accumulateStreamChunks(
replayable.createReplayStream(), replayable.createReplayStream(),
stageController, stageController,
// Tear down the chunk accumulation on abort too, so `resultPromise` // Abort accumulation as soon as the signal aborts instead of waiting
// settles promptly (instead of waiting out the aborted render) and // for the stream to close.
// the detached validation can see the abort and skip. requestAbortSignal ?? null
abortSignal ?? null
), ),
}) })
}, },
...@@ -5279,7 +5269,7 @@ async function renderWithWarmCachesForValidationInDev( ...@@ -5279,7 +5269,7 @@ async function renderWithWarmCachesForValidationInDev(
onError: (error: unknown) => void, onError: (error: unknown) => void,
prerenderResumeDataCache: ReturnType<typeof createPrerenderResumeDataCache>, prerenderResumeDataCache: ReturnType<typeof createPrerenderResumeDataCache>,
shouldRenderWithAppShell: boolean, shouldRenderWithAppShell: boolean,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): Promise<DevValidationInputs> { ): Promise<DevValidationInputs> {
const { ComponentMod, setReactDebugChannel } = ctx.renderOpts const { ComponentMod, setReactDebugChannel } = ctx.renderOpts
const { clientModules } = getClientReferenceManifest() const { clientModules } = getClientReferenceManifest()
...@@ -5329,14 +5319,14 @@ async function renderWithWarmCachesForValidationInDev( ...@@ -5329,14 +5319,14 @@ async function renderWithWarmCachesForValidationInDev(
startTime, startTime,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
signal: abortSignal, signal: requestAbortSignal,
} }
) as Readable ) as Readable
return accumulateStreamChunks( return accumulateStreamChunks(
sourceStream, sourceStream,
stageController, stageController,
abortSignal ?? null requestAbortSignal ?? null
) )
}, },
() => stageController.advanceStage(RenderStage.ShellStatic), () => stageController.advanceStage(RenderStage.ShellStatic),
...@@ -5374,7 +5364,7 @@ async function prerenderWithWarmCachesForStaticValidationInDev( ...@@ -5374,7 +5364,7 @@ async function prerenderWithWarmCachesForStaticValidationInDev(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void, onError: (error: unknown) => void,
prerenderResumeDataCache: ReturnType<typeof createPrerenderResumeDataCache>, prerenderResumeDataCache: ReturnType<typeof createPrerenderResumeDataCache>,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): Promise<DevValidationInputs> { ): Promise<DevValidationInputs> {
const { ComponentMod, setReactDebugChannel } = ctx.renderOpts const { ComponentMod, setReactDebugChannel } = ctx.renderOpts
const { clientModules } = getClientReferenceManifest() const { clientModules } = getClientReferenceManifest()
...@@ -5385,10 +5375,8 @@ async function prerenderWithWarmCachesForStaticValidationInDev( ...@@ -5385,10 +5375,8 @@ async function prerenderWithWarmCachesForStaticValidationInDev(
const finalReactController = new AbortController() const finalReactController = new AbortController()
const finalDataController = new AbortController() const finalDataController = new AbortController()
// Aborting `finalReactController` (the prerender's own controller) on the if (requestAbortSignal) {
// signal tears the render down the same way its runtime-stage abort does. abortWhenSignalAborts(requestAbortSignal, finalReactController)
if (abortSignal) {
abortWhenSignalAborts(abortSignal, finalReactController)
} }
const stageController = new StagedRenderingController({ const stageController = new StagedRenderingController({
...@@ -5551,7 +5539,7 @@ async function stagedRenderWithCachesInDev({ ...@@ -5551,7 +5539,7 @@ async function stagedRenderWithCachesInDev({
fallbackRouteParams, fallbackRouteParams,
getDevRenderDidError, getDevRenderDidError,
navigationKind, navigationKind,
abortSignal, requestAbortSignal,
}: StagedRenderWithCachesInDevOptions): Promise<{ }: StagedRenderWithCachesInDevOptions): Promise<{
stream: Readable stream: Readable
debugChannel: NodeDebugChannelPair | undefined debugChannel: NodeDebugChannelPair | undefined
...@@ -5590,7 +5578,7 @@ async function stagedRenderWithCachesInDev({ ...@@ -5590,7 +5578,7 @@ async function stagedRenderWithCachesInDev({
onError, onError,
debugChannel, debugChannel,
navigationKind, navigationKind,
abortSignal, requestAbortSignal,
}) })
if (shouldValidate) { if (shouldValidate) {
...@@ -5608,7 +5596,7 @@ async function stagedRenderWithCachesInDev({ ...@@ -5608,7 +5596,7 @@ async function stagedRenderWithCachesInDev({
createRequestStore, createRequestStore,
getPayload, getPayload,
onError, onError,
abortSignal requestAbortSignal
) )
} else { } else {
logValidationSkipped(ctx) logValidationSkipped(ctx)
...@@ -6060,7 +6048,7 @@ async function runValidationInDevImpl( ...@@ -6060,7 +6048,7 @@ async function runValidationInDevImpl(
ctx: AppRenderContext, ctx: AppRenderContext,
fallbackRouteParams: OpaqueFallbackRouteParams | null, fallbackRouteParams: OpaqueFallbackRouteParams | null,
devRenderDidError: boolean, devRenderDidError: boolean,
abortSignal: AbortSignal | undefined requestAbortSignal: AbortSignal | undefined
): Promise<void> { ): Promise<void> {
const { componentMod: ComponentMod, getDynamicParamFromSegment } = ctx const { componentMod: ComponentMod, getDynamicParamFromSegment } = ctx
const loaderTree = ComponentMod.routeModule.userland.loaderTree const loaderTree = ComponentMod.routeModule.userland.loaderTree
...@@ -6121,7 +6109,7 @@ async function runValidationInDevImpl( ...@@ -6121,7 +6109,7 @@ async function runValidationInDevImpl(
//================================ //================================
{ {
// The request may have been aborted during the client module warmup above. // The request may have been aborted during the client module warmup above.
if (abortSignal?.aborted) { if (requestAbortSignal?.aborted) {
return return
} }
...@@ -6143,7 +6131,7 @@ async function runValidationInDevImpl( ...@@ -6143,7 +6131,7 @@ async function runValidationInDevImpl(
// The request was aborted while this validation render ran, so its result // The request was aborted while this validation render ran, so its result
// is being discarded. Don't surface its validation errors: they're for a // is being discarded. Don't surface its validation errors: they're for a
// render that won't be shown. // render that won't be shown.
if (abortSignal?.aborted) { if (requestAbortSignal?.aborted) {
return return
} }
if (result.length > 0) { if (result.length > 0) {
...@@ -6177,7 +6165,7 @@ async function runValidationInDevImpl( ...@@ -6177,7 +6165,7 @@ async function runValidationInDevImpl(
// The request was aborted while this render ran; don't surface its stale // The request was aborted while this render ran; don't surface its stale
// validation errors: they're for a render that won't be shown. // validation errors: they're for a render that won't be shown.
if (abortSignal?.aborted) { if (requestAbortSignal?.aborted) {
return return
} }
if (result.length > 0) { if (result.length > 0) {
......
...@@ -551,8 +551,9 @@ export function renderToNodeFlightStream( ...@@ -551,8 +551,9 @@ export function renderToNodeFlightStream(
// `renderToPipeableStream` has no `signal` option (unlike the Web // `renderToPipeableStream` has no `signal` option (unlike the Web
// `renderToReadableStream`), so pull `signal` out of the options and abort // `renderToReadableStream`), so pull `signal` out of the options and abort
// the returned pipeable ourselves when it fires. The listener is removed once // the returned pipeable ourselves when it fires. We drop the listener when
// the passthrough closes. // the passthrough closes so a finished render's `pipeable` isn't retained by
// the request signal, which can outlive it.
const { signal, ...renderOptions } = opts ?? {} const { signal, ...renderOptions } = opts ?? {}
const pt = new PassThrough() const pt = new PassThrough()
......
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