Skip to content

Commit

Permalink
positive revalidate values should override force-dynamic in fetch cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Nov 5, 2024
1 parent 8f603ae commit 8c6d427
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 8c6d427

Please sign in to comment.