Skip to content

Commit

Permalink
fetch cache: revalidate should take precedence over force-dynamic (ve…
Browse files Browse the repository at this point in the history
…rcel#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.
  • Loading branch information
ztanner authored Nov 6, 2024
1 parent 3be4ec6 commit 19f93c8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export function createPatchedFetcher(
const noFetchConfigAndForceDynamic =
!pageFetchCacheMode &&
!currentFetchCacheConfig &&
!currentFetchRevalidate &&
workStore.forceDynamic

if (
Expand Down
25 changes: 25 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 @@ -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 () => {
const $ = await next.render$('/force-dynamic-fetch-cache/default-cache')
const initData = $('#data').text()
Expand Down Expand Up @@ -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/route/route.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_client-reference-manifest.js",
"force-dynamic-fetch-cache/with-fetch-cache/route/route.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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>
}

0 comments on commit 19f93c8

Please sign in to comment.