Skip to content

Commit

Permalink
Fix fetch caching inside of "use cache" (vercel#71793)
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable authored and stipsan committed Nov 6, 2024
1 parent 7a1b85c commit cbc6910
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/app-dir/use-cache/app/cache-fetch/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').then(
(res) => res.text()
)
}

export default async function Page() {
return (
<>
<p>index page</p>
<p id="random">{await getData()}</p>
</>
)
}
18 changes: 18 additions & 0 deletions test/e2e/app-dir/use-cache/app/fetch-revalidate/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', {
next: { revalidate: 0 },
}).then((res) => res.text())
}

export default async function Page() {
return (
<>
<p>index page</p>
<p id="random">{await getData()}</p>
</>
)
}
18 changes: 18 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 @@ -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')

Expand Down

0 comments on commit cbc6910

Please sign in to comment.