Unverified Commit 95fe24f6 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix force-static and fetch no-store cases (#59549)

This ensures that `export const dynamic = 'force-static'` is properly
honored when a page contains fetches with `cache: 'no-store'`, `cache:
'no-cache'` or `next: { revalidate: 0 }`.

Closes NEXT-1858
parent e9757175
...@@ -201,6 +201,7 @@ export async function createComponentTree({ ...@@ -201,6 +201,7 @@ export async function createComponentTree({
} }
if ( if (
!staticGenerationStore.forceStatic &&
staticGenerationStore.isStaticGeneration && staticGenerationStore.isStaticGeneration &&
ctx.defaultRevalidate === 0 && ctx.defaultRevalidate === 0 &&
// If the postpone API isn't available, we can't postpone the render and // If the postpone API isn't available, we can't postpone the render and
......
...@@ -368,6 +368,9 @@ export function patchFetch({ ...@@ -368,6 +368,9 @@ export function patchFetch({
} }
if ( if (
// when force static is configured we don't bail from
// `revalidate: 0` values
!(staticGenerationStore.forceStatic && revalidate === 0) &&
// we don't consider autoNoCache to switch to dynamic during // we don't consider autoNoCache to switch to dynamic during
// revalidate although if it occurs during build we do // revalidate although if it occurs during build we do
!autoNoCache && !autoNoCache &&
...@@ -591,7 +594,7 @@ export function patchFetch({ ...@@ -591,7 +594,7 @@ export function patchFetch({
// Delete `cache` property as Cloudflare Workers will throw an error // Delete `cache` property as Cloudflare Workers will throw an error
if (isEdgeRuntime) delete init.cache if (isEdgeRuntime) delete init.cache
if (cache === 'no-store') { if (!staticGenerationStore.forceStatic && cache === 'no-store') {
const dynamicUsageReason = `no-store fetch ${input}${ const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.urlPathname staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}` ? ` ${staticGenerationStore.urlPathname}`
...@@ -618,9 +621,11 @@ export function patchFetch({ ...@@ -618,9 +621,11 @@ export function patchFetch({
(typeof staticGenerationStore.revalidate === 'number' && (typeof staticGenerationStore.revalidate === 'number' &&
next.revalidate < staticGenerationStore.revalidate)) next.revalidate < staticGenerationStore.revalidate))
) { ) {
const forceDynamic = staticGenerationStore.forceDynamic if (
!staticGenerationStore.forceDynamic &&
if (!forceDynamic && next.revalidate === 0) { !staticGenerationStore.forceStatic &&
next.revalidate === 0
) {
const dynamicUsageReason = `revalidate: 0 fetch ${input}${ const dynamicUsageReason = `revalidate: 0 fetch ${input}${
staticGenerationStore.urlPathname staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}` ? ` ${staticGenerationStore.urlPathname}`
...@@ -635,11 +640,10 @@ export function patchFetch({ ...@@ -635,11 +640,10 @@ export function patchFetch({
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
} }
if (!forceDynamic || next.revalidate !== 0) { if (!staticGenerationStore.forceStatic || next.revalidate !== 0) {
staticGenerationStore.revalidate = next.revalidate staticGenerationStore.revalidate = next.revalidate
} }
} }
if (hasNextConfig) delete init.next if (hasNextConfig) delete init.next
} }
......
...@@ -45,6 +45,14 @@ createNextDescribe( ...@@ -45,6 +45,14 @@ createNextDescribe(
'unstable_cache_tag1' 'unstable_cache_tag1'
) )
}) })
if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should honor force-static with fetch cache: no-store correctly', async () => {
const res = await next.fetch('/force-static-fetch-no-store')
expect(res.status).toBe(200)
expect(res.headers.get('x-nextjs-cache').toLowerCase()).toBe('hit')
})
}
} }
it('should correctly include headers instance in cache key', async () => { it('should correctly include headers instance in cache key', async () => {
...@@ -519,6 +527,10 @@ createNextDescribe( ...@@ -519,6 +527,10 @@ createNextDescribe(
'default-cache/page.js', 'default-cache/page.js',
'fetch-no-cache/page.js', 'fetch-no-cache/page.js',
'force-no-store/page.js', 'force-no-store/page.js',
'force-static-fetch-no-store.html',
'force-static-fetch-no-store.rsc',
'force-static-fetch-no-store/page.js',
'force-static-fetch-no-store/page_client-reference-manifest.js',
'force-static/first.rsc', 'force-static/first.rsc',
'api/draft-mode/route.js', 'api/draft-mode/route.js',
'blog/tim/first-post.rsc', 'blog/tim/first-post.rsc',
...@@ -902,6 +914,22 @@ createNextDescribe( ...@@ -902,6 +914,22 @@ createNextDescribe(
"initialRevalidateSeconds": 3, "initialRevalidateSeconds": 3,
"srcRoute": "/force-cache", "srcRoute": "/force-cache",
}, },
"/force-static-fetch-no-store": {
"dataRoute": "/force-static-fetch-no-store.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data",
},
],
"initialRevalidateSeconds": false,
"srcRoute": "/force-static-fetch-no-store",
},
"/force-static/first": { "/force-static/first": {
"dataRoute": "/force-static/first.rsc", "dataRoute": "/force-static/first.rsc",
"experimentalBypassFor": [ "experimentalBypassFor": [
......
export const dynamic = 'force-static'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page',
{
cache: 'no-store',
}
).then((res) => res.text())
const data2 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page',
{
next: {
revalidate: 0,
},
}
).then((res) => res.text())
const data3 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page',
{
cache: 'no-cache',
}
).then((res) => res.text())
return (
<>
<p id="page">/force-static-fetch-no-store</p>
<p id="page-data">{data}</p>
<p id="page-data2">{data2}</p>
<p id="page-data3">{data3}</p>
<p id="now">{Date.now()}</p>
</>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment