Unverified Commit 3c995a87 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

refactor: remove unnecessary switches from StagedRenderingController (#95383)

We have a couple switches in `StagedRenderingController` where the main
purpose ends up being eliminating `Before/Dynamic/Abandoned` (i.e. the
start and the end stages). Since we migrated everything to
`RENDER_STAGE_ORDER` loops, we no longer really need to list all the
intermediate stages out explicitly in these switches. It's enough to
rule out the "special" stages and let the rest of the code be generic.
This saves some work when adding/removing intermediate stages.

View with "hide whitespace", otherwise it looks messy.
parent 3bb780e7
...@@ -1417,5 +1417,6 @@ ...@@ -1417,5 +1417,6 @@
"1416": "Invalid \"%s\" provided, expected a finite number of seconds or Infinity, received %s", "1416": "Invalid \"%s\" provided, expected a finite number of seconds or Infinity, received %s",
"1417": "Could not parse output from TypeScript's --showConfig.", "1417": "Could not parse output from TypeScript's --showConfig.",
"1418": "TypeScript %s does not provide the compiler API required by Next.js. Enable %s in your Next.js config to use the TypeScript CLI, or install TypeScript 6 instead.", "1418": "TypeScript %s does not provide the compiler API required by Next.js. Enable %s in your Next.js config to use the TypeScript CLI, or install TypeScript 6 instead.",
"1419": "TypeScript CLI interrupted by %s" "1419": "TypeScript CLI interrupted by %s",
"1420": "shouldTrackSyncInterrupt allowed a sync IO interrupt in an unexpected stage: %s"
} }
...@@ -184,13 +184,15 @@ export class StagedRenderingController { ...@@ -184,13 +184,15 @@ export class StagedRenderingController {
} }
} }
/** Note: only call this if `shouldTrackSyncInterrupt()` returned true */
syncInterruptCurrentStageWithReason(reason: Error) { syncInterruptCurrentStageWithReason(reason: Error) {
if (this.currentStage === RenderStage.Before) { const { currentStage } = this
return if (
} currentStage === RenderStage.Before ||
currentStage === RenderStage.Dynamic ||
// If the render has already been abandoned, there's nothing to interrupt. currentStage === RenderStage.Abandoned
if (this.currentStage === RenderStage.Abandoned) { ) {
// Not interruptible. Defensive noop.
return return
} }
...@@ -214,35 +216,8 @@ export class StagedRenderingController { ...@@ -214,35 +216,8 @@ export class StagedRenderingController {
// If we're in a non-abandonable & non-abortable render, // If we're in a non-abandonable & non-abortable render,
// we need to advance to the Dynamic stage and capture the interruption reason. // we need to advance to the Dynamic stage and capture the interruption reason.
// (in dev, this will be the restarted render) // (in dev, this will be the restarted render)
switch (this.currentStage) { this.syncInterruptReason = reason
case RenderStage.ShellEarlyStatic: this.advanceStage(RenderStage.Dynamic)
case RenderStage.ShellStatic:
case RenderStage.EarlyStatic:
case RenderStage.Static:
case RenderStage.ShellEarlyRuntime:
case RenderStage.EarlyRuntime: {
// EarlyRuntime is for runtime-prefetchable segments. Sync IO here
// means the prefetch would be aborted too early.
this.syncInterruptReason = reason
this.advanceStage(RenderStage.Dynamic)
return
}
case RenderStage.ShellRuntime:
case RenderStage.Runtime: {
// `shouldTrackSyncInterrupt` returns false for [Shell]Runtime, so we should
// never get here. Defensive no-op.
break
}
case RenderStage.Dynamic: {
// `shouldTrackSyncInterrupt` returns false for Dynamic, so we should
// never get here. Defensive no-op.
break
}
default: {
this.currentStage satisfies never
}
}
} }
getSyncInterruptReason() { getSyncInterruptReason() {
...@@ -275,40 +250,30 @@ export class StagedRenderingController { ...@@ -275,40 +250,30 @@ export class StagedRenderingController {
// unblocking the dynamic stage would likely lead to wasted (uncached) IO. // unblocking the dynamic stage would likely lead to wasted (uncached) IO.
const { currentStage } = this const { currentStage } = this
switch (currentStage) {
case RenderStage.Before: { if (currentStage === RenderStage.Before) {
throw new InvariantError( throw new InvariantError(
"A render that hasn't started yet cannot be abandoned" "A render that hasn't started yet cannot be abandoned"
) )
} }
case RenderStage.ShellEarlyStatic: if (
case RenderStage.EarlyStatic: currentStage === RenderStage.Dynamic ||
case RenderStage.ShellStatic: currentStage === RenderStage.Abandoned
case RenderStage.Static: ) {
case RenderStage.ShellEarlyRuntime: // We shouldn't ever trigger an abandon in these. Defensive noop.
case RenderStage.EarlyRuntime: return
case RenderStage.ShellRuntime: }
case RenderStage.Runtime: {
// Resolve all stages after the current one, up to runtime (excluding dynamic) // Resolve all stages after the current one, up to runtime (excluding dynamic)
const nextStageIx = RENDER_STAGE_ADVANCE_ORDER.indexOf(currentStage) + 1 const nextStageIx = RENDER_STAGE_ADVANCE_ORDER.indexOf(currentStage) + 1
const dynamicStageIx = RENDER_STAGE_ADVANCE_ORDER.indexOf( const dynamicStageIx = RENDER_STAGE_ADVANCE_ORDER.indexOf(
RenderStage.Dynamic RenderStage.Dynamic
) )
for (let i = nextStageIx; i < dynamicStageIx; i++) { for (let i = nextStageIx; i < dynamicStageIx; i++) {
this.resolveStage(RENDER_STAGE_ADVANCE_ORDER[i]) this.resolveStage(RENDER_STAGE_ADVANCE_ORDER[i])
}
this.currentStage = RenderStage.Abandoned
break
}
case RenderStage.Dynamic:
case RenderStage.Abandoned: {
break
}
default: {
currentStage satisfies never
}
} }
this.currentStage = RenderStage.Abandoned
} }
advanceStage(targetStage: AdvanceableRenderStage) { advanceStage(targetStage: AdvanceableRenderStage) {
...@@ -317,43 +282,31 @@ export class StagedRenderingController { ...@@ -317,43 +282,31 @@ export class StagedRenderingController {
`Attempted to advance to stage ${RenderStage[targetStage]} but the render is limited to ${RenderStage[this.finalStage]}` `Attempted to advance to stage ${RenderStage[targetStage]} but the render is limited to ${RenderStage[this.finalStage]}`
) )
} }
const { currentStage } = this
if (
currentStage === RenderStage.Dynamic ||
currentStage === RenderStage.Abandoned
) {
// Terminal stages, nowhere left to advance.
return
}
// If we're already at the target stage or beyond, do nothing. // If we're already at the target stage or beyond, do nothing.
// (this can happen e.g. if sync IO advanced us to the dynamic stage) if (targetStage <= currentStage) {
if (targetStage <= this.currentStage) {
return return
} }
const { currentStage } = this
this.currentStage = targetStage this.currentStage = targetStage
switch (currentStage) { // Resolve all stages between the current stage and the target.
case RenderStage.Before: const nextStageIx =
case RenderStage.ShellEarlyStatic: currentStage === RenderStage.Before
case RenderStage.EarlyStatic: ? 0
case RenderStage.ShellStatic: : RENDER_STAGE_ADVANCE_ORDER.indexOf(currentStage) + 1
case RenderStage.Static: const targetStageIx = RENDER_STAGE_ADVANCE_ORDER.indexOf(targetStage)
case RenderStage.ShellEarlyRuntime: for (let i = nextStageIx; i <= targetStageIx; i++) {
case RenderStage.EarlyRuntime: this.resolveStage(RENDER_STAGE_ADVANCE_ORDER[i])
case RenderStage.ShellRuntime:
case RenderStage.Runtime: {
// Resolve all stages between the current stage and the target.
const nextStageIx =
currentStage === RenderStage.Before
? 0
: RENDER_STAGE_ADVANCE_ORDER.indexOf(currentStage) + 1
const targetStageIx = RENDER_STAGE_ADVANCE_ORDER.indexOf(targetStage)
for (let i = nextStageIx; i <= targetStageIx; i++) {
this.resolveStage(RENDER_STAGE_ADVANCE_ORDER[i])
}
break
}
case RenderStage.Dynamic:
case RenderStage.Abandoned: {
break
}
default: {
currentStage satisfies never
}
} }
} }
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
createSyncIORuntimeError, createSyncIORuntimeError,
type SyncIOApiType, type SyncIOApiType,
} from '../app-render/sync-io-messages' } from '../app-render/sync-io-messages'
import { InvariantError } from '../../shared/lib/invariant-error'
export function io(expression: string, type: SyncIOApiType) { export function io(expression: string, type: SyncIOApiType) {
const workUnitStore = workUnitAsyncStorage.getStore() const workUnitStore = workUnitAsyncStorage.getStore()
...@@ -56,21 +57,36 @@ export function io(expression: string, type: SyncIOApiType) { ...@@ -56,21 +57,36 @@ export function io(expression: string, type: SyncIOApiType) {
const stageController = workUnitStore.stagedRendering const stageController = workUnitStore.stagedRendering
if (stageController && stageController.shouldTrackSyncInterrupt()) { if (stageController && stageController.shouldTrackSyncInterrupt()) {
let syncIOError: Error let syncIOError: Error
if ( // NOTE: keep stages where we can interrupt in sync with
stageController.currentStage === RenderStage.Static || // `shouldTrackSyncInterrupt`/`syncInterruptCurrentStageWithReason`
stageController.currentStage === RenderStage.EarlyStatic switch (stageController.currentStage) {
) { case RenderStage.ShellEarlyStatic:
syncIOError = createSyncIOError(workStore.route, expression, type) case RenderStage.ShellStatic:
} else { case RenderStage.EarlyStatic:
// We're in the Runtime stage. case RenderStage.Static: {
// We only error for Sync IO in the Runtime stage if the route has a runtime prefetch config. syncIOError = createSyncIOError(workStore.route, expression, type)
// This check is implemented in `stageController.canSyncInterrupt()` -- break
// if runtime prefetching isn't enabled, then we won't get here. }
syncIOError = createSyncIORuntimeError( case RenderStage.ShellEarlyRuntime:
workStore.route, case RenderStage.EarlyRuntime: {
expression, // We only error for Sync IO in the Runtime stage if the segment has a runtime prefetch config.
type // Only Runtime prefetchable segments are rendered in the "early" runtime stages.
) syncIOError = createSyncIORuntimeError(
workStore.route,
expression,
type
)
break
}
case RenderStage.Before:
case RenderStage.ShellRuntime:
case RenderStage.Runtime:
case RenderStage.Dynamic:
case RenderStage.Abandoned: {
throw new InvariantError(
`shouldTrackSyncInterrupt allowed a sync IO interrupt in an unexpected stage: ${RenderStage[stageController.currentStage]}`
)
}
} }
syncIOError = applyOwnerStack(syncIOError) syncIOError = applyOwnerStack(syncIOError)
......
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