Unverified Commit b0ae0b28 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Respect `no-store` request header for `"use cache"` entries in dev mode (#75754)

parent 8daa66b6
......@@ -73,6 +73,7 @@ export interface WorkStore {
rootParams: Params
dynamicIOEnabled: boolean
dev: boolean
}
export type WorkAsyncStorage = AsyncLocalStorage<WorkStore>
......
......@@ -163,6 +163,7 @@ export type UseCacheStore = {
readonly hmrRefreshHash: string | undefined
readonly isHmrRefresh: boolean
readonly serverComponentsHmrCache: ServerComponentsHmrCache | undefined
readonly forceRevalidate: boolean
} & PhasePartial
export type UnstableCacheStore = {
......
......@@ -60,6 +60,7 @@ export type WorkStoreContext = {
| 'nextExport'
| 'isDraftMode'
| 'isDebugDynamicAccesses'
| 'dev'
> &
RequestLifecycleOpts &
Partial<Pick<RenderOpts, 'reactLoadableManifest'>>
......@@ -127,6 +128,7 @@ export function createWorkStore({
afterContext: createAfterContext(renderOpts),
dynamicIOEnabled: renderOpts.experimental.dynamicIO,
dev: renderOpts.dev ?? false,
}
// TODO: remove this when we resolve accessing the store outside the execution context
......
......@@ -168,6 +168,7 @@ function generateCacheEntryWithCacheContext(
hmrRefreshHash: outerWorkUnitStore && getHmrRefreshHash(outerWorkUnitStore),
isHmrRefresh: useCacheOrRequestStore?.isHmrRefresh ?? false,
serverComponentsHmrCache: useCacheOrRequestStore?.serverComponentsHmrCache,
forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore),
}
return workUnitAsyncStorage.run(
......@@ -697,10 +698,13 @@ export function cache(
workUnitStore === undefined || workUnitStore.type === 'unstable-cache'
? []
: workUnitStore.implicitTags
const entry: undefined | CacheEntry = await cacheHandler.get(
serializedCacheKey,
implicitTags
)
const forceRevalidate = shouldForceRevalidate(workStore, workUnitStore)
const entry = forceRevalidate
? undefined
: await cacheHandler.get(serializedCacheKey, implicitTags)
const currentTime = performance.timeOrigin + performance.now()
if (
workUnitStore !== undefined &&
......@@ -901,3 +905,20 @@ function isPageComponent(
(props as UseCachePageComponentProps).$$isPageComponent
)
}
function shouldForceRevalidate(
workStore: WorkStore,
workUnitStore: WorkUnitStore | undefined
): boolean {
if (workStore.dev && workUnitStore) {
if (workUnitStore.type === 'request') {
return workUnitStore.headers.get('cache-control') === 'no-cache'
}
if (workUnitStore.type === 'cache') {
return workUnitStore.forceRevalidate
}
}
return false
}
async function getRandomValue() {
'use cache'
return Math.random()
}
async function getData() {
'use cache'
return fetch('https://next-data-api-endpoint.vercel.app/api/random').then(
(res) => res.text().then((text) => [text, 'foo', Math.random()] as const)
(res) =>
res
.text()
.then(async (text) => [text, 'foo', await getRandomValue()] as const)
)
}
......@@ -10,10 +19,10 @@ export default async function Page() {
const [fetchedRandom, text, mathRandom] = await getData()
return (
<>
<div id="container">
<p id="fetchedRandom">{fetchedRandom}</p>
<p id="text">{text}</p>
<p id="mathRandom">{mathRandom}</p>
</>
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('use-cache-hmr', () => {
describe('use-cache-dev', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
})
......@@ -102,6 +102,37 @@ describe('use-cache-hmr', () => {
)
})
it('should return cached data after reload', async () => {
let $ = await next.render$('/')
const initialContent = $('#container').text()
$ = await next.render$('/')
const content = $('#container').text()
expect(content).toEqual(initialContent)
})
it('should return fresh data after hard reload', async () => {
let $ = await next.render$('/')
const initialContent = $('#container').text()
$ = await next.render$(
'/',
{},
{ headers: { 'cache-control': 'no-cache' } }
)
const hardReloadContent = $('#container').text()
expect(hardReloadContent).not.toEqual(initialContent)
// After a subsequent soft reload, cached data from the hard reload should
// be returned.
const softReloadContent = $('#container').text()
expect(softReloadContent).toEqual(hardReloadContent)
})
it('should successfully finish compilation when "use cache" directive is added/removed', async () => {
await next.browser('/')
let cliOutputLength = next.cliOutput.length
......
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