Unverified Commit f9b281a8 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix fetch caching inside of `"use cache"` (#71793)

parent bf5ffaf2
......@@ -3103,9 +3103,15 @@ export default abstract class Server<
)
}
return runWithCacheScope({ cache }, () =>
const response = runWithCacheScope({ cache }, () =>
originalResponseGenerator(state)
)
// Clear the prefetch cache to ensure a clean slate for the next
// request.
this.prefetchCacheScopesDev.del(urlPathname)
return response
}
return originalResponseGenerator(state)
......
......@@ -278,11 +278,15 @@ export function createPatchedFetcher(
? []
: workUnitStore.implicitTags
// Inside unstable-cache we treat it the same as force-no-store on the page.
// Inside unstable-cache or "use cache", we treat it the same as
// force-no-store on the page.
const pageFetchCacheMode =
workUnitStore && workUnitStore.type === 'unstable-cache'
workUnitStore &&
(workUnitStore.type === 'unstable-cache' ||
workUnitStore.type === 'cache')
? 'force-no-store'
: workStore.fetchCache
const isUsingNoStore = !!workStore.isUnstableNoStore
let currentFetchCacheConfig = getRequestMeta('cache')
......
import React from 'react'
async function getData() {
'use cache'
return fetch('https://next-data-api-endpoint.vercel.app/api/random').then(
(res) => res.text()
)
}
export default async function Page() {
return (
<>
<p>index page</p>
<p id="random">{await getData()}</p>
</>
)
}
import React from 'react'
async function getData() {
'use cache'
return fetch('https://next-data-api-endpoint.vercel.app/api/random', {
next: { revalidate: 0 },
}).then((res) => res.text())
}
export default async function Page() {
return (
<>
<p>index page</p>
<p id="random">{await getData()}</p>
</>
)
}
......@@ -267,6 +267,24 @@ describe('use-cache', () => {
// expect(time4).toBe(time3);
})
it('should use revalidate config in fetch', async () => {
const browser = await next.browser('/fetch-revalidate')
const initialValue = await browser.elementByCss('#random').text()
await browser.refresh()
expect(await browser.elementByCss('#random').text()).not.toBe(initialValue)
})
it('should cache fetch without no-store', async () => {
const browser = await next.browser('/cache-fetch')
const initialValue = await browser.elementByCss('#random').text()
await browser.refresh()
expect(await browser.elementByCss('#random').text()).toBe(initialValue)
})
it('should override fetch with no-store in use cache properly', async () => {
const browser = await next.browser('/cache-fetch-no-store')
......
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