Unverified Commit 1e5932d5 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[Runtime prefetch] resolve runtime APIs in a separate task (#82475)

In a runtime prefetch, sync IO is dangerous, because it makes us abort
the whole prerender. If this abort happens early, a runtime prefetch can
end up giving us *less* data than a static prefetch. To prevent this, we
can split the final prerender into two separate tasks (which i'm calling
"stages" in the code):
1. The static stage (task 1). This is meant to be equivalent to what
we'd do during build, so runtime APIs like `cookies()` aren't resolved
yet. This lets us render everything that's reachable statically. We
should not hit a sync IO here -- if we had, it would've also caused an
error during build.
2. The runtime stage (task 2). Here, we allow `cookies()` (and all other
runtime APIs available in a runtime prerender) to resolve. We might
encounter sync IO errors here (e.g. `await cookies(); Date.now()`),
which will make the result suboptimal, but still usable.
parent 4a0763e4
...@@ -775,5 +775,6 @@ ...@@ -775,5 +775,6 @@
"774": "Route %s used %s outside of a Server Component. This is not allowed.", "774": "Route %s used %s outside of a Server Component. This is not allowed.",
"775": "Node.js instrumentation extensions should not be loaded in the Edge runtime.", "775": "Node.js instrumentation extensions should not be loaded in the Edge runtime.",
"776": "`unstable_isUnrecognizedActionError` can only be used on the client.", "776": "`unstable_isUnrecognizedActionError` can only be used on the client.",
"777": "Invariant: failed to find source route %s for prerender %s" "777": "Invariant: failed to find source route %s for prerender %s",
"778": "`prerenderAndAbortInSequentialTasksWithStages` should not be called in edge runtime."
} }
...@@ -78,6 +78,7 @@ import { ...@@ -78,6 +78,7 @@ import {
doesExportedHtmlMatchBuildId, doesExportedHtmlMatchBuildId,
} from '../../../shared/lib/segment-cache/output-export-prefetch-encoding' } from '../../../shared/lib/segment-cache/output-export-prefetch-encoding'
import { FetchStrategy } from '../segment-cache' import { FetchStrategy } from '../segment-cache'
import { createPromiseWithResolvers } from '../../../shared/lib/promise-with-resolvers'
// A note on async/await when working in the prefetch cache: // A note on async/await when working in the prefetch cache:
// //
...@@ -2050,17 +2051,6 @@ function addSegmentPathToUrlInOutputExportMode( ...@@ -2050,17 +2051,6 @@ function addSegmentPathToUrlInOutputExportMode(
return url return url
} }
function createPromiseWithResolvers<T>(): PromiseWithResolvers<T> {
// Shim of Stage 4 Promise.withResolvers proposal
let resolve: (value: T | PromiseLike<T>) => void
let reject: (reason: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { resolve: resolve!, reject: reject!, promise }
}
/** /**
* Checks whether the new fetch strategy is likely to provide more content than the old one. * Checks whether the new fetch strategy is likely to provide more content than the old one.
* *
......
...@@ -31,6 +31,41 @@ export function prerenderAndAbortInSequentialTasks<R>( ...@@ -31,6 +31,41 @@ export function prerenderAndAbortInSequentialTasks<R>(
} }
} }
/**
* Like `prerenderAndAbortInSequentialTasks`, but with another task between `prerender` and `abort`,
* which allows us to move a part of the render into a separate task.
*/
export function prerenderAndAbortInSequentialTasksWithStages<R>(
prerender: () => Promise<R>,
advanceStage: () => void,
abort: () => void
): Promise<R> {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError(
'`prerenderAndAbortInSequentialTasksWithStages` should not be called in edge runtime.'
)
} else {
return new Promise((resolve, reject) => {
let pendingResult: Promise<R>
setImmediate(() => {
try {
pendingResult = prerender()
pendingResult.catch(() => {})
} catch (err) {
reject(err)
}
})
setImmediate(() => {
advanceStage()
})
setImmediate(() => {
abort()
resolve(pendingResult)
})
})
}
}
// React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection // React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection
// closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature) // closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature)
// has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream // has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream
......
...@@ -155,7 +155,10 @@ import { createMutableActionQueue } from '../../client/components/app-router-ins ...@@ -155,7 +155,10 @@ import { createMutableActionQueue } from '../../client/components/app-router-ins
import { getRevalidateReason } from '../instrumentation/utils' import { getRevalidateReason } from '../instrumentation/utils'
import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment' import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment'
import type { FallbackRouteParams } from '../request/fallback-params' import type { FallbackRouteParams } from '../request/fallback-params'
import { processPrelude } from './app-render-prerender-utils' import {
prerenderAndAbortInSequentialTasksWithStages,
processPrelude,
} from './app-render-prerender-utils'
import { import {
type ReactServerPrerenderResult, type ReactServerPrerenderResult,
ReactServerResult, ReactServerResult,
...@@ -200,6 +203,7 @@ import { getRequestMeta } from '../request-meta' ...@@ -200,6 +203,7 @@ import { getRequestMeta } from '../request-meta'
import { getDynamicParam } from '../../shared/lib/router/utils/get-dynamic-param' import { getDynamicParam } 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'
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
...@@ -711,6 +715,8 @@ async function prospectiveRuntimeServerPrerender( ...@@ -711,6 +715,8 @@ async function prospectiveRuntimeServerPrerender(
prerenderResumeDataCache, prerenderResumeDataCache,
hmrRefreshHash: undefined, hmrRefreshHash: undefined,
captureOwnerStack: undefined, captureOwnerStack: undefined,
// We only need task sequencing in the final prerender.
runtimeStagePromise: 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.
cookies, cookies,
draftMode, draftMode,
...@@ -821,6 +827,9 @@ async function finalRuntimeServerPrerender( ...@@ -821,6 +827,9 @@ async function finalRuntimeServerPrerender(
isDebugDynamicAccesses isDebugDynamicAccesses
) )
const { promise: runtimeStagePromise, resolve: resolveBlockedRuntimeAPIs } =
createPromiseWithResolvers<void>()
const finalServerPrerenderStore: PrerenderStoreModernRuntime = { const finalServerPrerenderStore: PrerenderStoreModernRuntime = {
type: 'prerender-runtime', type: 'prerender-runtime',
phase: 'render', phase: 'render',
...@@ -841,6 +850,8 @@ async function finalRuntimeServerPrerender( ...@@ -841,6 +850,8 @@ async function finalRuntimeServerPrerender(
renderResumeDataCache, renderResumeDataCache,
hmrRefreshHash: undefined, hmrRefreshHash: undefined,
captureOwnerStack: undefined, captureOwnerStack: undefined,
// Used to separate the "Static" stage from the "Runtime" stage.
runtimeStagePromise,
// 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.
cookies, cookies,
draftMode, draftMode,
...@@ -852,8 +863,9 @@ async function finalRuntimeServerPrerender( ...@@ -852,8 +863,9 @@ async function finalRuntimeServerPrerender(
) )
let prerenderIsPending = true let prerenderIsPending = true
const result = await prerenderAndAbortInSequentialTasks( const result = await prerenderAndAbortInSequentialTasksWithStages(
async () => { async () => {
// Static stage
const prerenderResult = await workUnitAsyncStorage.run( const prerenderResult = await workUnitAsyncStorage.run(
finalServerPrerenderStore, finalServerPrerenderStore,
ComponentMod.prerender, ComponentMod.prerender,
...@@ -869,6 +881,17 @@ async function finalRuntimeServerPrerender( ...@@ -869,6 +881,17 @@ async function finalRuntimeServerPrerender(
return prerenderResult return prerenderResult
}, },
() => { () => {
// Advance to the runtime stage.
//
// We make runtime APIs hang during the first task (above), and unblock them in the following task (here).
// This makes sure that, at this point, we'll have finished all the static parts (what we'd prerender statically).
// 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()`),
// we'll abort, but we'll produce at least as much output as a static prerender would.
resolveBlockedRuntimeAPIs()
},
() => {
// Abort.
if (finalServerController.signal.aborted) { if (finalServerController.signal.aborted) {
// If the server controller is already aborted we must have called something // If the server controller is already aborted we must have called something
// that required aborting the prerender synchronously such as with new Date() // that required aborting the prerender synchronously such as with new Date()
......
...@@ -26,6 +26,7 @@ import type { ...@@ -26,6 +26,7 @@ import type {
RequestStore, RequestStore,
PrerenderStoreLegacy, PrerenderStoreLegacy,
PrerenderStoreModern, PrerenderStoreModern,
PrerenderStoreModernRuntime,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
// Once postpone is in stable we should switch to importing the postpone export directly // Once postpone is in stable we should switch to importing the postpone export directly
...@@ -33,7 +34,10 @@ import React from 'react' ...@@ -33,7 +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 { workUnitAsyncStorage } from './work-unit-async-storage.external' import {
getRuntimeStagePromise,
workUnitAsyncStorage,
} from './work-unit-async-storage.external'
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 {
...@@ -548,13 +552,26 @@ export function createHangingInputAbortSignal( ...@@ -548,13 +552,26 @@ export function createHangingInputAbortSignal(
}) })
} else { } else {
// Otherwise we're in the final render and we should already have all // Otherwise we're in the final render and we should already have all
// our caches filled. We might still be waiting on some microtasks so we // our caches filled.
// If the prerender uses stages, we have wait until the runtime stage,
// at which point all runtime inputs will be resolved.
// (otherwise, a runtime prerender might consider `cookies()` hanging
// even though they'd resolve in the next task.)
//
// We might still be waiting on some microtasks so we
// wait one tick before giving up. When we give up, we still want to // wait one tick before giving up. When we give up, we still want to
// 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 (runtimeStagePromise) {
runtimeStagePromise.then(() =>
scheduleOnNextTick(() => controller.abort())
)
} else {
scheduleOnNextTick(() => controller.abort()) scheduleOnNextTick(() => controller.abort())
} }
}
return controller.signal return controller.signal
case 'prerender-client': case 'prerender-client':
...@@ -824,3 +841,13 @@ export function throwIfDisallowedDynamic( ...@@ -824,3 +841,13 @@ export function throwIfDisallowedDynamic(
} }
} }
} }
export function delayUntilRuntimeStage<T>(
prerenderStore: PrerenderStoreModernRuntime,
result: Promise<T>
): Promise<T> {
if (prerenderStore.runtimeStagePromise) {
return prerenderStore.runtimeStagePromise.then(() => result)
}
return result
}
...@@ -106,6 +106,18 @@ export interface PrerenderStoreModernRuntime ...@@ -106,6 +106,18 @@ export interface PrerenderStoreModernRuntime
extends PrerenderStoreModernCommon { extends PrerenderStoreModernCommon {
readonly type: 'prerender-runtime' readonly type: 'prerender-runtime'
/**
* 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 cookies: RequestStore['cookies'] readonly cookies: RequestStore['cookies']
readonly draftMode: RequestStore['draftMode'] readonly draftMode: RequestStore['draftMode']
} }
...@@ -268,6 +280,18 @@ export interface PublicUseCacheStore extends CommonUseCacheStore { ...@@ -268,6 +280,18 @@ 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
/** /**
* As opposed to the public cache store, the private cache store is allowed to * As opposed to the public cache store, the private cache store is allowed to
* access the request cookies. * access the request cookies.
...@@ -486,3 +510,23 @@ export function getCacheSignal( ...@@ -486,3 +510,23 @@ 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
}
}
...@@ -15,6 +15,7 @@ import { ...@@ -15,6 +15,7 @@ import {
type PrerenderStoreModern, type PrerenderStoreModern,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { import {
delayUntilRuntimeStage,
postponeWithTracking, postponeWithTracking,
throwToInterruptStaticGeneration, throwToInterruptStaticGeneration,
trackDynamicDataInDynamicRender, trackDynamicDataInDynamicRender,
...@@ -118,6 +119,10 @@ export function cookies(): Promise<ReadonlyRequestCookies> { ...@@ -118,6 +119,10 @@ export function cookies(): Promise<ReadonlyRequestCookies> {
workUnitStore workUnitStore
) )
case 'prerender-runtime': case 'prerender-runtime':
return delayUntilRuntimeStage(
workUnitStore,
makeUntrackedExoticCookies(workUnitStore.cookies)
)
case 'private-cache': case 'private-cache':
return makeUntrackedExoticCookies(workUnitStore.cookies) return makeUntrackedExoticCookies(workUnitStore.cookies)
case 'request': case 'request':
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external' import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { import {
abortAndThrowOnSynchronousRequestDataAccess, abortAndThrowOnSynchronousRequestDataAccess,
delayUntilRuntimeStage,
postponeWithTracking, postponeWithTracking,
trackDynamicDataInDynamicRender, trackDynamicDataInDynamicRender,
trackSynchronousRequestDataAccessInDev, trackSynchronousRequestDataAccessInDev,
...@@ -56,6 +57,11 @@ export function draftMode(): Promise<DraftMode> { ...@@ -56,6 +57,11 @@ export function draftMode(): Promise<DraftMode> {
switch (workUnitStore.type) { switch (workUnitStore.type) {
case 'prerender-runtime': case 'prerender-runtime':
// TODO(runtime-ppr): does it make sense to delay this? normally it's always microtasky
return delayUntilRuntimeStage(
workUnitStore,
createOrGetCachedDraftMode(workUnitStore.draftMode, workStore)
)
case 'request': case 'request':
return createOrGetCachedDraftMode(workUnitStore.draftMode, workStore) return createOrGetCachedDraftMode(workUnitStore.draftMode, workStore)
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
throwToInterruptStaticGeneration, throwToInterruptStaticGeneration,
postponeWithTracking, postponeWithTracking,
trackSynchronousRequestDataAccessInDev, trackSynchronousRequestDataAccessInDev,
delayUntilRuntimeStage,
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
import { import {
...@@ -114,6 +115,10 @@ export function createServerParamsForRoute( ...@@ -114,6 +115,10 @@ export function createServerParamsForRoute(
'createServerParamsForRoute should not be called in cache contexts.' 'createServerParamsForRoute should not be called in cache contexts.'
) )
case 'prerender-runtime': case 'prerender-runtime':
return delayUntilRuntimeStage(
workUnitStore,
createRenderParams(underlyingParams, workStore)
)
case 'request': case 'request':
break break
default: default:
...@@ -142,6 +147,10 @@ export function createServerParamsForServerSegment( ...@@ -142,6 +147,10 @@ export function createServerParamsForServerSegment(
'createServerParamsForServerSegment should not be called in cache contexts.' 'createServerParamsForServerSegment should not be called in cache contexts.'
) )
case 'prerender-runtime': case 'prerender-runtime':
return delayUntilRuntimeStage(
workUnitStore,
createRenderParams(underlyingParams, workStore)
)
case 'request': case 'request':
break break
default: default:
......
import type { WorkStore } from '../app-render/work-async-storage.external' import type { WorkStore } from '../app-render/work-async-storage.external'
import { import {
delayUntilRuntimeStage,
postponeWithTracking, postponeWithTracking,
type DynamicTrackingState, type DynamicTrackingState,
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
...@@ -37,6 +38,10 @@ export function createServerPathnameForMetadata( ...@@ -37,6 +38,10 @@ export function createServerPathnameForMetadata(
) )
case 'prerender-runtime': case 'prerender-runtime':
return delayUntilRuntimeStage(
workUnitStore,
createRenderPathname(underlyingPathname)
)
case 'request': case 'request':
break break
default: default:
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
trackDynamicDataInDynamicRender, trackDynamicDataInDynamicRender,
annotateDynamicAccess, annotateDynamicAccess,
trackSynchronousRequestDataAccessInDev, trackSynchronousRequestDataAccessInDev,
delayUntilRuntimeStage,
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
import { import {
...@@ -114,6 +115,10 @@ export function createServerSearchParamsForServerPage( ...@@ -114,6 +115,10 @@ export function createServerSearchParamsForServerPage(
'createServerSearchParamsForServerPage should not be called in cache contexts.' 'createServerSearchParamsForServerPage should not be called in cache contexts.'
) )
case 'prerender-runtime': case 'prerender-runtime':
return delayUntilRuntimeStage(
workUnitStore,
createRenderSearchParams(underlyingSearchParams, workStore)
)
case 'request': case 'request':
break break
default: default:
......
...@@ -34,6 +34,7 @@ import { ...@@ -34,6 +34,7 @@ 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 { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
...@@ -199,6 +200,7 @@ function createUseCacheStore( ...@@ -199,6 +200,7 @@ function createUseCacheStore(
outerWorkUnitStore outerWorkUnitStore
), ),
forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore), forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore),
runtimeStagePromise: getRuntimeStagePromise(outerWorkUnitStore),
draftMode: getDraftModeProviderForCacheScope( draftMode: getDraftModeProviderForCacheScope(
workStore, workStore,
outerWorkUnitStore outerWorkUnitStore
...@@ -953,6 +955,17 @@ export function cache( ...@@ -953,6 +955,17 @@ export function cache(
? createHangingInputAbortSignal(workUnitStore) ? createHangingInputAbortSignal(workUnitStore)
: undefined : undefined
// 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.
if (cacheContext.kind === 'private') {
const runtimeStagePromise = getRuntimeStagePromise(
cacheContext.outerWorkUnitStore
)
if (runtimeStagePromise) {
await runtimeStagePromise
}
}
let isPageOrLayout = false let isPageOrLayout = false
// For page and layout components, the cache function is overwritten, // For page and layout components, the cache function is overwritten,
...@@ -1068,6 +1081,14 @@ export function cache( ...@@ -1068,6 +1081,14 @@ export function cache(
switch (workUnitStore?.type) { switch (workUnitStore?.type) {
case 'prerender-runtime': case 'prerender-runtime':
// We're currently only using `dynamicAccessAsyncStorage` for params,
// which are always available in a runtime prerender, so they will never hang,
// effectively making the tracking below a no-op.
// However, a runtime prerender shares a lot of the semantics with a static prerender,
// and might need to follow this codepath in the future
// if we start using `dynamicAccessAsyncStorage` for other APIs.
//
// fallthrough
case 'prerender': case 'prerender':
if (!isPageOrLayout) { if (!isPageOrLayout) {
// If the "use cache" function is not a page or a layout, we need to // If the "use cache" function is not a page or a layout, we need to
...@@ -1156,7 +1177,14 @@ export function cache( ...@@ -1156,7 +1177,14 @@ export function cache(
workStore.route, workStore.route,
'dynamic "use cache"' 'dynamic "use 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
// are resolved with a delay, in the runtime stage.
if (workUnitStore.runtimeStagePromise) {
await workUnitStore.runtimeStagePromise
}
break
}
case 'prerender-ppr': case 'prerender-ppr':
case 'prerender-legacy': case 'prerender-legacy':
case 'request': case 'request':
...@@ -1338,6 +1366,12 @@ export function cache( ...@@ -1338,6 +1366,12 @@ export function cache(
'dynamic "use cache"' 'dynamic "use 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
// are resolved with a delay, in the runtime stage.
if (workUnitStore.runtimeStagePromise) {
await workUnitStore.runtimeStagePromise
}
break
case 'prerender-ppr': case 'prerender-ppr':
case 'prerender-legacy': case 'prerender-legacy':
case 'request': case 'request':
......
export function createPromiseWithResolvers<T>(): PromiseWithResolvers<T> {
// Shim of Stage 4 Promise.withResolvers proposal
let resolve: (value: T | PromiseLike<T>) => void
let reject: (reason: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { resolve: resolve!, reject: reject!, promise }
}
import { DebugLinkAccordion } from '../../../components/link-accordion'
export default async function Page() {
return (
<main>
<h1>Errors</h1>
<h2>thrown errors</h2>
<ul>
<li>
<DebugLinkAccordion
href="/errors/error-after-cookies"
prefetch={true}
/>
</li>
</ul>
<h2>sync IO</h2>
<ul>
<li>
<DebugLinkAccordion
href="/errors/sync-io-after-runtime-api/cookies"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/errors/sync-io-after-runtime-api/dynamic-params/123"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/errors/sync-io-after-runtime-api/search-params?foo=bar"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/errors/sync-io-after-runtime-api/private-cache"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/errors/sync-io-after-runtime-api/quickly-expiring-public-cache"
prefetch={true}
/>
</li>
</ul>
</main>
)
}
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { Suspense } from 'react' import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../shared' import { DebugRenderKind } from '../../../../shared'
export default async function Page() { export default async function Page() {
return ( return (
...@@ -8,18 +8,16 @@ export default async function Page() { ...@@ -8,18 +8,16 @@ export default async function Page() {
<DebugRenderKind /> <DebugRenderKind />
<p id="intro"> <p id="intro">
This page performs sync IO after a cookies() call, so we should only see This page performs sync IO after a cookies() call, so we should only see
the error in a runtime prefetch or a navigation (and not during the error in a runtime prefetch
prerendering / prefetching)
</p> </p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}> <Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<One /> <RuntimePrefetchable />
</Suspense> </Suspense>
</main> </main>
) )
} }
async function One() { async function RuntimePrefetchable() {
const cookieStore = await cookies() await cookies()
await cachedDelay(['/cookies', cookieStore.get('user-agent')?.value])
return <div id="timestamp">Timestamp: {Date.now()}</div> return <div id="timestamp">Timestamp: {Date.now()}</div>
} }
import { Suspense } from 'react'
import { DebugRenderKind } from '../../../../../shared'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
type Params = { id: string }
export default async function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<DebugRenderKind />
<p id="intro">
This page performs sync IO after awaiting params, so we should only see
the error in a runtime prefetch
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable params={params} />
</Suspense>
</main>
)
}
async function RuntimePrefetchable({ params }: { params: Promise<Params> }) {
const res = await params
console.log(
'RuntimePrefetchable :: awaited params',
res,
workUnitAsyncStorage.getStore()
)
return <div id="timestamp">Timestamp: {Date.now()}</div>
}
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../../shared'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p id="intro">
This page performs sync IO after awaiting a private cache, so we should
only see the error in a runtime prefetch
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable />
</Suspense>
</main>
)
}
async function RuntimePrefetchable() {
await privateCache()
return <div id="timestamp">Timestamp: {Date.now()}</div>
}
async function privateCache() {
'use cache: private'
await cachedDelay([__dirname])
}
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../../shared'
import { unstable_cacheLife } from 'next/cache'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p id="intro">
This page performs sync IO after awaiting a quickly-expiring public
cache, so we should only see the error in a runtime prefetch
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable />
</Suspense>
</main>
)
}
async function RuntimePrefetchable() {
await publicCache()
return <div id="timestamp">Timestamp: {Date.now()}</div>
}
async function publicCache() {
'use cache'
unstable_cacheLife('seconds')
await cachedDelay([__dirname])
}
import { Suspense } from 'react'
import { DebugRenderKind } from '../../../../shared'
type AnySearchParams = { [key: string]: string | string[] | undefined }
export default async function Page({
searchParams,
}: {
searchParams: Promise<AnySearchParams>
}) {
return (
<main>
<DebugRenderKind />
<p id="intro">
This page performs sync IO after awaiting searchParams, so we should
only see the error in a runtime prefetch
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable searchParams={searchParams} />
</Suspense>
</main>
)
}
async function RuntimePrefetchable({
searchParams,
}: {
searchParams: Promise<AnySearchParams>
}) {
await searchParams
return <div id="timestamp">Timestamp: {Date.now()}</div>
}
...@@ -136,6 +136,68 @@ export default async function Page() { ...@@ -136,6 +136,68 @@ export default async function Page() {
</li> </li>
</ul> </ul>
<h2>
<code>runtime promise passed to public cache</code>
</h2>
<ul>
<li>
cookies() promise passed to public cache + dynamic content
<ul>
<li>
<DebugLinkAccordion
href="/passed-to-public-cache/cookies"
prefetch={true}
/>
</li>
</ul>
</li>
<li>
dynamic params promise passed to public cache + dynamic content
<ul>
<li>
<DebugLinkAccordion
href="/passed-to-public-cache/dynamic-params/123"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/passed-to-public-cache/dynamic-params/456"
prefetch={true}
/>
</li>
</ul>
</li>
<li>
search params promise passed to public cache + dynamic content
<ul>
<li>
<DebugLinkAccordion
href="/passed-to-public-cache/search-params?searchParam=123"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/passed-to-public-cache/search-params?searchParam=456"
prefetch={true}
/>
</li>
</ul>
</li>
<li>
only cookies passed to public cache (no dynamic content)
<ul>
<li>
<DebugLinkAccordion
href="/passed-to-public-cache/cookies-only"
prefetch={true}
/>
</li>
</ul>
</li>
</ul>
<h2>short-lived caches</h2> <h2>short-lived caches</h2>
<ul> <ul>
<li> <li>
...@@ -231,22 +293,6 @@ export default async function Page() { ...@@ -231,22 +293,6 @@ export default async function Page() {
<DebugLinkAccordion href="/fully-static" prefetch={true} /> <DebugLinkAccordion href="/fully-static" prefetch={true} />
</li> </li>
</ul> </ul>
<h2>errors</h2>
<ul>
<li>
<DebugLinkAccordion
href="/errors/error-after-cookies"
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href="/errors/sync-io-after-cookies"
prefetch={true}
/>
</li>
</ul>
</main> </main>
) )
} }
import { cookies } from 'next/headers'
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../shared'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p>
This page uses cookies (from a private cache) and no uncached IO, So it
should be completely prefetchable with a runtime prefetch.
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable />
</Suspense>
</main>
)
}
async function RuntimePrefetchable() {
await cookies() // Guard from being statically prerendered, which would make the cache hang
// We've already awaited cookies, but we still want to make sure
// that the cache doesn't consider them a hanging promise
const cookieValue = await publicCache(
cookies().then(
(cookieStore) => cookieStore.get('testCookie')?.value ?? null
)
)
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="cookie-value">{`Cookie: ${cookieValue}`}</div>
</div>
)
}
async function publicCache(cookiePromise: Promise<string | null>) {
'use cache'
const cookieValue = await cookiePromise
await cachedDelay([__filename, cookieValue])
return cookieValue
}
import { cookies } from 'next/headers'
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../shared'
import { connection } from 'next/server'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p>
This page passes cookies to a public cache, and uses some uncached IO,
so parts of it should be prefetchable with a runtime prefetch.
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable />
</Suspense>
<form
action={async (formData: FormData) => {
'use server'
const cookieStore = await cookies()
const cookieValue = formData.get('cookie') as string | null
if (cookieValue) {
cookieStore.set('testCookie', cookieValue)
}
}}
>
<input type="text" name="cookie" />
<button type="submit">Update cookie</button>
</form>
</main>
)
}
async function RuntimePrefetchable() {
await cookies() // Guard from being statically prerendered, which would make the cache hang
// We've already awaited cookies, but we still want to make sure
// that the cache doesn't consider them a hanging promise
const cookieValue = await publicCache(
cookies().then(
(cookieStore) => cookieStore.get('testCookie')?.value ?? null
)
)
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="cookie-value">{`Cookie: ${cookieValue}`}</div>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
</div>
)
}
async function publicCache(cookiePromise: Promise<string | null>) {
'use cache'
const cookieValue = await cookiePromise
await cachedDelay([__filename, cookieValue])
return cookieValue
}
async function Dynamic() {
await connection()
return (
<div style={{ border: '1px solid tomato', padding: '1em' }}>
<div id="dynamic-content">Dynamic content</div>
</div>
)
}
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../../shared'
import { connection } from 'next/server'
import { cookies } from 'next/headers'
type Params = { id: string }
export default async function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<DebugRenderKind />
<p>
This page passes dynamic params to a public cache, and uses some
uncached IO, so parts of it should be prefetchable with a runtime
prefetch.
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable params={params} />
</Suspense>
</main>
)
}
async function RuntimePrefetchable({ params }: { params: Promise<Params> }) {
await cookies() // Guard from being statically prerendered, which would make the cache hang
const id = await publicCache(params)
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="param-value">{`Param: ${id}`}</div>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
</div>
)
}
async function publicCache(params: Promise<Params>) {
'use cache'
const { id } = await params
await cachedDelay([__filename, id])
return id
}
async function Dynamic() {
await connection()
return (
<div style={{ border: '1px solid tomato', padding: '1em' }}>
<div id="dynamic-content">Dynamic content</div>
</div>
)
}
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../shared'
import { connection } from 'next/server'
import { cookies } from 'next/headers'
type AnySearchParams = { [key: string]: string | string[] | undefined }
export default async function Page({
searchParams,
}: {
searchParams: Promise<AnySearchParams>
}) {
return (
<main>
<DebugRenderKind />
<p>
This page passes search params to a public cache, and uses some uncached
IO, so parts of it should be prefetchable with a runtime prefetch.
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable searchParams={searchParams} />
</Suspense>
</main>
)
}
async function RuntimePrefetchable({
searchParams,
}: {
searchParams: Promise<AnySearchParams>
}) {
await cookies() // Guard from being statically prerendered, which would make the cache hang
const searchParam = await publicCache(searchParams)
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="search-param-value">{`Search param: ${searchParam}`}</div>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
</div>
)
}
async function publicCache(searchParams: Promise<AnySearchParams>) {
'use cache'
const { searchParam } = await searchParams
await cachedDelay([__filename, searchParam])
return searchParam
}
async function Dynamic() {
await connection()
return (
<div style={{ border: '1px solid tomato', padding: '1em' }}>
<div id="dynamic-content">Dynamic content</div>
</div>
)
}
...@@ -51,6 +51,29 @@ export default async function Page() { ...@@ -51,6 +51,29 @@ export default async function Page() {
</ul> </ul>
</li> </li>
</ul> </ul>
<h2>
<code>promise passed to public cache</code>
</h2>
<ul>
<li>
root params + dynamic content
<ul>
<li>
<DebugLinkAccordion
href={`/with-root-param/${currentLang}/passed-to-public-cache/root-params`}
prefetch={true}
/>
</li>
<li>
<DebugLinkAccordion
href={`/with-root-param/${otherLang}/passed-to-public-cache/root-params`}
prefetch={true}
/>
</li>
</ul>
</li>
</ul>
</main> </main>
) )
} }
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../../shared'
import { connection } from 'next/server'
import { lang } from 'next/root-params'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p>
This page uses root params (passed to a private cache) and some uncached
IO. Root params should always be available in static prerenders, so a
runtime prefetch should have them too, and they should not be considered
a hanging input.
</p>
<Suspense fallback="Loading 1...">
<DynamicallyPrefetchable />
</Suspense>
</main>
)
}
async function DynamicallyPrefetchable() {
const currentLang = await publicCache(lang())
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="root-param-value">{`Lang: ${currentLang}`}</div>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
</div>
)
}
async function publicCache(currentLangPromise: Promise<string>) {
'use cache'
const currentLang = await currentLangPromise
await cachedDelay([__filename, currentLang])
return currentLang
}
async function Dynamic() {
await uncachedIO()
await connection()
return (
<div style={{ border: '1px solid tomato', padding: '1em' }}>
<div id="dynamic-content">Dynamic content</div>
</div>
)
}
...@@ -11,9 +11,36 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -11,9 +11,36 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
return return
} }
let currentCliOutputIndex = 0
beforeEach(() => {
resetCliOutput()
})
const getCliOutput = () => {
if (next.cliOutput.length < currentCliOutputIndex) {
// cliOutput shrank since we started the test, so something (like a `sandbox`) reset the logs
currentCliOutputIndex = 0
}
return next.cliOutput.slice(currentCliOutputIndex)
}
const resetCliOutput = () => {
currentCliOutputIndex = next.cliOutput.length
}
describe.each([ describe.each([
{ description: 'in a page', prefix: 'in-page' }, {
{ description: 'in a private cache', prefix: 'in-private-cache' }, description: 'in a page',
prefix: 'in-page',
},
{
description: 'in a private cache',
prefix: 'in-private-cache',
},
{
description: 'passed to a public cache',
prefix: 'passed-to-public-cache',
},
])('$description', ({ prefix }) => { ])('$description', ({ prefix }) => {
it('includes dynamic params, but not dynamic content', async () => { it('includes dynamic params, but not dynamic content', async () => {
let page: Playwright.Page let page: Playwright.Page
...@@ -951,7 +978,38 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -951,7 +978,38 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
}) })
describe('errors', () => { describe('errors', () => {
it('aborts the prerender and logs an error when sync IO is used after cookies()', async () => { it.each([
{
description: 'when sync IO is used after awaiting cookies()',
path: '/errors/sync-io-after-runtime-api/cookies',
},
// TODO(dynamic-ppr):
// A tree prefetch for "/dynamic-params/123" currently causes it to be prerendered on demand,
// meaning that we end up statically prerendering it with all the params included.
// Because of this, `await params` doesn't hang in the static prerender, so we hit the sync IO error behind it.
// This error somehow causes an infinite redirect loop between two different `?_rsc` URLs.
// Investigate this separately or wait until we start always using fallback params for route trees.
//
// {
// description: 'when sync IO is used after awaiting dynamic params',
// path: '/errors/sync-io-after-runtime-api/dynamic-params/123',
// },
{
description: 'when sync IO is used after awaiting searchParams',
path: '/errors/sync-io-after-runtime-api/search-params?foo=bar',
},
{
description: 'when sync IO is used after awaiting a private cache',
path: '/errors/sync-io-after-runtime-api/private-cache',
},
{
description:
'when sync IO is used after awaiting a quickly-expiring public cache',
path: '/errors/sync-io-after-runtime-api/quickly-expiring-public-cache',
},
])(
'aborts the prerender and logs an error $description',
async ({ path }) => {
// In a runtime prefetch, we might encounter sync IO usages that weren't caught during build, // In a runtime prefetch, we might encounter sync IO usages that weren't caught during build,
// because they were hidden behind e.g. a cookies() call. // because they were hidden behind e.g. a cookies() call.
// We currently have no way to catch these statically. // We currently have no way to catch these statically.
...@@ -960,19 +1018,19 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -960,19 +1018,19 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
// TODO: this doesn't work as well as it could, see comment before the navigation // TODO: this doesn't work as well as it could, see comment before the navigation
let page: Playwright.Page let page: Playwright.Page
const browser = await next.browser('/', { const browser = await next.browser('/errors', {
beforePageLoad(p: Playwright.Page) { beforePageLoad(p: Playwright.Page) {
page = p page = p
}, },
}) })
const act = createRouterAct(page) const act = createRouterAct(page)
const STATIC_CONTENT = 'This page performs sync IO after a cookies() call' const STATIC_CONTENT = 'This page performs sync IO after'
// Reveal the link to trigger a runtime prefetch // Reveal the link to trigger a runtime prefetch
await act(async () => { await act(async () => {
const linkToggle = await browser.elementByCss( const linkToggle = await browser.elementByCss(
`input[data-prefetch="runtime"][data-link-accordion="/errors/sync-io-after-cookies"]` `input[data-prefetch="runtime"][data-link-accordion="${path}"]`
) )
await linkToggle.click() await linkToggle.click()
}, [ }, [
...@@ -989,28 +1047,30 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -989,28 +1047,30 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
]) ])
if (!isNextDeploy) { if (!isNextDeploy) {
expect(next.cliOutput).toContain( expect(getCliOutput()).toMatch(
'Error: Route "/errors/sync-io-after-cookies" used `Date.now()` instead of using `performance` or without explicitly calling `await connection()` beforehand.' /Error: Route ".*?" used `Date\.now\(\)` instead of using `performance` or without explicitly calling `await connection\(\)` beforehand\./
) )
} }
// TODO(runtime-ppr): we should be able to display the (aborted, partial) prefetched contents before navigating,
// but it seems like when we abort the render, we're also inadvertently cutting off some promises related to metadata
// which end up suspending on the client and blocking react from rendering.
// Ideally, we'd be able to use the partial result, but this is an error scenario and we don't crash,
// so i'm just settling for that for now.
// Navigate to the page // Navigate to the page
await act(async () => {
await act( await act(
async () => { async () => {
await browser await browser.elementByCss(`a[href="${path}"]`).click()
.elementByCss(`a[href="/errors/sync-io-after-cookies"]`)
.click()
}, },
{ {
// Temporarily block the navigation request.
includes: 'Timestamp', includes: 'Timestamp',
block: true,
} }
) )
// We aborted the render because of sync IO, so we won't display the timestamp,
// but due to the way we sequence tasks, we should've at least finished rendering the static parts.
expect(await browser.elementsByCss('#timestamp')).toHaveLength(0)
expect(await browser.elementById('intro').text()).toInclude(
STATIC_CONTENT
)
})
// After navigating, we should see the sync IO result that we omitted from the prefetch. // After navigating, we should see the sync IO result that we omitted from the prefetch.
expect(await browser.elementById('intro').text()).toInclude( expect(await browser.elementById('intro').text()).toInclude(
...@@ -1019,14 +1079,15 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -1019,14 +1079,15 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
expect(await browser.elementById('timestamp').text()).toMatch( expect(await browser.elementById('timestamp').text()).toMatch(
/Timestamp: \d+/ /Timestamp: \d+/
) )
}) }
)
it('should trigger error boundaries for errors that occurred in runtime-prefetched content', async () => { it('should trigger error boundaries for errors that occurred in runtime-prefetched content', async () => {
// A thrown error in the prerender should not stop us from sending a prefetch response. // A thrown error in the prerender should not stop us from sending a prefetch response.
// This should work without any extra effort, but I'm adding a test for it as a sanity check. // This should work without any extra effort, but I'm adding a test for it as a sanity check.
let page: Playwright.Page let page: Playwright.Page
const browser = await next.browser('/', { const browser = await next.browser('/errors', {
beforePageLoad(p: Playwright.Page) { beforePageLoad(p: Playwright.Page) {
page = p page = p
}, },
...@@ -1049,7 +1110,7 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -1049,7 +1110,7 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
]) ])
if (!isNextDeploy) { if (!isNextDeploy) {
expect(next.cliOutput).toContain('Error: Kaboom') expect(getCliOutput()).toContain('Error: Kaboom')
} }
// Navigate to the page. We already have the paged cached. // Navigate to the page. We already have the paged cached.
......
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