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,7 +304,11 @@ export function createPatchedFetcher( ...@@ -304,7 +304,11 @@ 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"
// we shouldn't set the revalidate to 0 as it's overridden
// by the cache context
workUnitStore?.type !== 'cache' &&
(currentFetchCacheConfig === 'no-cache' ||
currentFetchCacheConfig === 'no-store' || currentFetchCacheConfig === 'no-store' ||
pageFetchCacheMode === 'force-no-store' || pageFetchCacheMode === 'force-no-store' ||
pageFetchCacheMode === 'only-no-store' || pageFetchCacheMode === 'only-no-store' ||
...@@ -313,7 +317,7 @@ export function createPatchedFetcher( ...@@ -313,7 +317,7 @@ export function createPatchedFetcher(
// is considered a "top-level" cache mode, whereas something like `fetchCache` is more // 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 // fine-grained. Top-level modes are responsible for setting reasonable defaults for the
// other configurations. // other configurations.
(!pageFetchCacheMode && workStore.forceDynamic) (!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