Skip to content

Commit

Permalink
Fix fetch with no-store inside of use cache (#71754)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored Oct 23, 2024
1 parent 82196b8 commit dca6516
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,20 @@ export function createPatchedFetcher(
if (currentFetchCacheConfig === 'force-cache') {
currentFetchRevalidate = false
} else if (
currentFetchCacheConfig === 'no-cache' ||
currentFetchCacheConfig === 'no-store' ||
pageFetchCacheMode === 'force-no-store' ||
pageFetchCacheMode === 'only-no-store' ||
// If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set,
// 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)
// 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' ||
pageFetchCacheMode === 'force-no-store' ||
pageFetchCacheMode === 'only-no-store' ||
// If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set,
// 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
}
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/app-dir/use-cache/app/cache-fetch-no-store/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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>
</>
)
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,13 @@ describe('use-cache', () => {
// const time4 = await browser.waitForElementByCss('#t').text()
// 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)
})
})

0 comments on commit dca6516

Please sign in to comment.