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 committed Apr 15, 2024
1 parent c325ecd commit 4024a89
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 @@ -668,6 +668,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 @@ -695,6 +696,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 @@ -703,6 +703,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 4024a89

Please sign in to comment.