Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetch caching inside of "use cache" #71793

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should delete this once the warmup render completes. deleting it after getting a dev response is strange because we stream in dev so the overlap for concurrent requests tot he same URL is more ambiguous and racy


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
Loading