From 4024a89ee830cec6ab451ba7871ae078d51a9757 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 15 Apr 2024 10:09:57 -0700 Subject: [PATCH] Fix DynamicServerError not being thrown in fetch (#64511) 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: https://github.com/vercel/next.js/issues/64462 Closes NEXT-3114 --- packages/next/src/server/lib/patch-fetch.ts | 2 ++ test/e2e/app-dir/app-static/app-static.test.ts | 2 ++ .../app/force-no-store-bailout/page.js | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 test/e2e/app-dir/app-static/app/force-no-store-bailout/page.js diff --git a/packages/next/src/server/lib/patch-fetch.ts b/packages/next/src/server/lib/patch-fetch.ts index e5318dc371796..5bc3e58740d61 100644 --- a/packages/next/src/server/lib/patch-fetch.ts +++ b/packages/next/src/server/lib/patch-fetch.ts @@ -668,6 +668,7 @@ function createPatchedFetcher( const err = new DynamicServerError(dynamicUsageReason) staticGenerationStore.dynamicUsageErr = err staticGenerationStore.dynamicUsageDescription = dynamicUsageReason + throw err } const hasNextConfig = 'next' in init @@ -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) { 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 b19aee58b71ec..2f6905b50d661 100644 --- a/test/e2e/app-dir/app-static/app-static.test.ts +++ b/test/e2e/app-dir/app-static/app-static.test.ts @@ -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", diff --git a/test/e2e/app-dir/app-static/app/force-no-store-bailout/page.js b/test/e2e/app-dir/app-static/app/force-no-store-bailout/page.js new file mode 100644 index 0000000000000..77d9320a8f9ea --- /dev/null +++ b/test/e2e/app-dir/app-static/app/force-no-store-bailout/page.js @@ -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 ( + <> +

/force-no-store-bailout

+

{Date.now()}

+ + ) +}