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