Skip to content

Commit

Permalink
Fix DynamicServerError not being thrown in fetch (#64511)
Browse files Browse the repository at this point in the history
This ensures we properly skip calling a fetch during build-time that has
`cache: 'no-store'` as it should only be called during runtime instead.

Fixes: #64462

Closes NEXT-3114
  • Loading branch information
ijjk authored and ztanner committed Apr 17, 2024
1 parent 2072327 commit 6c31a2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ function createPatchedFetcher(
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}

const hasNextConfig = 'next' in init
Expand Down Expand Up @@ -689,6 +690,7 @@ function createPatchedFetcher(
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}

if (!staticGenerationStore.forceStatic || next.revalidate !== 0) {
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ createNextDescribe(
"force-dynamic-no-prerender/[id]/page_client-reference-manifest.js",
"force-dynamic-prerender/[slug]/page.js",
"force-dynamic-prerender/[slug]/page_client-reference-manifest.js",
"force-no-store-bailout/page.js",
"force-no-store-bailout/page_client-reference-manifest.js",
"force-no-store/page.js",
"force-no-store/page_client-reference-manifest.js",
"force-static-fetch-no-store.html",
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/app-dir/app-static/app/force-no-store-bailout/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const fetchCache = 'force-no-store'

export default async function Page() {
// this should not be invoked during build as
// no-store should have it bail out
await fetch('https://non-existent', {
cache: 'no-store',
})

return (
<>
<p id="page">/force-no-store-bailout</p>
<p id="now">{Date.now()}</p>
</>
)
}

0 comments on commit 6c31a2d

Please sign in to comment.