Unverified Commit 19f93c89 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fetch cache: revalidate should take precedence over force-dynamic (#72357)

`force-dynamic` will automatically disable caching on a fetch. If `force-dynamic` is set, and there's a `revalidate` value on the fetch, the revalidate value will be a no-op unless you explicitly opt into caching via `cache: 'force-cache'`. This changes the behavior to favor a positive revalidate value over `force-dynamic` when deciding to cache the fetch.
parent 3be4ec65
...@@ -331,6 +331,7 @@ export function createPatchedFetcher( ...@@ -331,6 +331,7 @@ export function createPatchedFetcher(
const noFetchConfigAndForceDynamic = const noFetchConfigAndForceDynamic =
!pageFetchCacheMode && !pageFetchCacheMode &&
!currentFetchCacheConfig && !currentFetchCacheConfig &&
!currentFetchRevalidate &&
workStore.forceDynamic workStore.forceDynamic
if ( if (
......
...@@ -211,6 +211,29 @@ describe('app-dir static/dynamic handling', () => { ...@@ -211,6 +211,29 @@ describe('app-dir static/dynamic handling', () => {
}) })
}) })
it('should infer a fetch cache of "force-cache" when force-dynamic is used on a fetch with revalidate', async () => {
let currentData: string | undefined
await retry(async () => {
const $ = await next.render$('/force-dynamic-fetch-cache/revalidate')
const initialData = $('#data').text()
expect($('#data').text()).toBeTruthy()
const $2 = await next.render$('/force-dynamic-fetch-cache/revalidate')
currentData = $2('#data').text()
expect(currentData).toBeTruthy()
expect(currentData).toBe(initialData)
})
// wait for revalidation
await waitFor(3000)
await retry(async () => {
const $3 = await next.render$('/force-dynamic-fetch-cache/revalidate')
const finalValue = $3('#data').text()
expect(finalValue).toBeTruthy()
expect(finalValue).not.toBe(currentData)
})
})
it('force-dynamic should supercede a "default" cache value', async () => { it('force-dynamic should supercede a "default" cache value', async () => {
const $ = await next.render$('/force-dynamic-fetch-cache/default-cache') const $ = await next.render$('/force-dynamic-fetch-cache/default-cache')
const initData = $('#data').text() const initData = $('#data').text()
...@@ -857,6 +880,8 @@ describe('app-dir static/dynamic handling', () => { ...@@ -857,6 +880,8 @@ describe('app-dir static/dynamic handling', () => {
"force-dynamic-fetch-cache/no-fetch-cache/page_client-reference-manifest.js", "force-dynamic-fetch-cache/no-fetch-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/no-fetch-cache/route/route.js", "force-dynamic-fetch-cache/no-fetch-cache/route/route.js",
"force-dynamic-fetch-cache/no-fetch-cache/route/route_client-reference-manifest.js", "force-dynamic-fetch-cache/no-fetch-cache/route/route_client-reference-manifest.js",
"force-dynamic-fetch-cache/revalidate/page.js",
"force-dynamic-fetch-cache/revalidate/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/with-fetch-cache/page.js", "force-dynamic-fetch-cache/with-fetch-cache/page.js",
"force-dynamic-fetch-cache/with-fetch-cache/page_client-reference-manifest.js", "force-dynamic-fetch-cache/with-fetch-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/with-fetch-cache/route/route.js", "force-dynamic-fetch-cache/with-fetch-cache/route/route.js",
......
export const dynamic = 'force-dynamic'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
next: {
revalidate: 3,
},
}
).then((res) => res.text())
return (
<>
<p id="page">/force-dynamic-fetch-cache/revalidate</p>
<p id="data">{data}</p>
</>
)
}
export const dynamic = 'force-dynamic'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?revalidate-3',
{
next: {
revalidate: 3,
},
}
).then((res) => res.text())
return <div>Hello World! {data}</div>
}
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