Unverified Commit dca6516c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix fetch with no-store inside of use cache (#71754)

parent 82196b8e
...@@ -304,16 +304,20 @@ export function createPatchedFetcher( ...@@ -304,16 +304,20 @@ export function createPatchedFetcher(
if (currentFetchCacheConfig === 'force-cache') { if (currentFetchCacheConfig === 'force-cache') {
currentFetchRevalidate = false currentFetchRevalidate = false
} else if ( } else if (
currentFetchCacheConfig === 'no-cache' || // if we are inside of "use cache"/"unstable_cache"
currentFetchCacheConfig === 'no-store' || // we shouldn't set the revalidate to 0 as it's overridden
pageFetchCacheMode === 'force-no-store' || // by the cache context
pageFetchCacheMode === 'only-no-store' || workUnitStore?.type !== 'cache' &&
// If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set, (currentFetchCacheConfig === 'no-cache' ||
// we shouldn't consider caching the fetch. This is because the `dynamic` cache currentFetchCacheConfig === 'no-store' ||
// is considered a "top-level" cache mode, whereas something like `fetchCache` is more pageFetchCacheMode === 'force-no-store' ||
// fine-grained. Top-level modes are responsible for setting reasonable defaults for the pageFetchCacheMode === 'only-no-store' ||
// other configurations. // If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set,
(!pageFetchCacheMode && workStore.forceDynamic) // we shouldn't consider caching the fetch. This is because the `dynamic` cache
// is considered a "top-level" cache mode, whereas something like `fetchCache` is more
// fine-grained. Top-level modes are responsible for setting reasonable defaults for the
// other configurations.
(!pageFetchCacheMode && workStore.forceDynamic))
) { ) {
currentFetchRevalidate = 0 currentFetchRevalidate = 0
} }
......
import React from 'react'
async function getData() {
'use cache'
return fetch('https://next-data-api-endpoint.vercel.app/api/random', {
cache: 'no-store',
}).then((res) => res.text())
}
export default async function Page() {
return (
<>
<p>index page</p>
<p id="random">{await getData()}</p>
</>
)
}
...@@ -266,4 +266,13 @@ describe('use-cache', () => { ...@@ -266,4 +266,13 @@ describe('use-cache', () => {
// const time4 = await browser.waitForElementByCss('#t').text() // const time4 = await browser.waitForElementByCss('#t').text()
// expect(time4).toBe(time3); // expect(time4).toBe(time3);
}) })
it('should override fetch with no-store in use cache properly', async () => {
const browser = await next.browser('/cache-fetch-no-store')
const initialValue = await browser.elementByCss('#random').text()
await browser.refresh()
expect(await browser.elementByCss('#random').text()).toBe(initialValue)
})
}) })
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