Unverified Commit 89ca693c authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Derive foreground cache revalidation from the consumer (#96731)

## Summary

Foreground revalidation is necessary when a stale result will be
persisted by another server cache, not only when the request is
prerendering. Otherwise, a cache created during a dynamic request can
consume a stale inner value and extend its lifetime.

- derive foreground revalidation from whether the immediate work-unit
consumer will persist the result in a server cache
- model that capability as willConsumerServerCache rather than
inheriting it through the full outer scope chain
- treat server cache and static prerender work units as server-caching
consumers, while runtime prerenders are not
- add regression coverage for an outer "use cache" scope consuming a
stale unstable_cache entry

<!-- NEXT_JS_LLM -->
parent 40680b95
...@@ -342,11 +342,11 @@ export type StaticPrerenderStore = Exclude< ...@@ -342,11 +342,11 @@ export type StaticPrerenderStore = Exclude<
export interface CommonCacheStore export interface CommonCacheStore
extends Omit<CommonWorkUnitStore, 'implicitTags'> { extends Omit<CommonWorkUnitStore, 'implicitTags'> {
/** /**
* Whether a stale cache entry must be revalidated before the outer work unit * Whether this work unit will persist the results it consumes in a server
* can continue. This is copied from the outer work unit because entering a * cache. This only describes the immediate consumer; it is not inherited
* cache scope intentionally hides that store. * from outer scopes.
*/ */
readonly shouldRevalidateStaleCacheEntryInForeground: boolean readonly consumerWillServerCache: boolean
/** /**
* A cache work unit store might not always have an outer work unit store, * A cache work unit store might not always have an outer work unit store,
* from which implicit tags could be inherited. * from which implicit tags could be inherited.
...@@ -445,7 +445,7 @@ export type WorkUnitStore = ...@@ -445,7 +445,7 @@ export type WorkUnitStore =
| PrerenderStore | PrerenderStore
| GenerateStaticParamsStore | GenerateStaticParamsStore
export function shouldRevalidateStaleCacheEntryInForeground( export function willConsumerServerCache(
workUnitStore: WorkUnitStore | undefined workUnitStore: WorkUnitStore | undefined
): boolean { ): boolean {
if (!workUnitStore) { if (!workUnitStore) {
...@@ -456,7 +456,7 @@ export function shouldRevalidateStaleCacheEntryInForeground( ...@@ -456,7 +456,7 @@ export function shouldRevalidateStaleCacheEntryInForeground(
case 'cache': case 'cache':
case 'private-cache': case 'private-cache':
case 'unstable-cache': case 'unstable-cache':
return workUnitStore.shouldRevalidateStaleCacheEntryInForeground return workUnitStore.consumerWillServerCache
case 'prerender': case 'prerender':
case 'prerender-client': case 'prerender-client':
case 'prerender-ppr': case 'prerender-ppr':
......
...@@ -22,10 +22,10 @@ import type { FetchMetric } from '../base-http' ...@@ -22,10 +22,10 @@ import type { FetchMetric } from '../base-http'
import { createDedupeFetch } from './dedupe-fetch' import { createDedupeFetch } from './dedupe-fetch'
import { import {
getCacheSignal, getCacheSignal,
shouldRevalidateStaleCacheEntryInForeground,
type RevalidateStore, type RevalidateStore,
type WorkUnitAsyncStorage, type WorkUnitAsyncStorage,
type WorkUnitStore, type WorkUnitStore,
willConsumerServerCache,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { import {
CachedRouteKind, CachedRouteKind,
...@@ -1120,12 +1120,9 @@ export function createPatchedFetcher( ...@@ -1120,12 +1120,9 @@ export function createPatchedFetcher(
} }
if (entry?.value && entry.value.kind === CachedRouteKind.FETCH) { if (entry?.value && entry.value.kind === CachedRouteKind.FETCH) {
// when stale and is revalidating we wait for fresh data // If the consumer will persist this result in a server cache,
// so the revalidated entry has the updated data // wait for fresh data so it doesn't persist a stale value.
if ( if (willConsumerServerCache(workUnitStore) && entry.isStale) {
shouldRevalidateStaleCacheEntryInForeground(workUnitStore) &&
entry.isStale
) {
isForegroundRevalidate = true isForegroundRevalidate = true
} else { } else {
if (entry.isStale) { if (entry.isStale) {
......
...@@ -34,7 +34,7 @@ import { ...@@ -34,7 +34,7 @@ import {
getCacheSignal, getCacheSignal,
isHmrRefresh, isHmrRefresh,
getServerComponentsHmrCache, getServerComponentsHmrCache,
shouldRevalidateStaleCacheEntryInForeground, willConsumerServerCache,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { import {
...@@ -753,8 +753,7 @@ function createUseCacheStore( ...@@ -753,8 +753,7 @@ function createUseCacheStore(
return { return {
type: 'private-cache', type: 'private-cache',
phase: 'render', phase: 'render',
shouldRevalidateStaleCacheEntryInForeground: consumerWillServerCache: true,
shouldRevalidateStaleCacheEntryInForeground(outerWorkUnitStore),
implicitTags: outerWorkUnitStore?.implicitTags, implicitTags: outerWorkUnitStore?.implicitTags,
revalidate: defaultCacheLife.revalidate, revalidate: defaultCacheLife.revalidate,
expire: defaultCacheLife.expire, expire: defaultCacheLife.expire,
...@@ -804,8 +803,7 @@ function createUseCacheStore( ...@@ -804,8 +803,7 @@ function createUseCacheStore(
return { return {
type: 'cache', type: 'cache',
phase: 'render', phase: 'render',
shouldRevalidateStaleCacheEntryInForeground: consumerWillServerCache: true,
shouldRevalidateStaleCacheEntryInForeground(outerWorkUnitStore),
implicitTags: outerWorkUnitStore.implicitTags, implicitTags: outerWorkUnitStore.implicitTags,
revalidate: defaultCacheLife.revalidate, revalidate: defaultCacheLife.revalidate,
expire: defaultCacheLife.expire, expire: defaultCacheLife.expire,
...@@ -3255,15 +3253,14 @@ export async function cache( ...@@ -3255,15 +3253,14 @@ export async function cache(
? Math.max(entry.expire, MIN_PRERENDERABLE_EXPIRE) ? Math.max(entry.expire, MIN_PRERENDERABLE_EXPIRE)
: entry.expire) * : entry.expire) *
1000 || 1000 ||
(shouldRevalidateStaleCacheEntryInForeground(workUnitStore) && (willConsumerServerCache(workUnitStore) &&
currentTime > entry.timestamp + entry.revalidate * 1000) currentTime > entry.timestamp + entry.revalidate * 1000)
) { ) {
// Miss. Generate a new result. // Miss. Generate a new result.
// If the cache entry is stale and the outer work unit requires // If the cache entry is stale and its consumer will persist the
// foreground revalidation, don't use the stale entry since it would // result in a server cache, don't persist the stale entry into a new
// unnecessarily shorten the lifetime of the generated result. We're // cache. We're not time constrained here, so regenerate it now.
// not time constrained here, so regenerate it now.
// We need to run this inside a clean AsyncLocalStorage snapshot so // We need to run this inside a clean AsyncLocalStorage snapshot so
// that the cache generation cannot read anything from the context // that the cache generation cannot read anything from the context
...@@ -3279,7 +3276,7 @@ export async function cache( ...@@ -3279,7 +3276,7 @@ export async function cache(
} }
if ( if (
shouldRevalidateStaleCacheEntryInForeground(workUnitStore) && willConsumerServerCache(workUnitStore) &&
currentTime > entry.timestamp + entry.revalidate * 1000 currentTime > entry.timestamp + entry.revalidate * 1000
) { ) {
debug?.( debug?.(
......
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
import { import {
getCacheSignal, getCacheSignal,
getDraftModeProviderForCacheScope, getDraftModeProviderForCacheScope,
shouldRevalidateStaleCacheEntryInForeground, willConsumerServerCache,
workUnitAsyncStorage, workUnitAsyncStorage,
} from '../../app-render/work-unit-async-storage.external' } from '../../app-render/work-unit-async-storage.external'
import { import {
...@@ -147,8 +147,7 @@ export function unstable_cache<T extends Callback>( ...@@ -147,8 +147,7 @@ export function unstable_cache<T extends Callback>(
const innerCacheStore: UnstableCacheStore = { const innerCacheStore: UnstableCacheStore = {
type: 'unstable-cache', type: 'unstable-cache',
phase: 'render', phase: 'render',
shouldRevalidateStaleCacheEntryInForeground: consumerWillServerCache: true,
shouldRevalidateStaleCacheEntryInForeground(workUnitStore),
implicitTags, implicitTags,
draftMode: draftMode:
workUnitStore && workUnitStore &&
...@@ -283,9 +282,7 @@ export function unstable_cache<T extends Callback>( ...@@ -283,9 +282,7 @@ export function unstable_cache<T extends Callback>(
// Attach the empty catch here so we don't get a "unhandled promise // Attach the empty catch here so we don't get a "unhandled promise
// rejection" warning. (Behavior is matched with patch-fetch) // rejection" warning. (Behavior is matched with patch-fetch)
if ( if (willConsumerServerCache(workUnitStore)) {
shouldRevalidateStaleCacheEntryInForeground(workUnitStore)
) {
revalidationPromise.catch(() => {}) revalidationPromise.catch(() => {})
} }
...@@ -294,15 +291,14 @@ export function unstable_cache<T extends Callback>( ...@@ -294,15 +291,14 @@ export function unstable_cache<T extends Callback>(
} }
// Check if we need to do foreground revalidation // Check if we need to do foreground revalidation
if ( if (willConsumerServerCache(workUnitStore)) {
shouldRevalidateStaleCacheEntryInForeground(workUnitStore) // When the consumer will persist this result in a server
) { // cache, wait for fresh data so it doesn't persist a stale
// When the page is revalidating and the cache entry is stale, // value. The `await` here also keeps `cacheSignal.endRead` (in
// we need to wait for fresh data (blocking revalidate). The // the outer `finally`) suspended until the recompute +
// `await` here keeps `cacheSignal.endRead` (in the outer // cacheNewResult actually complete, so a prospective
// `finally`) suspended until the recompute + cacheNewResult // prerender's `cacheSignal` doesn't resolve `cacheReady`
// actually complete, so the prospective prerender's // prematurely.
// `cacheSignal` doesn't resolve `cacheReady` prematurely.
return await workStore.pendingRevalidates[invocationKey] return await workStore.pendingRevalidates[invocationKey]
} }
// Otherwise, we're doing background revalidation - return stale immediately // Otherwise, we're doing background revalidation - return stale immediately
......
import { cacheLife, unstable_cache } from 'next/cache'
import { connection } from 'next/server'
const getInnerValue = unstable_cache(
async () => Date.now(),
['cache-consumer-foreground-revalidate-inner'],
{
revalidate: false,
tags: ['cache-consumer-foreground-revalidate-inner'],
}
)
async function getOuterValue(key: string) {
'use cache'
cacheLife({ revalidate: 60, expire: 300 })
return {
key,
innerValue: await getInnerValue(),
}
}
export default async function Page({
params,
}: {
params: Promise<{ key: string }>
}) {
await connection()
const { key } = await params
const value = await getOuterValue(key)
return <p id="inner-value">{value.innerValue}</p>
}
import { revalidateTag } from 'next/cache'
export async function POST() {
revalidateTag('cache-consumer-foreground-revalidate-inner', 'max')
return new Response(null, { status: 204 })
}
...@@ -433,6 +433,31 @@ describe('use-cache', () => { ...@@ -433,6 +433,31 @@ describe('use-cache', () => {
}) })
}) })
it('should refresh a stale cache before storing it in an outer cache', async () => {
const browser = await next.browser(
'/cache-consumer-foreground-revalidate/first'
)
const firstValue = await browser.elementById('inner-value').text()
const response = await next.fetch('/api/revalidate-cache-consumer', {
method: 'POST',
})
expect(response.status).toBe(204)
// Use a different outer key to force a cache miss. Because that outer
// scope will cache what it consumes, it must wait for the stale inner
// entry to revalidate instead of persisting the stale value.
await browser.loadPage(
new URL(
'/cache-consumer-foreground-revalidate/second',
next.url
).toString()
)
const secondValue = await browser.elementById('inner-value').text()
expect(secondValue).not.toBe(firstValue)
})
it('should revalidate caches during on-demand revalidation', async () => { it('should revalidate caches during on-demand revalidation', async () => {
const browser = await next.browser('/on-demand-revalidate') const browser = await next.browser('/on-demand-revalidate')
const initial = await browser.elementById('value').text() const initial = await browser.elementById('value').text()
......
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