Unverified Commit e5e0d356 authored by Josh Story's avatar Josh Story Committed by GitHub

Use StageController for runtime prerendering (#89972)

Stacked on #89971

This controller was introduced to help with dev time validation. It
models stage transitions in a helpful way that will become more useful
soon as we get more advanced with what kinds of sync IO are considered
errors.

To prepare for this we will update the implementation to use the stage
controller for build and runtime prerendering not just dev

We don't yet use it for static prerendering because that one is so
simple and there is really no stage beyond the first one and then an
abort so we will keep it as is

Additionally, the private cache store no longer has the
`runtimeStagePromise`. when you first enter this store type during a
runtime prerender we wait until the outer store's runtime stage has
begun. Previously the proimse signifying this stage transition was
included in the private cache store itself so nested caches could
similarly wait, however you can't actually get to a nested cache until
after it is resolved b/c the outermost one blocks.
parent 960437cf
...@@ -222,7 +222,6 @@ import { ...@@ -222,7 +222,6 @@ import {
} from '../../shared/lib/router/utils/get-dynamic-param' } from '../../shared/lib/router/utils/get-dynamic-param'
import type { ExperimentalConfig } from '../config-shared' import type { ExperimentalConfig } from '../config-shared'
import type { Params } from '../request/params' import type { Params } from '../request/params'
import { createPromiseWithResolvers } from '../../shared/lib/promise-with-resolvers'
import { ImageConfigContext } from '../../shared/lib/image-config-context.shared-runtime' import { ImageConfigContext } from '../../shared/lib/image-config-context.shared-runtime'
import { imageConfigDefault } from '../../shared/lib/image-config' import { imageConfigDefault } from '../../shared/lib/image-config'
import { RenderStage, StagedRenderingController } from './staged-rendering' import { RenderStage, StagedRenderingController } from './staged-rendering'
...@@ -1120,8 +1119,8 @@ async function prospectiveRuntimeServerPrerender( ...@@ -1120,8 +1119,8 @@ async function prospectiveRuntimeServerPrerender(
hmrRefreshHash: undefined, hmrRefreshHash: undefined,
// We don't track vary params during initial prerender, only the final one // We don't track vary params during initial prerender, only the final one
varyParamsAccumulator: null, varyParamsAccumulator: null,
// We only need task sequencing in the final prerender. // No stage sequencing needed for prospective renders.
runtimeStagePromise: null, stagedRendering: null,
// These are not present in regular prerenders, but allowed in a runtime prerender. // These are not present in regular prerenders, but allowed in a runtime prerender.
headers, headers,
cookies, cookies,
...@@ -1344,8 +1343,10 @@ async function finalRuntimeServerPrerender( ...@@ -1344,8 +1343,10 @@ async function finalRuntimeServerPrerender(
isDebugDynamicAccesses isDebugDynamicAccesses
) )
const { promise: runtimeStagePromise, resolve: resolveBlockedRuntimeAPIs } = const finalStageController = new StagedRenderingController(
createPromiseWithResolvers<void>() finalServerController.signal,
true
)
const finalServerPrerenderStore: PrerenderStoreModernRuntime = { const finalServerPrerenderStore: PrerenderStoreModernRuntime = {
type: 'prerender-runtime', type: 'prerender-runtime',
...@@ -1369,7 +1370,7 @@ async function finalRuntimeServerPrerender( ...@@ -1369,7 +1370,7 @@ async function finalRuntimeServerPrerender(
// TODO: Enable vary params tracking for runtime prefetches. // TODO: Enable vary params tracking for runtime prefetches.
varyParamsAccumulator: null, varyParamsAccumulator: null,
// Used to separate the "Static" stage from the "Runtime" stage. // Used to separate the "Static" stage from the "Runtime" stage.
runtimeStagePromise, stagedRendering: finalStageController,
// These are not present in regular prerenders, but allowed in a runtime prerender. // These are not present in regular prerenders, but allowed in a runtime prerender.
headers, headers,
cookies, cookies,
...@@ -1409,7 +1410,7 @@ async function finalRuntimeServerPrerender( ...@@ -1409,7 +1410,7 @@ async function finalRuntimeServerPrerender(
// We know that they don't contain any incorrect sync IO, because that'd have caused a build error. // We know that they don't contain any incorrect sync IO, because that'd have caused a build error.
// After we unblock Runtime APIs, if we encounter sync IO (e.g. `await cookies(); Date.now()`), // After we unblock Runtime APIs, if we encounter sync IO (e.g. `await cookies(); Date.now()`),
// we'll abort, but we'll produce at least as much output as a static prerender would. // we'll abort, but we'll produce at least as much output as a static prerender would.
resolveBlockedRuntimeAPIs() finalStageController.advanceStage(RenderStage.Runtime)
}, },
() => { () => {
// Abort. // Abort.
......
...@@ -34,10 +34,10 @@ import React from 'react' ...@@ -34,10 +34,10 @@ import React from 'react'
import { DynamicServerError } from '../../client/components/hooks-server-context' import { DynamicServerError } from '../../client/components/hooks-server-context'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { import {
getRuntimeStagePromise,
throwForMissingRequestStore, throwForMissingRequestStore,
workUnitAsyncStorage, workUnitAsyncStorage,
} from './work-unit-async-storage.external' } from './work-unit-async-storage.external'
import { RenderStage } from './staged-rendering'
import { workAsyncStorage } from '../app-render/work-async-storage.external' import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
import { import {
...@@ -539,11 +539,14 @@ export function createHangingInputAbortSignal( ...@@ -539,11 +539,14 @@ export function createHangingInputAbortSignal(
// render the content of this cache as deeply as we can so that we can // render the content of this cache as deeply as we can so that we can
// suspend as deeply as possible in the tree or not at all if we don't // suspend as deeply as possible in the tree or not at all if we don't
// end up waiting for the input. // end up waiting for the input.
const runtimeStagePromise = getRuntimeStagePromise(workUnitStore) if (
if (runtimeStagePromise) { // eslint-disable-next-line no-restricted-syntax -- We are discriminating between two different refined types and don't need an addition exhaustive switch here
runtimeStagePromise.then(() => workUnitStore.type === 'prerender-runtime' &&
scheduleOnNextTick(() => controller.abort()) workUnitStore.stagedRendering
) ) {
workUnitStore.stagedRendering
.waitForStage(RenderStage.Runtime)
.then(() => scheduleOnNextTick(() => controller.abort()))
} else { } else {
scheduleOnNextTick(() => controller.abort()) scheduleOnNextTick(() => controller.abort())
} }
...@@ -1152,8 +1155,10 @@ export function delayUntilRuntimeStage<T>( ...@@ -1152,8 +1155,10 @@ export function delayUntilRuntimeStage<T>(
prerenderStore: PrerenderStoreModernRuntime, prerenderStore: PrerenderStoreModernRuntime,
result: Promise<T> result: Promise<T>
): Promise<T> { ): Promise<T> {
if (prerenderStore.runtimeStagePromise) { if (prerenderStore.stagedRendering) {
return prerenderStore.runtimeStagePromise.then(() => result) return prerenderStore.stagedRendering
.waitForStage(RenderStage.Runtime)
.then(() => result)
} }
return result return result
} }
...@@ -98,7 +98,7 @@ export class StagedRenderingController { ...@@ -98,7 +98,7 @@ export class StagedRenderingController {
} }
// If Sync IO occurs during an abandonable render, we trigger the abandon. // If Sync IO occurs during an abandonable render, we trigger the abandon.
// The abandon listener will call abandonRenderImpl which advances through // The abandon listener will call abandonRender which advances through
// stages to let caches fill before marking as Abandoned. // stages to let caches fill before marking as Abandoned.
if (this.abandonController) { if (this.abandonController) {
this.abandonController.abort() this.abandonController.abort()
......
...@@ -125,16 +125,11 @@ export interface PrerenderStoreModernRuntime ...@@ -125,16 +125,11 @@ export interface PrerenderStoreModernRuntime
readonly type: 'prerender-runtime' readonly type: 'prerender-runtime'
/** /**
* A runtime prerender resolves APIs in two tasks: * The staged rendering controller for this prerender. Models stage
* * transitions (Before → Static → Runtime → Dynamic). Null for prospective
* 1. Static data (available in a static prerender) * renders where all stages run without sequencing.
* 2. Runtime data (available in a runtime prerender)
*
* This separation is achieved by awaiting this promise in "runtime" APIs.
* In the final prerender, the promise will be resolved during the second task,
* and the render will be aborted in the task that follows it.
*/ */
readonly runtimeStagePromise: Promise<void> | null readonly stagedRendering: StagedRenderingController | null
readonly headers: RequestStore['headers'] readonly headers: RequestStore['headers']
readonly cookies: RequestStore['cookies'] readonly cookies: RequestStore['cookies']
...@@ -301,18 +296,6 @@ export interface PublicUseCacheStore extends CommonUseCacheStore { ...@@ -301,18 +296,6 @@ export interface PublicUseCacheStore extends CommonUseCacheStore {
export interface PrivateUseCacheStore extends CommonUseCacheStore { export interface PrivateUseCacheStore extends CommonUseCacheStore {
readonly type: 'private-cache' readonly type: 'private-cache'
/**
* A runtime prerender resolves APIs in two tasks:
*
* 1. Static data (available in a static prerender)
* 2. Runtime data (available in a runtime prerender)
*
* This separation is achieved by awaiting this promise in "runtime" APIs.
* In the final prerender, the promise will be resolved during the second task,
* and the render will be aborted in the task that follows it.
*/
readonly runtimeStagePromise: Promise<void> | null
readonly headers: ReadonlyHeaders readonly headers: ReadonlyHeaders
readonly cookies: ReadonlyRequestCookies readonly cookies: ReadonlyRequestCookies
...@@ -538,23 +521,3 @@ export function getCacheSignal( ...@@ -538,23 +521,3 @@ export function getCacheSignal(
return workUnitStore satisfies never return workUnitStore satisfies never
} }
} }
export function getRuntimeStagePromise(
workUnitStore: WorkUnitStore
): Promise<void> | null {
switch (workUnitStore.type) {
case 'prerender-runtime':
case 'private-cache':
return workUnitStore.runtimeStagePromise
case 'prerender':
case 'prerender-client':
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'unstable-cache':
return null
default:
return workUnitStore satisfies never
}
}
...@@ -34,7 +34,6 @@ import { ...@@ -34,7 +34,6 @@ import {
getCacheSignal, getCacheSignal,
isHmrRefresh, isHmrRefresh,
getServerComponentsHmrCache, getServerComponentsHmrCache,
getRuntimeStagePromise,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { import {
...@@ -221,7 +220,6 @@ function createUseCacheStore( ...@@ -221,7 +220,6 @@ function createUseCacheStore(
isHmrRefresh: isHmrRefresh(outerWorkUnitStore), isHmrRefresh: isHmrRefresh(outerWorkUnitStore),
serverComponentsHmrCache: getServerComponentsHmrCache(outerWorkUnitStore), serverComponentsHmrCache: getServerComponentsHmrCache(outerWorkUnitStore),
forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore), forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore),
runtimeStagePromise: getRuntimeStagePromise(outerWorkUnitStore),
draftMode: getDraftModeProviderForCacheScope( draftMode: getDraftModeProviderForCacheScope(
workStore, workStore,
outerWorkUnitStore outerWorkUnitStore
...@@ -1055,8 +1053,9 @@ export async function cache( ...@@ -1055,8 +1053,9 @@ export async function cache(
case 'prerender-runtime': { case 'prerender-runtime': {
// In a runtime prerender, we have to make sure that APIs that would hang during a static prerender // In a runtime prerender, we have to make sure that APIs that would hang during a static prerender
// are resolved with a delay, in the runtime stage. Private caches are one of these. // are resolved with a delay, in the runtime stage. Private caches are one of these.
if (outerWorkUnitStore.runtimeStagePromise) { const stagedRendering = outerWorkUnitStore.stagedRendering
await outerWorkUnitStore.runtimeStagePromise if (stagedRendering) {
await stagedRendering.waitForStage(RenderStage.Runtime)
} }
break break
} }
...@@ -1365,8 +1364,9 @@ export async function cache( ...@@ -1365,8 +1364,9 @@ export async function cache(
// In the final phase of a runtime prerender, we have to make // In the final phase of a runtime prerender, we have to make
// sure that APIs that would hang during a static prerender // sure that APIs that would hang during a static prerender
// are resolved with a delay, in the runtime stage. // are resolved with a delay, in the runtime stage.
if (workUnitStore.runtimeStagePromise) { const stagedRendering = workUnitStore.stagedRendering
await workUnitStore.runtimeStagePromise if (stagedRendering) {
await stagedRendering.waitForStage(RenderStage.Runtime)
} }
break break
} }
......
...@@ -2570,13 +2570,13 @@ describe('Cache Components Errors', () => { ...@@ -2570,13 +2570,13 @@ describe('Cache Components Errors', () => {
expect(output).toMatchInlineSnapshot(` expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-low-expire/fast": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route "Error: Route "/use-cache-low-expire/fast": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
at cache (webpack:///<next-src>) at cache (webpack:///<next-src>)
1357 | cacheSignal.endRead() 1356 | cacheSignal.endRead()
1358 | } 1357 | }
> 1359 | return makeHangingPromise( > 1358 | return makeHangingPromise(
| ^ | ^
1360 | workUnitStore.renderSignal, 1359 | workUnitStore.renderSignal,
1361 | workStore.route, 1360 | workStore.route,
1362 | 'dynamic "use cache"' 1361 | 'dynamic "use cache"'
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-low-expire/fast" in your browser to investigate the error. To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-low-expire/fast" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-low-expire/fast". Read more: https://nextjs.org/docs/messages/prerender-error Error occurred prerendering page "/use-cache-low-expire/fast". Read more: https://nextjs.org/docs/messages/prerender-error
...@@ -2714,13 +2714,13 @@ describe('Cache Components Errors', () => { ...@@ -2714,13 +2714,13 @@ describe('Cache Components Errors', () => {
expect(output).toMatchInlineSnapshot(` expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-low-expire/slow": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route "Error: Route "/use-cache-low-expire/slow": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
at cache (webpack:///<next-src>) at cache (webpack:///<next-src>)
1357 | cacheSignal.endRead() 1356 | cacheSignal.endRead()
1358 | } 1357 | }
> 1359 | return makeHangingPromise( > 1358 | return makeHangingPromise(
| ^ | ^
1360 | workUnitStore.renderSignal, 1359 | workUnitStore.renderSignal,
1361 | workStore.route, 1360 | workStore.route,
1362 | 'dynamic "use cache"' 1361 | 'dynamic "use cache"'
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-low-expire/slow" in your browser to investigate the error. To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-low-expire/slow" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-low-expire/slow". Read more: https://nextjs.org/docs/messages/prerender-error Error occurred prerendering page "/use-cache-low-expire/slow". Read more: https://nextjs.org/docs/messages/prerender-error
...@@ -2962,13 +2962,13 @@ describe('Cache Components Errors', () => { ...@@ -2962,13 +2962,13 @@ describe('Cache Components Errors', () => {
expect(output).toMatchInlineSnapshot(` expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-revalidate-0/fast": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route "Error: Route "/use-cache-revalidate-0/fast": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
at cache (webpack:///<next-src>) at cache (webpack:///<next-src>)
1357 | cacheSignal.endRead() 1356 | cacheSignal.endRead()
1358 | } 1357 | }
> 1359 | return makeHangingPromise( > 1358 | return makeHangingPromise(
| ^ | ^
1360 | workUnitStore.renderSignal, 1359 | workUnitStore.renderSignal,
1361 | workStore.route, 1360 | workStore.route,
1362 | 'dynamic "use cache"' 1361 | 'dynamic "use cache"'
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-revalidate-0/fast" in your browser to investigate the error. To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-revalidate-0/fast" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-revalidate-0/fast". Read more: https://nextjs.org/docs/messages/prerender-error Error occurred prerendering page "/use-cache-revalidate-0/fast". Read more: https://nextjs.org/docs/messages/prerender-error
...@@ -3106,13 +3106,13 @@ describe('Cache Components Errors', () => { ...@@ -3106,13 +3106,13 @@ describe('Cache Components Errors', () => {
expect(output).toMatchInlineSnapshot(` expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-revalidate-0/slow": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route "Error: Route "/use-cache-revalidate-0/slow": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
at cache (webpack:///<next-src>) at cache (webpack:///<next-src>)
1357 | cacheSignal.endRead() 1356 | cacheSignal.endRead()
1358 | } 1357 | }
> 1359 | return makeHangingPromise( > 1358 | return makeHangingPromise(
| ^ | ^
1360 | workUnitStore.renderSignal, 1359 | workUnitStore.renderSignal,
1361 | workStore.route, 1360 | workStore.route,
1362 | 'dynamic "use cache"' 1361 | 'dynamic "use cache"'
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-revalidate-0/slow" in your browser to investigate the error. To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-revalidate-0/slow" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-revalidate-0/slow". Read more: https://nextjs.org/docs/messages/prerender-error Error occurred prerendering page "/use-cache-revalidate-0/slow". Read more: https://nextjs.org/docs/messages/prerender-error
...@@ -3896,13 +3896,13 @@ describe('Cache Components Errors', () => { ...@@ -3896,13 +3896,13 @@ describe('Cache Components Errors', () => {
at cache (webpack:///<next-src>) at cache (webpack:///<next-src>)
at Private (webpack:///app/use-cache-private-without-suspense/page.tsx:15:1) at Private (webpack:///app/use-cache-private-without-suspense/page.tsx:15:1)
at Page (webpack:///app/use-cache-private-without-suspense/page.tsx:10:7) at Page (webpack:///app/use-cache-private-without-suspense/page.tsx:10:7)
937 | // "use cache: private" is dynamic in prerendering contexts. 935 | // "use cache: private" is dynamic in prerendering contexts.
938 | case 'prerender': 936 | case 'prerender':
> 939 | return makeHangingPromise( > 937 | return makeHangingPromise(
| ^ | ^
940 | workUnitStore.renderSignal, 938 | workUnitStore.renderSignal,
941 | workStore.route, 939 | workStore.route,
942 | expression 940 | expression
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-private-without-suspense" in your browser to investigate the error. To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-private-without-suspense" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-private-without-suspense". Read more: https://nextjs.org/docs/messages/prerender-error Error occurred prerendering page "/use-cache-private-without-suspense". Read more: https://nextjs.org/docs/messages/prerender-error
......
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