Unverified Commit db275073 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

pass postpone through staticGenerationStore (#58229)

Instead of requiring React in `maybePostpone` (which is susceptible to referencing the wrong version of React during build time, such as in the case where the static worker patches fetch within an app-route, which doesn't have an experimental runtime), this provides the postpone API to the `staticGenerationStore`. That way we know the API is available in render which is when we'd expect to postpone. 
parent b50090bf
......@@ -1294,7 +1294,8 @@ export async function buildAppStaticPaths({
supportsDynamicHTML: true,
isRevalidate: false,
isBot: false,
experimental: { ppr },
// building static paths should never postpone
experimental: { ppr: false },
},
},
async () => {
......
......@@ -13,15 +13,16 @@ export function maybePostpone(
return
}
// App Route's cannot be postponed, so we only postpone if it's a page. If the
// postpone API is available, use it now.
const React = require('react') as typeof import('react')
if (typeof React.unstable_postpone !== 'function') return
if (!staticGenerationStore.postpone) {
throw new Error(
'Invariant: PPR is enabled but the postpone API is unavailable'
)
}
// Keep track of if the postpone API has been called.
staticGenerationStore.postponeWasTriggered = true
React.unstable_postpone(
staticGenerationStore.postpone(
`This page needs to bail out of prerendering at this point because it used ${reason}. ` +
`React throws this special object to indicate where. It should not be caught by ` +
`your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-caught-error`
......
......@@ -28,6 +28,7 @@ export interface StaticGenerationStore {
dynamicShouldError?: boolean
pendingRevalidates?: Promise<any>[]
postponeWasTriggered?: boolean
postpone?: (reason: string) => never
dynamicUsageDescription?: string
dynamicUsageStack?: string
......
......@@ -1102,7 +1102,11 @@ export const renderToHTMLOrFlight: AppPageRender = (
(requestStore) =>
StaticGenerationAsyncStorageWrapper.wrap(
renderOpts.ComponentMod.staticGenerationAsyncStorage,
{ urlPathname: pathname, renderOpts },
{
urlPathname: pathname,
renderOpts,
postpone: React.unstable_postpone,
},
(staticGenerationStore) =>
renderToHTMLOrFlightImpl(req, res, pagePath, query, renderOpts, {
requestStore,
......
......@@ -5,6 +5,7 @@ import type { IncrementalCache } from '../lib/incremental-cache'
export type StaticGenerationContext = {
urlPathname: string
postpone?: (reason: string) => never
renderOpts: {
originalPathname?: string
incrementalCache?: IncrementalCache
......@@ -37,7 +38,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
> = {
wrap<Result>(
storage: AsyncLocalStorage<StaticGenerationStore>,
{ urlPathname, renderOpts }: StaticGenerationContext,
{ urlPathname, renderOpts, postpone }: StaticGenerationContext,
callback: (store: StaticGenerationStore) => Result
): Result {
/**
......@@ -77,6 +78,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
isDraftMode: renderOpts.isDraftMode,
experimental: renderOpts.experimental,
postpone,
}
// TODO: remove this when we resolve accessing the store outside the execution context
......
......@@ -89,7 +89,7 @@ describe('ppr build errors', () => {
describe('when a postpone call is caught and logged it should', () => {
it('should include a message telling why', async () => {
expect(stdout).toContain(
"Logged error: This page needs to bail out of prerendering at this point because it used Page couldn't be rendered statically because it used `cookies`."
'Logged error: This page needs to bail out of prerendering at this point because it used cookies.'
)
})
})
......
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