Unverified Commit 30e6794b authored by Josh Story's avatar Josh Story Committed by GitHub

Refactor prerender workUnitStores (#70986)

This continues the refactor by adding an explicit type for PPR without
dynamicIO. It gets rid of `isDynamicIOPrerender` helper in favor or just
using a new discriminated type. It narrows the function arguments where
possible. dynamicIO prerenders encompass both PPR and non-PPR since
prerendering semnatics aren't different even when PPR is enabled. It
also sets up passing an AbortSignal into the dynamicIO prerender stores.
We don't use it yet but we will end up relying on it to clean up hanging
promises after the render is complete
parent a2d8421a
......@@ -923,7 +923,9 @@ async function renderToHTMLOrFlightImpl(
const staticGenStore = ComponentMod.workAsyncStorage.getStore()
const prerenderStore = workUnitAsyncStorage.getStore()
const isPPR =
prerenderStore && prerenderStore.type === 'prerender'
prerenderStore &&
(prerenderStore.type === 'prerender' ||
prerenderStore.type === 'prerender-ppr')
? !!prerenderStore.dynamicTracking?.dynamicAccesses?.length
: false
......@@ -1854,10 +1856,13 @@ async function prerenderToStream(
const PRERENDER_COMPLETE = 'NEXT_PRERENDER_COMPLETE'
const abortReason = new Error(PRERENDER_COMPLETE)
let flightController = new AbortController()
const cacheSignal = new CacheSignal()
const prospectiveRenderPrerenderStore: PrerenderStore = {
type: 'prerender',
pathname: ctx.requestStore.url.pathname,
renderSignal: flightController.signal,
cacheSignal,
// During the prospective render we don't want to synchronously abort on dynamic access
// because it could prevent us from discovering all caches in siblings. So we omit the controller
......@@ -1869,8 +1874,6 @@ async function prerenderToStream(
dynamicTracking: null,
}
let flightController = new AbortController()
let reactServerIsDynamic = false
function onError(err: unknown) {
if (err === abortReason) {
......@@ -1950,6 +1953,7 @@ async function prerenderToStream(
const finalRenderPrerenderStore: PrerenderStore = {
type: 'prerender',
pathname: ctx.requestStore.url.pathname,
renderSignal: flightController.signal,
// During the final prerender we don't need to track cache access so we omit the signal
cacheSignal: null,
// During the final render we do want to abort synchronously on dynamic access so we
......@@ -2006,6 +2010,7 @@ async function prerenderToStream(
const ssrPrerenderStore: PrerenderStore = {
type: 'prerender',
pathname: ctx.requestStore.url.pathname,
renderSignal: SSRController.signal,
// For HTML Generation we don't need to track cache reads (RSC only)
cacheSignal: null,
// We expect the SSR render to complete in a single Task and need to be able to synchronously abort
......@@ -2211,6 +2216,7 @@ async function prerenderToStream(
const prospectiveRenderPrerenderStore: PrerenderStore = {
type: 'prerender',
pathname: ctx.requestStore.url.pathname,
renderSignal: flightController.signal,
cacheSignal,
// When PPR is off we can synchronously abort the prospective render because we will
// always hit this path on the final render and thus we can skip the final render and just
......@@ -2290,6 +2296,7 @@ async function prerenderToStream(
const finalRenderPrerenderStore: PrerenderStore = {
type: 'prerender',
pathname: ctx.requestStore.url.pathname,
renderSignal: flightController.signal,
// During the final prerender we don't need to track cache access so we omit the signal
cacheSignal: null,
controller: flightController,
......@@ -2300,6 +2307,7 @@ async function prerenderToStream(
const ssrPrerenderStore: PrerenderStore = {
type: 'prerender',
pathname: ctx.requestStore.url.pathname,
renderSignal: SSRController.signal,
// For HTML Generation we don't need to track cache reads (RSC only)
cacheSignal: null,
// We expect the SSR render to complete in a single Task and need to be able to synchronously abort
......@@ -2479,10 +2487,8 @@ async function prerenderToStream(
renderOpts.isDebugDynamicAccesses
)
const reactServerPrerenderStore: PrerenderStore = {
type: 'prerender',
type: 'prerender-ppr',
pathname: ctx.requestStore.url.pathname,
cacheSignal: null,
controller: null,
dynamicTracking,
}
const RSCPayload = await workUnitAsyncStorage.run(
......@@ -2507,10 +2513,8 @@ async function prerenderToStream(
))
const ssrPrerenderStore: PrerenderStore = {
type: 'prerender',
type: 'prerender-ppr',
pathname: ctx.requestStore.url.pathname,
cacheSignal: null,
controller: null,
dynamicTracking,
}
......@@ -2693,7 +2697,9 @@ async function prerenderToStream(
const renderToReadableStream = require('react-dom/server.edge')
.renderToReadableStream as (typeof import('react-dom/server.edge'))['renderToReadableStream']
const htmlStream = await renderToReadableStream(
const htmlStream = await workUnitAsyncStorage.run(
prerenderLegacyStore,
renderToReadableStream,
<App
reactServerStream={reactServerResult.asUnclosingStream()}
preinitScripts={preinitScripts}
......@@ -2813,9 +2819,21 @@ async function prerenderToStream(
'/_not-found/page'
)
const errorRSCPayload = await getErrorRSCPayload(tree, ctx, errorType)
const prerenderLegacyStore: PrerenderStore = {
type: 'prerender-legacy',
pathname: ctx.requestStore.url.pathname,
}
const errorRSCPayload = await workUnitAsyncStorage.run(
prerenderLegacyStore,
getErrorRSCPayload,
tree,
ctx,
errorType
)
const errorServerStream = ComponentMod.renderToReadableStream(
const errorServerStream = workUnitAsyncStorage.run(
prerenderLegacyStore,
ComponentMod.renderToReadableStream,
errorRSCPayload,
clientReferenceManifest.clientModules,
{
......
......@@ -29,8 +29,8 @@ import React from 'react'
import { DynamicServerError } from '../../client/components/hooks-server-context'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
type PrerenderStoreLegacy,
type PrerenderStoreModern,
} from './work-unit-async-storage.external'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
......@@ -142,9 +142,8 @@ export function markCurrentScopeAsDynamic(
)
}
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore && prerenderStore.type === 'prerender') {
if (isDynamicIOPrerender(prerenderStore)) {
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// We're prerendering the RSC stream with dynamicIO enabled and we need to abort the
// current render because something dynamic is being used.
// This won't throw so we still need to fall through to determine if/how we handle
......@@ -152,19 +151,17 @@ export function markCurrentScopeAsDynamic(
abortAndThrowOnSynchronousDynamicDataAccess(
store.route,
expression,
prerenderStore
workUnitStore
)
} else {
} else if (workUnitStore.type === 'prerender-ppr') {
postponeWithTracking(
store.route,
expression,
prerenderStore.dynamicTracking
workUnitStore.dynamicTracking
)
}
} else {
store.revalidate = 0
} else if (workUnitStore.type === 'prerender-legacy') {
store.revalidate = 0
if (store.isStaticGeneration) {
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Route ${store.route} couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
......@@ -174,7 +171,9 @@ export function markCurrentScopeAsDynamic(
throw err
}
// We fall through in all other cases to just tracking that something dynamic happened on the work store
}
store.revalidate = 0
}
/**
......@@ -190,7 +189,7 @@ export function trackFallbackParamAccessed(
expression: string
): void {
const prerenderStore = workUnitAsyncStorage.getStore()
if (!prerenderStore || prerenderStore.type !== 'prerender') return
if (!prerenderStore || prerenderStore.type !== 'prerender-ppr') return
postponeWithTracking(store.route, expression, prerenderStore.dynamicTracking)
}
......@@ -220,36 +219,32 @@ export function trackDynamicDataAccessed(
)
}
}
if (store.dynamicShouldError) {
throw new StaticGenBailoutError(
`Route ${store.route} with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
)
}
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore && prerenderStore.type === 'prerender') {
if (isDynamicIOPrerender(prerenderStore)) {
// We're prerendering the RSC stream with dynamicIO enabled and we need to abort the
// current render because something dynamic is being used.
// This won't throw so we still need to fall through to determine if/how we handle
// this specific dynamic request.
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// dynamicIO Prerender
abortAndThrowOnSynchronousDynamicDataAccess(
store.route,
expression,
prerenderStore
workUnitStore
)
} else {
} else if (workUnitStore.type === 'prerender-ppr') {
// PPR Prerender
postponeWithTracking(
store.route,
expression,
prerenderStore.dynamicTracking
workUnitStore.dynamicTracking
)
}
} else {
store.revalidate = 0
} else if (workUnitStore.type === 'prerender-legacy') {
// legacy Prerender
store.revalidate = 0
if (store.isStaticGeneration) {
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Route ${store.route} couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
......@@ -258,6 +253,8 @@ export function trackDynamicDataAccessed(
throw err
}
} else {
store.revalidate = 0
}
}
......@@ -270,25 +267,15 @@ export function trackDynamicDataAccessed(
export function throwToInterruptStaticGeneration(
expression: string,
store: WorkStore,
workUnitStore: undefined | WorkUnitStore
// We don't actually use this store but making it part of the function signature enforces
// that it is only called in contexts where we are in fact performing a legacy prerender
_prerenderStore: PrerenderStoreLegacy
): never {
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Route ${store.route} couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
if (workUnitStore) {
if (
workUnitStore.type === 'cache' ||
workUnitStore.type === 'unstable-cache'
) {
// inside cache scopes marking a scope as dynamic has no effect because the outer cache scope
// creates a cache boundary. This is subtly different from reading a dynamic data source which is
// forbidden inside a cache scope.
throw err
}
}
store.revalidate = 0
store.dynamicUsageDescription = expression
......@@ -390,7 +377,7 @@ type PostponeProps = {
export function Postpone({ reason, route }: PostponeProps): never {
const prerenderStore = workUnitAsyncStorage.getStore()
const dynamicTracking =
prerenderStore && prerenderStore.type === 'prerender'
prerenderStore && prerenderStore.type === 'prerender-ppr'
? prerenderStore.dynamicTracking
: null
postponeWithTracking(route, reason, dynamicTracking)
......@@ -576,26 +563,24 @@ export function useDynamicRouteParams(expression: string) {
) {
// There are fallback route params, we should track these as dynamic
// accesses.
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore && prerenderStore.type === 'prerender') {
const workUnitStore = workUnitAsyncStorage.getStore()
if (workUnitStore) {
// We're prerendering with dynamicIO or PPR or both
if (isDynamicIOPrerender(prerenderStore)) {
if (workUnitStore.type === 'prerender') {
// We are in a prerender with dynamicIO semantics
// We are going to hang here and never resolve. This will cause the currently
// rendering component to effectively be a dynamic hole
React.use(makeHangingPromise())
} else {
} else if (workUnitStore.type === 'prerender-ppr') {
// We're prerendering with PPR
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
workUnitStore.dynamicTracking
)
} else if (workUnitStore.type === 'prerender-legacy') {
throwToInterruptStaticGeneration(expression, workStore, workUnitStore)
}
} else {
// We're prerendering in legacy mode
const workUnitStore = workUnitAsyncStorage.getStore()
throwToInterruptStaticGeneration(expression, workStore, workUnitStore)
}
}
}
......
......@@ -63,25 +63,32 @@ export type PrerenderStoreModern = {
*/
readonly cacheSignal: null | CacheSignal
/**
* This signal is used to clean up the prerender once it is complete.
*/
readonly renderSignal: AbortSignal
/**
* During some prerenders we want to track dynamic access.
*/
readonly dynamicTracking: null | DynamicTrackingState
}
export type PrerenderStorePPR = {
type: 'prerender-ppr'
pathname: string | undefined
readonly dynamicTracking: null | DynamicTrackingState
}
export type PrerenderStoreLegacy = {
type: 'prerender-legacy'
pathname: string | undefined
}
export type PrerenderStore = PrerenderStoreLegacy | PrerenderStoreModern
export function isDynamicIOPrerender(workUnitStore: WorkUnitStore): boolean {
return (
workUnitStore.type === 'prerender' &&
!!(workUnitStore.controller || workUnitStore.cacheSignal)
)
}
export type PrerenderStore =
| PrerenderStoreLegacy
| PrerenderStorePPR
| PrerenderStoreModern
export type UseCacheStore = {
type: 'cache'
......@@ -114,6 +121,7 @@ export function getExpectedRequestStore(
}
if (
workUnitStore.type === 'prerender' ||
workUnitStore.type === 'prerender-ppr' ||
workUnitStore.type === 'prerender-legacy'
) {
// This should not happen because we should have checked it already.
......
......@@ -175,6 +175,7 @@ export function addImplicitTags(
? workUnitStore.type === 'request'
? workUnitStore.url.pathname
: workUnitStore.type === 'prerender' ||
workUnitStore.type === 'prerender-ppr' ||
workUnitStore.type === 'prerender-legacy'
? workUnitStore.pathname
: undefined
......@@ -642,7 +643,7 @@ export function createPatchedFetcher(
(isCacheableRevalidate || requestStore?.serverComponentsHmrCache)
) {
if (workUnitStore && workUnitStore.type === 'prerender') {
// We are prerendering at build time or revalidate time so we need to
// We are prerendering at build time or revalidate time with dynamicIO so we need to
// buffer the response so we can guarantee it can be read in a microtask
const bodyBuffer = await res.arrayBuffer()
......
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
} from '../app-render/work-unit-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { abortOnSynchronousDynamicDataAccess } from '../app-render/dynamic-rendering'
export function io(expression: string) {
const workUnitStore = workUnitAsyncStorage.getStore()
if (
workUnitStore &&
workUnitStore.type === 'prerender' &&
isDynamicIOPrerender(workUnitStore)
) {
if (workUnitStore && workUnitStore.type === 'prerender') {
const workStore = workAsyncStorage.getStore()
const route = workStore ? workStore.route : ''
if (workStore) {
......
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
} from '../app-render/work-unit-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import {
postponeWithTracking,
throwToInterruptStaticGeneration,
......@@ -44,31 +41,24 @@ export function connection(): Promise<void> {
)
}
if (workUnitStore && workUnitStore.type === 'prerender') {
// We are in PPR and/or dynamicIO mode and prerendering
if (isDynamicIOPrerender(workUnitStore)) {
// We use the controller and cacheSignal as an indication we are in dynamicIO mode.
// When resolving headers for a prerender with dynamic IO we return a forever promise
// along with property access tracked synchronous headers.
// We don't track dynamic access here because access will be tracked when you access
// one of the properties of the headers object.
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// dynamicIO Prerender
// We return a promise that never resolves to allow the prender to stall at this point
return makeHangingPromise()
} else {
// We are prerendering with PPR. We need track dynamic access here eagerly
// to keep continuity with how headers has worked in PPR without dynamicIO.
// TODO consider switching the semantic to throw on property access intead
} else if (workUnitStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
// We use React's postpone API to interrupt rendering here to create a dynamic hole
postponeWithTracking(
workStore.route,
'connection',
workUnitStore.dynamicTracking
)
} else if (workUnitStore.type === 'prerender-legacy') {
// Legacy Prerender
// We throw an error here to interrupt prerendering to mark the route as dynamic
throwToInterruptStaticGeneration('connection', workStore, workUnitStore)
}
} else if (workStore.isStaticGeneration) {
// We are in a legacy static generation mode while prerendering
// We treat this function call as a bailout of static generation
throwToInterruptStaticGeneration('connection', workStore, workUnitStore)
}
// We fall through to the dynamic context below but we still track dynamic access
// because in dev we can still error for things like using headers inside a cache context
......
......@@ -6,7 +6,6 @@ import {
import { RequestCookies } from '../../server/web/spec-extension/cookies'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
type PrerenderStoreModern,
} from '../app-render/work-unit-async-storage.external'
......@@ -81,31 +80,24 @@ export function cookies(): Promise<ReadonlyRequestCookies> {
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// We are in PPR and/or dynamicIO mode and prerendering
if (isDynamicIOPrerender(workUnitStore)) {
// We use the controller and cacheSignal as an indication we are in dynamicIO mode.
// When resolving cookies for a prerender with dynamic IO we return a forever promise
// along with property access tracked synchronous cookies.
// We don't track dynamic access here because access will be tracked when you access
// one of the properties of the cookies object.
return makeDynamicallyTrackedExoticCookies(
workStore.route,
workUnitStore
)
} else {
// We are prerendering with PPR. We need track dynamic access here eagerly
// to keep continuity with how cookies has worked in PPR without dynamicIO.
// TODO consider switching the semantic to throw on property access instead
postponeWithTracking(
workStore.route,
callingExpression,
workUnitStore.dynamicTracking
)
}
// dynamicIO Prerender
// We don't track dynamic access here because access will be tracked when you access
// one of the properties of the cookies object.
return makeDynamicallyTrackedExoticCookies(
workStore.route,
workUnitStore
)
} else if (workUnitStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
// We are prerendering with PPR. We need track dynamic access here eagerly
// to keep continuity with how cookies has worked in PPR without dynamicIO.
postponeWithTracking(
workStore.route,
callingExpression,
workUnitStore.dynamicTracking
)
} else if (workUnitStore.type === 'prerender-legacy') {
// We are in a legacy static generation mode while prerendering
// Legacy Prerender
// We track dynamic access here so we don't need to wrap the cookies in
// individual property access tracking.
throwToInterruptStaticGeneration(
......
......@@ -43,6 +43,7 @@ export function draftMode(): Promise<DraftMode> {
(workUnitStore.type === 'cache' ||
workUnitStore.type === 'unstable-cache' ||
workUnitStore.type === 'prerender' ||
workUnitStore.type === 'prerender-ppr' ||
workUnitStore.type === 'prerender-legacy')
) {
// Return empty draft mode
......
......@@ -5,7 +5,6 @@ import {
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
type PrerenderStoreModern,
} from '../app-render/work-unit-async-storage.external'
......@@ -86,30 +85,25 @@ export function headers(): Promise<ReadonlyHeaders> {
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// We are in PPR and/or dynamicIO mode and prerendering
if (isDynamicIOPrerender(workUnitStore)) {
// We use the controller and cacheSignal as an indication we are in dynamicIO mode.
// When resolving headers for a prerender with dynamic IO we return a forever promise
// along with property access tracked synchronous headers.
// We don't track dynamic access here because access will be tracked when you access
// one of the properties of the headers object.
return makeDynamicallyTrackedExoticHeaders(
workStore.route,
workUnitStore
)
} else {
// We are prerendering with PPR. We need track dynamic access here eagerly
// to keep continuity with how headers has worked in PPR without dynamicIO.
// TODO consider switching the semantic to throw on property access instead
postponeWithTracking(
workStore.route,
'headers',
workUnitStore.dynamicTracking
)
}
// dynamicIO Prerender
// We don't track dynamic access here because access will be tracked when you access
// one of the properties of the headers object.
return makeDynamicallyTrackedExoticHeaders(
workStore.route,
workUnitStore
)
} else if (workUnitStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
// We are prerendering with PPR. We need track dynamic access here eagerly
// to keep continuity with how headers has worked in PPR without dynamicIO.
// TODO consider switching the semantic to throw on property access instead
postponeWithTracking(
workStore.route,
'headers',
workUnitStore.dynamicTracking
)
} else if (workUnitStore.type === 'prerender-legacy') {
// Legacy Prerender
// We are in a legacy static generation mode while prerendering
// We track dynamic access here so we don't need to wrap the headers in
// individual property access tracking.
......
......@@ -9,9 +9,9 @@ import {
} from '../app-render/dynamic-rendering'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
type WorkUnitStore,
type PrerenderStorePPR,
type PrerenderStoreLegacy,
type PrerenderStoreModern,
} from '../app-render/work-unit-async-storage.external'
import { InvariantError } from '../../shared/lib/invariant-error'
......@@ -96,16 +96,14 @@ export function createPrerenderParamsForClientSegment(
): Promise<Params> {
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore && prerenderStore.type === 'prerender') {
if (isDynamicIOPrerender(prerenderStore)) {
const fallbackParams = workStore.fallbackRouteParams
if (fallbackParams) {
for (let key in underlyingParams) {
if (fallbackParams.has(key)) {
// This params object has one of more fallback params so we need to consider
// the awaiting of this params object "dynamic". Since we are in dynamicIO mode
// we encode this as a promise that never resolves
return makeHangingPromise()
}
const fallbackParams = workStore.fallbackRouteParams
if (fallbackParams) {
for (let key in underlyingParams) {
if (fallbackParams.has(key)) {
// This params object has one of more fallback params so we need to consider
// the awaiting of this params object "dynamic". Since we are in dynamicIO mode
// we encode this as a promise that never resolves
return makeHangingPromise()
}
}
}
......@@ -132,25 +130,31 @@ function createPrerenderParams(
if (hasSomeFallbackParams) {
// params need to be treated as dynamic because we have at least one fallback param
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore && prerenderStore.type === 'prerender') {
if (isDynamicIOPrerender(prerenderStore)) {
const workUnitStore = workUnitAsyncStorage.getStore()
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// We are in a dynamicIO (PPR or otherwise) prerender
return makeAbortingExoticParams(
underlyingParams,
workStore.route,
prerenderStore
workUnitStore
)
} else if (
workUnitStore.type === 'prerender-legacy' ||
workUnitStore.type === 'prerender-ppr'
)
// We aren't in a dynamicIO prerender but we do have fallback params at this
// level so we need to make an erroring exotic params object which will postpone
// if you access the fallback params
return makeErroringExoticParams(
underlyingParams,
fallbackParams,
workStore,
workUnitStore
)
}
}
// We aren't in a dynamicIO prerender but we do have fallback params at this
// level so we need to make an erroring exotic params object which will postpone
// if you access the fallback params
return makeErroringExoticParams(
underlyingParams,
fallbackParams,
workStore,
prerenderStore
throw new InvariantError(
'createPrerenderParams called without a prerenderStore in scope. This is a bug in Next.js'
)
}
}
......@@ -249,7 +253,7 @@ function makeErroringExoticParams(
underlyingParams: Params,
fallbackParams: FallbackRouteParams,
workStore: WorkStore,
prerenderStore: undefined | WorkUnitStore
prerenderStore: PrerenderStorePPR | PrerenderStoreLegacy
): Promise<Params> {
const cachedParams = CachedParams.get(underlyingParams)
if (cachedParams) {
......@@ -304,17 +308,19 @@ function makeErroringExoticParams(
// fallback shells
// TODO remove this comment when dynamicIO is the default since there
// will be no `dynamic = "error"`
if (prerenderStore && prerenderStore.type === 'prerender') {
if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
// Legacy Prerender
throwToInterruptStaticGeneration(
expression,
workStore,
undefined
prerenderStore
)
}
},
......@@ -329,17 +335,19 @@ function makeErroringExoticParams(
// fallback shells
// TODO remove this comment when dynamicIO is the default since there
// will be no `dynamic = "error"`
if (prerenderStore && prerenderStore.type === 'prerender') {
if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
// Legacy Prerender
throwToInterruptStaticGeneration(
expression,
workStore,
undefined
prerenderStore
)
}
},
......
......@@ -10,9 +10,9 @@ import {
} from '../app-render/dynamic-rendering'
import {
isDynamicIOPrerender,
workUnitAsyncStorage,
type WorkUnitStore,
type PrerenderStoreLegacy,
type PrerenderStorePPR,
type PrerenderStoreModern,
} from '../app-render/work-unit-async-storage.external'
import { InvariantError } from '../../shared/lib/invariant-error'
......@@ -90,12 +90,11 @@ export function createPrerenderSearchParamsForClientPage(
}
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore) {
if (isDynamicIOPrerender(prerenderStore)) {
// We're prerendering in a mode that aborts (dynamicIO) and should stall
// the promise to ensure the RSC side is considered dynamic
return makeHangingPromise()
}
if (prerenderStore && prerenderStore.type === 'prerender') {
// dynamicIO Prerender
// We're prerendering in a mode that aborts (dynamicIO) and should stall
// the promise to ensure the RSC side is considered dynamic
return makeHangingPromise()
}
// We're prerendering in a mode that does not aborts. We resolve the promise without
// any tracking because we're just transporting a value from server to client where the tracking
......@@ -112,17 +111,23 @@ function createPrerenderSearchParams(
return Promise.resolve({})
}
const prerenderStore = workUnitAsyncStorage.getStore()
if (prerenderStore && prerenderStore.type === 'prerender') {
if (prerenderStore.controller || prerenderStore.cacheSignal) {
const workUnitStore = workUnitAsyncStorage.getStore()
if (workUnitStore) {
if (workUnitStore.type === 'prerender') {
// We are in a dynamicIO (PPR or otherwise) prerender
return makeAbortingExoticSearchParams(workStore.route, prerenderStore)
return makeAbortingExoticSearchParams(workStore.route, workUnitStore)
} else if (
workUnitStore.type === 'prerender-legacy' ||
workUnitStore.type === 'prerender-ppr'
) {
// We are in a legacy static generation and need to interrupt the prerender
// when search params are accessed.
return makeErroringExoticSearchParams(workStore, workUnitStore)
}
}
// We are in a legacy static generation and need to interrupt the prerender
// when search params are accessed.
return makeErroringExoticSearchParams(workStore, prerenderStore)
throw new InvariantError(
'createPrerenderSearchParams called without a prerenderStore in scope. This is a bug in Next.js'
)
}
function createRenderSearchParams(
......@@ -258,7 +263,7 @@ function makeAbortingExoticSearchParams(
function makeErroringExoticSearchParams(
workStore: WorkStore,
prerenderStore: undefined | WorkUnitStore
prerenderStore: PrerenderStoreLegacy | PrerenderStorePPR
): Promise<SearchParams> {
const cachedSearchParams = CachedSearchParams.get(workStore)
if (cachedSearchParams) {
......@@ -311,18 +316,19 @@ function makeErroringExoticSearchParams(
workStore.route,
expression
)
} else if (prerenderStore && prerenderStore.type === 'prerender') {
} else if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
const workUnitStore = workUnitAsyncStorage.getStore()
// Legacy Prerender
throwToInterruptStaticGeneration(
expression,
workStore,
workUnitStore
prerenderStore
)
}
return
......@@ -335,18 +341,19 @@ function makeErroringExoticSearchParams(
workStore.route,
expression
)
} else if (prerenderStore && prerenderStore.type === 'prerender') {
} else if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
const workUnitStore = workUnitAsyncStorage.getStore()
// Legacy Prerender
throwToInterruptStaticGeneration(
expression,
workStore,
workUnitStore
prerenderStore
)
}
return
......@@ -362,18 +369,19 @@ function makeErroringExoticSearchParams(
workStore.route,
expression
)
} else if (prerenderStore && prerenderStore.type === 'prerender') {
} else if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
const workUnitStore = workUnitAsyncStorage.getStore()
// Legacy Prerender
throwToInterruptStaticGeneration(
expression,
workStore,
workUnitStore
prerenderStore
)
}
}
......@@ -396,15 +404,20 @@ function makeErroringExoticSearchParams(
workStore.route,
expression
)
} else if (prerenderStore && prerenderStore.type === 'prerender') {
} else if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
const workUnitStore = workUnitAsyncStorage.getStore()
throwToInterruptStaticGeneration(expression, workStore, workUnitStore)
// Legacy Prerender
throwToInterruptStaticGeneration(
expression,
workStore,
prerenderStore
)
}
return false
}
......@@ -418,15 +431,16 @@ function makeErroringExoticSearchParams(
workStore.route,
expression
)
} else if (prerenderStore && prerenderStore.type === 'prerender') {
} else if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no dynamicIO)
postponeWithTracking(
workStore.route,
expression,
prerenderStore.dynamicTracking
)
} else {
const workUnitStore = workUnitAsyncStorage.getStore()
throwToInterruptStaticGeneration(expression, workStore, workUnitStore)
// Legacy Prerender
throwToInterruptStaticGeneration(expression, workStore, prerenderStore)
}
},
})
......
......@@ -303,6 +303,7 @@ export class AppRouteRouteModule extends RouteModule<
workUnitStore.type === 'request'
? workUnitStore.url.pathname
: workUnitStore.type === 'prerender' ||
workUnitStore.type === 'prerender-ppr' ||
workUnitStore.type === 'prerender-legacy'
? workUnitStore.pathname
: undefined
......@@ -328,12 +329,14 @@ export class AppRouteRouteModule extends RouteModule<
* Functionally though IO should still take longer than the time it takes to unwrap the response body
* so our heuristic of excluding any IO should be preserved.
*/
const prospectiveController = new AbortController()
let prospectiveRenderIsDynamic = false
const cacheSignal = new CacheSignal()
let dynamicTracking = createDynamicTrackingState(undefined)
const prospectiveRoutePrerenderStore: PrerenderStore = {
type: 'prerender',
renderSignal: prospectiveController.signal,
pathname,
cacheSignal,
// During prospective render we don't use a controller
......@@ -397,14 +400,15 @@ export class AppRouteRouteModule extends RouteModule<
// TODO start passing this controller to the route handler. We should expose
// it so the handler to abort inflight requests and other operations if we abort
// the prerender.
const controller = new AbortController()
const finalController = new AbortController()
dynamicTracking = createDynamicTrackingState(undefined)
const finalRoutePrerenderStore: PrerenderStore = {
type: 'prerender',
renderSignal: finalController.signal,
pathname,
cacheSignal: null,
controller,
controller: finalController,
dynamicTracking,
}
......@@ -446,7 +450,7 @@ export class AppRouteRouteModule extends RouteModule<
scheduleImmediate(() => {
if (!bodyHandled) {
bodyHandled = true
controller.abort()
finalController.abort()
reject(createDynamicIOError(workStore.route))
}
})
......@@ -457,19 +461,19 @@ export class AppRouteRouteModule extends RouteModule<
scheduleImmediate(() => {
if (!responseHandled) {
responseHandled = true
controller.abort()
finalController.abort()
reject(createDynamicIOError(workStore.route))
}
})
})
if (controller.signal.aborted) {
if (finalController.signal.aborted) {
// We aborted from within the execution
throw createDynamicIOError(workStore.route)
} else {
// We didn't abort during the execution. We can abort now as a matter of semantics
// though at the moment nothing actually consumes this signal so it won't halt any
// inflight work.
controller.abort()
finalController.abort()
}
} else if (isStaticGeneration) {
res = await workUnitAsyncStorage.run(
......
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