Skip to content

Commit

Permalink
use a separate flag to determine if we can statically prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Oct 1, 2023
1 parent 3c8d500 commit ce80500
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface StaticGenerationStore {
dynamicUsageDescription?: string
dynamicUsageStack?: string
dynamicUsageErr?: DynamicServerError
staticPrefetchBailout?: boolean

nextFetchId?: number
pathWasRevalidated?: boolean
Expand Down
10 changes: 6 additions & 4 deletions packages/next/src/client/components/static-generation-bailout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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) {
Expand Down
20 changes: 11 additions & 9 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
1 change: 0 additions & 1 deletion test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ce80500

Please sign in to comment.