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