From ce80500781621fa84430c9ae32f2b217c1a3b91b Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Sun, 1 Oct 2023 14:03:28 -0700 Subject: [PATCH] use a separate flag to determine if we can statically prefetch --- ...tatic-generation-async-storage.external.ts | 1 + .../components/static-generation-bailout.ts | 10 ++++++---- packages/next/src/export/routes/app-page.ts | 20 ++++++++++--------- .../next/src/server/app-render/app-render.tsx | 4 +++- .../e2e/app-dir/app-static/app-static.test.ts | 1 - 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/next/src/client/components/static-generation-async-storage.external.ts b/packages/next/src/client/components/static-generation-async-storage.external.ts index 6cc9b9d6e2d4a7..256e7457c11278 100644 --- a/packages/next/src/client/components/static-generation-async-storage.external.ts +++ b/packages/next/src/client/components/static-generation-async-storage.external.ts @@ -31,6 +31,7 @@ export interface StaticGenerationStore { dynamicUsageDescription?: string dynamicUsageStack?: string dynamicUsageErr?: DynamicServerError + staticPrefetchBailout?: boolean nextFetchId?: number pathWasRevalidated?: boolean diff --git a/packages/next/src/client/components/static-generation-bailout.ts b/packages/next/src/client/components/static-generation-bailout.ts index 438f55a61284bb..3363fd7b5d3a8e 100644 --- a/packages/next/src/client/components/static-generation-bailout.ts +++ b/packages/next/src/client/components/static-generation-bailout.ts @@ -30,10 +30,6 @@ export const staticGenerationBailout: StaticGenerationBailout = ( return true } - if (staticGenerationStore?.isStaticPrefetch) { - staticGenerationStore.dynamicUsageDescription = reason - } - if (staticGenerationStore?.dynamicShouldError) { throw new StaticGenBailoutError( formatErrorMessage(reason, { ...opts, dynamic: opts?.dynamic ?? 'error' }) @@ -42,6 +38,12 @@ export const staticGenerationBailout: StaticGenerationBailout = ( if (staticGenerationStore) { staticGenerationStore.revalidate = 0 + + if (!opts?.dynamic) { + // we can statically prefetch pages that opt into dynamic, + // but not things like headers/cookies + staticGenerationStore.staticPrefetchBailout = true + } } if (staticGenerationStore?.isStaticGeneration) { diff --git a/packages/next/src/export/routes/app-page.ts b/packages/next/src/export/routes/app-page.ts index 02a7144aa47bd1..30680e8ecd65ec 100644 --- a/packages/next/src/export/routes/app-page.ts +++ b/packages/next/src/export/routes/app-page.ts @@ -47,7 +47,7 @@ export async function generatePrefetchRsc( const prefetchRscData = Buffer.concat(res.buffers) - if ((renderOpts as any).store.dynamicUsageDescription) return + if ((renderOpts as any).store.staticPrefetchBailout) return await fs.writeFile( htmlFilepath.replace(/\.html$/, '.prefetch.rsc'), @@ -100,14 +100,16 @@ export async function exportAppPage( ) } - await generatePrefetchRsc( - req, - path, - res, - pathname, - htmlFilepath, - renderOpts - ) + if (!(renderOpts as any).store.staticPrefetchBailout) { + await generatePrefetchRsc( + req, + path, + res, + pathname, + htmlFilepath, + renderOpts + ) + } const { staticBailoutInfo = {} } = metadata diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index e846b283192d26..54a4eb34013d48 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -701,7 +701,9 @@ export const renderToHTMLOrFlight: AppPageRender = ( staticGenerationStore.dynamicShouldError = true } else if (dynamic === 'force-dynamic') { staticGenerationStore.forceDynamic = true - staticGenerationBailout(`force-dynamic`, { dynamic }) + if (!staticGenerationStore.isStaticPrefetch) { + staticGenerationBailout(`force-dynamic`, { dynamic }) + } } else { staticGenerationStore.dynamicShouldError = false if (dynamic === 'force-static') { diff --git a/test/e2e/app-dir/app-static/app-static.test.ts b/test/e2e/app-dir/app-static/app-static.test.ts index 2fa85426c9f416..ede749c02a3d9a 100644 --- a/test/e2e/app-dir/app-static/app-static.test.ts +++ b/test/e2e/app-dir/app-static/app-static.test.ts @@ -531,7 +531,6 @@ createNextDescribe( 'force-static/second.html', 'ssg-draft-mode/test.html', 'blog/seb/second-post.html', - 'force-static.prefetch.rsc', 'ssg-draft-mode/test-2.rsc', 'blog/styfle/first-post.rsc', 'default-cache.prefetch.rsc',