Unverified Commit 6a1b6336 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Remove `revalidate` property from incremental cache `ctx` for `FETCH` kind (#76500)

The `revalidate` property of the `ctx` object that is passed into the
incremental cache by the patched `fetch` as well as `unstable_cache` is
unused since it was introduced in #43659. It was just added because of
how the method signature for `set()` was changed back then.

However, for those kinds of cache entries, the `revalidate` context
property is never used, and instead the `revalidate` property of the
passed-in `data` is used.

To avoid further confusion (e.g. in [this
question](https://github.com/vercel/next.js/pull/76207#discussion_r1968478141)),
this PR improves the method signatures and types of the incremental
cache so that the different call-site use cases can be clearly
discriminated, and superfluous context properties can be omitted.
parent 4eb7e23b
...@@ -649,5 +649,7 @@ ...@@ -649,5 +649,7 @@
"648": "@rspack/plugin-react-refresh is not available. Please make sure `@next/plugin-rspack` is correctly installed.", "648": "@rspack/plugin-react-refresh is not available. Please make sure `@next/plugin-rspack` is correctly installed.",
"649": "Cache handlers not initialized", "649": "Cache handlers not initialized",
"650": "experimental.nodeMiddleware", "650": "experimental.nodeMiddleware",
"651": "Unexpected module type %s" "651": "Unexpected module type %s",
"652": "Expected cached value for cache key %s not to be a %s kind, got \"FETCH\" instead.",
"653": "Expected cached value for cache key %s to be a \"FETCH\" kind, got %s instead."
} }
...@@ -19,8 +19,8 @@ import { ...@@ -19,8 +19,8 @@ import {
CachedRouteKind, CachedRouteKind,
type CachedImageValue, type CachedImageValue,
type IncrementalCacheEntry, type IncrementalCacheEntry,
type IncrementalCacheItem,
type IncrementalCacheValue, type IncrementalCacheValue,
type IncrementalResponseCacheEntry,
} from './response-cache' } from './response-cache'
import { sendEtagResponse } from './send-payload' import { sendEtagResponse } from './send-payload'
import { getContentType, getExtension } from './serve-static' import { getContentType, getExtension } from './serve-static'
...@@ -390,7 +390,7 @@ export class ImageOptimizerCache { ...@@ -390,7 +390,7 @@ export class ImageOptimizerCache {
this.nextConfig = nextConfig this.nextConfig = nextConfig
} }
async get(cacheKey: string): Promise<IncrementalCacheEntry | null> { async get(cacheKey: string): Promise<IncrementalResponseCacheEntry | null> {
try { try {
const cacheDir = join(this.cacheDir, cacheKey) const cacheDir = join(this.cacheDir, cacheKey)
const files = await promises.readdir(cacheDir) const files = await promises.readdir(cacheDir)
...@@ -414,7 +414,7 @@ export class ImageOptimizerCache { ...@@ -414,7 +414,7 @@ export class ImageOptimizerCache {
revalidateAfter: revalidateAfter:
Math.max(maxAge, this.nextConfig.images.minimumCacheTTL) * 1000 + Math.max(maxAge, this.nextConfig.images.minimumCacheTTL) * 1000 +
Date.now(), Date.now(),
curRevalidate: maxAge, revalidate: maxAge,
isStale: now > expireAt, isStale: now > expireAt,
isFallback: false, isFallback: false,
} }
...@@ -508,7 +508,7 @@ export function getMaxAge(str: string | null | undefined): number { ...@@ -508,7 +508,7 @@ export function getMaxAge(str: string | null | undefined): number {
} }
export function getPreviouslyCachedImageOrNull( export function getPreviouslyCachedImageOrNull(
upstreamImage: ImageUpstream, upstreamImage: ImageUpstream,
previousCacheEntry: IncrementalCacheItem | undefined previousCacheEntry: IncrementalCacheEntry | null | undefined
): CachedImageValue | null { ): CachedImageValue | null {
if ( if (
previousCacheEntry?.value?.kind === 'IMAGE' && previousCacheEntry?.value?.kind === 'IMAGE' &&
...@@ -678,7 +678,7 @@ export async function imageOptimizer( ...@@ -678,7 +678,7 @@ export async function imageOptimizer(
opts: { opts: {
isDev?: boolean isDev?: boolean
silent?: boolean silent?: boolean
previousCacheEntry?: IncrementalCacheItem previousCacheEntry?: IncrementalResponseCacheEntry | null
} }
): Promise<{ ): Promise<{
buffer: Buffer buffer: Buffer
...@@ -772,7 +772,7 @@ export async function imageOptimizer( ...@@ -772,7 +772,7 @@ export async function imageOptimizer(
return { return {
buffer: previouslyCachedImage.buffer, buffer: previouslyCachedImage.buffer,
contentType, contentType,
maxAge: opts?.previousCacheEntry?.curRevalidate || maxAge, maxAge: opts?.previousCacheEntry?.revalidate || maxAge,
etag: previouslyCachedImage.etag, etag: previouslyCachedImage.etag,
upstreamEtag: previouslyCachedImage.upstreamEtag, upstreamEtag: previouslyCachedImage.upstreamEtag,
} }
......
...@@ -5,6 +5,9 @@ import { ...@@ -5,6 +5,9 @@ import {
CachedRouteKind, CachedRouteKind,
IncrementalCacheKind, IncrementalCacheKind,
type CachedFetchValue, type CachedFetchValue,
type IncrementalCacheValue,
type SetIncrementalFetchCacheContext,
type SetIncrementalResponseCacheContext,
} from '../../response-cache' } from '../../response-cache'
import { LRUCache } from '../lru-cache' import { LRUCache } from '../lru-cache'
...@@ -106,12 +109,16 @@ export default class FileSystemCache implements CacheHandler { ...@@ -106,12 +109,16 @@ export default class FileSystemCache implements CacheHandler {
public async get(...args: Parameters<CacheHandler['get']>) { public async get(...args: Parameters<CacheHandler['get']>) {
const [key, ctx] = args const [key, ctx] = args
const { tags, softTags, kind, isRoutePPREnabled, isFallback } = ctx const { kind } = ctx
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
if (this.debug) { if (this.debug) {
console.log('get', key, tags, kind, !!data) if (kind === IncrementalCacheKind.FETCH) {
console.log('get', key, ctx.tags, kind, !!data)
} else {
console.log('get', key, kind, !!data)
}
} }
// let's check the disk for seed data // let's check the disk for seed data
...@@ -157,6 +164,8 @@ export default class FileSystemCache implements CacheHandler { ...@@ -157,6 +164,8 @@ export default class FileSystemCache implements CacheHandler {
const { mtime } = await this.fs.stat(filePath) const { mtime } = await this.fs.stat(filePath)
if (kind === IncrementalCacheKind.FETCH) { if (kind === IncrementalCacheKind.FETCH) {
const { tags, fetchIdx, fetchUrl } = ctx
if (!this.flushToDisk) return null if (!this.flushToDisk) return null
const lastModified = mtime.getTime() const lastModified = mtime.getTime()
...@@ -177,8 +186,10 @@ export default class FileSystemCache implements CacheHandler { ...@@ -177,8 +186,10 @@ export default class FileSystemCache implements CacheHandler {
console.log('tags vs storedTags mismatch', tags, storedTags) console.log('tags vs storedTags mismatch', tags, storedTags)
} }
await this.set(key, data.value, { await this.set(key, data.value, {
fetchCache: true,
tags, tags,
isRoutePPREnabled, fetchIdx,
fetchUrl,
}) })
} }
} }
...@@ -226,10 +237,10 @@ export default class FileSystemCache implements CacheHandler { ...@@ -226,10 +237,10 @@ export default class FileSystemCache implements CacheHandler {
} }
let rscData: Buffer | undefined let rscData: Buffer | undefined
if (!isFallback) { if (!ctx.isFallback) {
rscData = await this.fs.readFile( rscData = await this.fs.readFile(
this.getFilePath( this.getFilePath(
`${key}${isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`, `${key}${ctx.isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`,
IncrementalCacheKind.APP_PAGE IncrementalCacheKind.APP_PAGE
) )
) )
...@@ -251,7 +262,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -251,7 +262,7 @@ export default class FileSystemCache implements CacheHandler {
let meta: RouteMetadata | undefined let meta: RouteMetadata | undefined
let pageData: string | object = {} let pageData: string | object = {}
if (!isFallback) { if (!ctx.isFallback) {
pageData = JSON.parse( pageData = JSON.parse(
await this.fs.readFile( await this.fs.readFile(
this.getFilePath( this.getFilePath(
...@@ -315,7 +326,10 @@ export default class FileSystemCache implements CacheHandler { ...@@ -315,7 +326,10 @@ export default class FileSystemCache implements CacheHandler {
} }
} }
} else if (data?.value?.kind === CachedRouteKind.FETCH) { } else if (data?.value?.kind === CachedRouteKind.FETCH) {
const combinedTags = [...(tags || []), ...(softTags || [])] const combinedTags =
ctx.kind === IncrementalCacheKind.FETCH
? [...(ctx.tags || []), ...(ctx.softTags || [])]
: []
const wasRevalidated = combinedTags.some((tag) => { const wasRevalidated = combinedTags.some((tag) => {
if (this.revalidatedTags.includes(tag)) { if (this.revalidatedTags.includes(tag)) {
...@@ -338,9 +352,11 @@ export default class FileSystemCache implements CacheHandler { ...@@ -338,9 +352,11 @@ export default class FileSystemCache implements CacheHandler {
return data ?? null return data ?? null
} }
public async set(...args: Parameters<CacheHandler['set']>) { public async set(
const [key, data, ctx] = args key: string,
const { isFallback } = ctx data: IncrementalCacheValue | null,
ctx: SetIncrementalFetchCacheContext | SetIncrementalResponseCacheContext
) {
memoryCache?.set(key, { memoryCache?.set(key, {
value: data, value: data,
lastModified: Date.now(), lastModified: Date.now(),
...@@ -388,7 +404,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -388,7 +404,7 @@ export default class FileSystemCache implements CacheHandler {
writer.append(htmlPath, data.html) writer.append(htmlPath, data.html)
// Fallbacks don't generate a data file. // Fallbacks don't generate a data file.
if (!isFallback) { if (!ctx.fetchCache && !ctx.isFallback) {
writer.append( writer.append(
this.getFilePath( this.getFilePath(
`${key}${ `${key}${
...@@ -441,7 +457,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -441,7 +457,7 @@ export default class FileSystemCache implements CacheHandler {
filePath, filePath,
JSON.stringify({ JSON.stringify({
...data, ...data,
tags: ctx.tags, tags: ctx.fetchCache ? ctx.tags : [],
}) })
) )
} }
......
...@@ -6,6 +6,13 @@ import { ...@@ -6,6 +6,13 @@ import {
type IncrementalCache as IncrementalCacheType, type IncrementalCache as IncrementalCacheType,
IncrementalCacheKind, IncrementalCacheKind,
CachedRouteKind, CachedRouteKind,
type IncrementalResponseCacheEntry,
type IncrementalFetchCacheEntry,
type GetIncrementalFetchCacheContext,
type GetIncrementalResponseCacheContext,
type CachedFetchValue,
type SetIncrementalFetchCacheContext,
type SetIncrementalResponseCacheContext,
} from '../../response-cache' } from '../../response-cache'
import type { Revalidate } from '../revalidate' import type { Revalidate } from '../revalidate'
import type { DeepReadonly } from '../../../shared/lib/deep-readonly' import type { DeepReadonly } from '../../../shared/lib/deep-readonly'
...@@ -27,6 +34,7 @@ import { ...@@ -27,6 +34,7 @@ import {
getRenderResumeDataCache, getRenderResumeDataCache,
} from '../../app-render/work-unit-async-storage.external' } from '../../app-render/work-unit-async-storage.external'
import { getCacheHandlers } from '../../use-cache/handlers' import { getCacheHandlers } from '../../use-cache/handlers'
import { InvariantError } from '../../../shared/lib/invariant-error'
export interface CacheHandlerContext { export interface CacheHandlerContext {
fs?: CacheFs fs?: CacheFs
...@@ -52,13 +60,16 @@ export class CacheHandler { ...@@ -52,13 +60,16 @@ export class CacheHandler {
constructor(_ctx: CacheHandlerContext) {} constructor(_ctx: CacheHandlerContext) {}
public async get( public async get(
..._args: Parameters<IncrementalCache['get']> _cacheKey: string,
_ctx: GetIncrementalFetchCacheContext | GetIncrementalResponseCacheContext
): Promise<CacheHandlerValue | null> { ): Promise<CacheHandlerValue | null> {
return {} as any return {} as any
} }
public async set( public async set(
..._args: Parameters<IncrementalCache['set']> _cacheKey: string,
_data: IncrementalCacheValue | null,
_ctx: SetIncrementalFetchCacheContext | SetIncrementalResponseCacheContext
): Promise<void> {} ): Promise<void> {}
public async revalidateTag( public async revalidateTag(
...@@ -389,19 +400,17 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -389,19 +400,17 @@ export class IncrementalCache implements IncrementalCacheType {
} }
} }
// get data from cache if available
async get( async get(
cacheKey: string, cacheKey: string,
ctx: { ctx: GetIncrementalFetchCacheContext
kind: IncrementalCacheKind ): Promise<IncrementalFetchCacheEntry | null>
revalidate?: Revalidate async get(
fetchUrl?: string cacheKey: string,
fetchIdx?: number ctx: GetIncrementalResponseCacheContext
tags?: string[] ): Promise<IncrementalResponseCacheEntry | null>
softTags?: string[] async get(
isRoutePPREnabled?: boolean cacheKey: string,
isFallback: boolean | undefined ctx: GetIncrementalFetchCacheContext | GetIncrementalResponseCacheContext
}
): Promise<IncrementalCacheEntry | null> { ): Promise<IncrementalCacheEntry | null> {
// Unlike other caches if we have a resume data cache, we use it even if // Unlike other caches if we have a resume data cache, we use it even if
// testmode would normally disable it or if requestHeaders say 'no-cache'. // testmode would normally disable it or if requestHeaders say 'no-cache'.
...@@ -413,12 +422,7 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -413,12 +422,7 @@ export class IncrementalCache implements IncrementalCacheType {
if (resumeDataCache) { if (resumeDataCache) {
const memoryCacheData = resumeDataCache.fetch.get(cacheKey) const memoryCacheData = resumeDataCache.fetch.get(cacheKey)
if (memoryCacheData?.kind === CachedRouteKind.FETCH) { if (memoryCacheData?.kind === CachedRouteKind.FETCH) {
return { return { isStale: false, value: memoryCacheData }
isStale: false,
value: memoryCacheData,
revalidateAfter: false,
isFallback: false,
}
} }
} }
} }
...@@ -434,18 +438,24 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -434,18 +438,24 @@ export class IncrementalCache implements IncrementalCacheType {
return null return null
} }
const { isFallback } = ctx
cacheKey = this._getPathname( cacheKey = this._getPathname(
cacheKey, cacheKey,
ctx.kind === IncrementalCacheKind.FETCH ctx.kind === IncrementalCacheKind.FETCH
) )
let entry: IncrementalCacheEntry | null = null
let revalidate = ctx.revalidate
const cacheData = await this.cacheHandler?.get(cacheKey, ctx) const cacheData = await this.cacheHandler?.get(cacheKey, ctx)
if (cacheData?.value?.kind === CachedRouteKind.FETCH) { if (ctx.kind === IncrementalCacheKind.FETCH) {
if (!cacheData) {
return null
}
if (cacheData.value?.kind !== CachedRouteKind.FETCH) {
throw new InvariantError(
`Expected cached value for cache key ${JSON.stringify(cacheKey)} to be a "FETCH" kind, got ${JSON.stringify(cacheData.value?.kind)} instead.`
)
}
const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])] const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])]
// if a tag was revalidated we don't return stale data // if a tag was revalidated we don't return stale data
if ( if (
...@@ -456,7 +466,7 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -456,7 +466,7 @@ export class IncrementalCache implements IncrementalCacheType {
return null return null
} }
revalidate = revalidate || cacheData.value.revalidate const revalidate = ctx.revalidate || cacheData.value.revalidate
const age = const age =
(performance.timeOrigin + (performance.timeOrigin +
performance.now() - performance.now() -
...@@ -467,19 +477,19 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -467,19 +477,19 @@ export class IncrementalCache implements IncrementalCacheType {
const data = cacheData.value.data const data = cacheData.value.data
return { return {
isStale: isStale, isStale,
value: { value: { kind: CachedRouteKind.FETCH, data, revalidate },
kind: CachedRouteKind.FETCH, }
data, } else if (cacheData?.value?.kind === CachedRouteKind.FETCH) {
revalidate: revalidate, throw new InvariantError(
}, `Expected cached value for cache key ${JSON.stringify(cacheKey)} not to be a ${JSON.stringify(ctx.kind)} kind, got "FETCH" instead.`
revalidateAfter: )
performance.timeOrigin + performance.now() + revalidate * 1000,
isFallback,
} satisfies IncrementalCacheEntry
} }
const curRevalidate = this.revalidateTimings.get(toRoute(cacheKey)) let entry: IncrementalResponseCacheEntry | null = null
const { isFallback } = ctx
const revalidate = this.revalidateTimings.get(toRoute(cacheKey))
let isStale: boolean | -1 | undefined let isStale: boolean | -1 | undefined
let revalidateAfter: Revalidate let revalidateAfter: Revalidate
...@@ -491,7 +501,7 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -491,7 +501,7 @@ export class IncrementalCache implements IncrementalCacheType {
revalidateAfter = this.calculateRevalidate( revalidateAfter = this.calculateRevalidate(
cacheKey, cacheKey,
cacheData?.lastModified || performance.timeOrigin + performance.now(), cacheData?.lastModified || performance.timeOrigin + performance.now(),
this.dev ? ctx.kind !== IncrementalCacheKind.FETCH : false, this.dev ?? false,
ctx.isFallback ctx.isFallback
) )
isStale = isStale =
...@@ -504,7 +514,7 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -504,7 +514,7 @@ export class IncrementalCache implements IncrementalCacheType {
if (cacheData) { if (cacheData) {
entry = { entry = {
isStale, isStale,
curRevalidate, revalidate,
revalidateAfter, revalidateAfter,
value: cacheData.value, value: cacheData.value,
isFallback, isFallback,
...@@ -523,29 +533,30 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -523,29 +533,30 @@ export class IncrementalCache implements IncrementalCacheType {
entry = { entry = {
isStale, isStale,
value: null, value: null,
curRevalidate, revalidate,
revalidateAfter, revalidateAfter,
isFallback, isFallback,
} }
this.set(cacheKey, entry.value, ctx) this.set(cacheKey, entry.value, { ...ctx, revalidate })
} }
return entry return entry
} }
// populate the incremental cache with new data async set(
pathname: string,
data: CachedFetchValue | null,
ctx: SetIncrementalFetchCacheContext
): Promise<void>
async set(
pathname: string,
data: Exclude<IncrementalCacheValue, CachedFetchValue> | null,
ctx: SetIncrementalResponseCacheContext
): Promise<void>
async set( async set(
pathname: string, pathname: string,
data: IncrementalCacheValue | null, data: IncrementalCacheValue | null,
ctx: { ctx: SetIncrementalFetchCacheContext | SetIncrementalResponseCacheContext
revalidate?: Revalidate ): Promise<void> {
fetchCache?: boolean
fetchUrl?: string
fetchIdx?: number
tags?: string[]
isRoutePPREnabled?: boolean
isFallback?: boolean
}
) {
// Even if we otherwise disable caching for testMode or if no fetchCache is // Even if we otherwise disable caching for testMode or if no fetchCache is
// configured we still always stash results in the resume data cache if one // configured we still always stash results in the resume data cache if one
// exists. This is because this is a transient in memory cache that // exists. This is because this is a transient in memory cache that
...@@ -585,7 +596,7 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -585,7 +596,7 @@ export class IncrementalCache implements IncrementalCacheType {
try { try {
// Set the value for the revalidate seconds so if it changes we can // Set the value for the revalidate seconds so if it changes we can
// update the cache with the new value. // update the cache with the new value.
if (typeof ctx.revalidate !== 'undefined' && !ctx.fetchCache) { if (!ctx.fetchCache && typeof ctx.revalidate !== 'undefined') {
this.revalidateTimings.set(toRoute(pathname), ctx.revalidate) this.revalidateTimings.set(toRoute(pathname), ctx.revalidate)
} }
......
...@@ -86,7 +86,6 @@ describe('createPatchedFetcher', () => { ...@@ -86,7 +86,6 @@ describe('createPatchedFetcher', () => {
fetchCache: true, fetchCache: true,
fetchIdx: 1, fetchIdx: 1,
fetchUrl: 'https://example.com/', fetchUrl: 'https://example.com/',
revalidate: false,
tags: [], tags: [],
} }
) )
......
...@@ -635,8 +635,6 @@ export function createPatchedFetcher( ...@@ -635,8 +635,6 @@ export function createPatchedFetcher(
finalRevalidate >= INFINITE_CACHE finalRevalidate >= INFINITE_CACHE
? CACHE_ONE_YEAR ? CACHE_ONE_YEAR
: finalRevalidate : finalRevalidate
const externalRevalidate =
finalRevalidate >= INFINITE_CACHE ? false : finalRevalidate
if (workUnitStore && workUnitStore.type === 'prerender') { if (workUnitStore && workUnitStore.type === 'prerender') {
// We are prerendering at build time or revalidate time with dynamicIO so we need to // We are prerendering at build time or revalidate time with dynamicIO so we need to
...@@ -660,13 +658,7 @@ export function createPatchedFetcher( ...@@ -660,13 +658,7 @@ export function createPatchedFetcher(
data: fetchedData, data: fetchedData,
revalidate: normalizedRevalidate, revalidate: normalizedRevalidate,
}, },
{ { fetchCache: true, fetchUrl, fetchIdx, tags }
fetchCache: true,
revalidate: externalRevalidate,
fetchUrl,
fetchIdx,
tags,
}
) )
await handleUnlock() await handleUnlock()
...@@ -712,13 +704,7 @@ export function createPatchedFetcher( ...@@ -712,13 +704,7 @@ export function createPatchedFetcher(
data: fetchedData, data: fetchedData,
revalidate: normalizedRevalidate, revalidate: normalizedRevalidate,
}, },
{ { fetchCache: true, fetchUrl, fetchIdx, tags }
fetchCache: true,
revalidate: externalRevalidate,
fetchUrl,
fetchIdx,
tags,
}
) )
} }
}) })
...@@ -771,7 +757,6 @@ export function createPatchedFetcher( ...@@ -771,7 +757,6 @@ export function createPatchedFetcher(
fetchIdx, fetchIdx,
tags, tags,
softTags: implicitTags, softTags: implicitTags,
isFallback: false,
}) })
if (hasNoExplicitCacheConfig) { if (hasNoExplicitCacheConfig) {
......
...@@ -77,7 +77,7 @@ import { getCloneableBody } from './body-streams' ...@@ -77,7 +77,7 @@ import { getCloneableBody } from './body-streams'
import { checkIsOnDemandRevalidate } from './api-utils' import { checkIsOnDemandRevalidate } from './api-utils'
import ResponseCache, { import ResponseCache, {
CachedRouteKind, CachedRouteKind,
type IncrementalCacheItem, type IncrementalResponseCacheEntry,
} from './response-cache' } from './response-cache'
import { IncrementalCache } from './lib/incremental-cache' import { IncrementalCache } from './lib/incremental-cache'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
...@@ -665,7 +665,7 @@ export default class NextNodeServer extends BaseServer< ...@@ -665,7 +665,7 @@ export default class NextNodeServer extends BaseServer<
req: NodeNextRequest, req: NodeNextRequest,
res: NodeNextResponse, res: NodeNextResponse,
paramsResult: import('./image-optimizer').ImageParamsResult, paramsResult: import('./image-optimizer').ImageParamsResult,
previousCacheEntry?: IncrementalCacheItem previousCacheEntry?: IncrementalResponseCacheEntry | null
): Promise<{ ): Promise<{
buffer: Buffer buffer: Buffer
contentType: string contentType: string
......
import { import type {
type IncrementalCache, ResponseCacheEntry,
type ResponseCacheEntry, ResponseGenerator,
type ResponseGenerator, ResponseCacheBase,
type IncrementalCacheItem, IncrementalResponseCacheEntry,
type ResponseCacheBase, IncrementalResponseCache,
CachedRouteKind,
} from './types' } from './types'
import { Batcher } from '../../lib/batcher' import { Batcher } from '../../lib/batcher'
...@@ -21,7 +20,7 @@ export * from './types' ...@@ -21,7 +20,7 @@ export * from './types'
export default class ResponseCache implements ResponseCacheBase { export default class ResponseCache implements ResponseCacheBase {
private readonly batcher = Batcher.create< private readonly batcher = Batcher.create<
{ key: string; isOnDemandRevalidate: boolean }, { key: string; isOnDemandRevalidate: boolean },
IncrementalCacheItem | null, IncrementalResponseCacheEntry | null,
string string
>({ >({
// Ensure on-demand revalidate doesn't block normal requests, it should be // Ensure on-demand revalidate doesn't block normal requests, it should be
...@@ -36,7 +35,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -36,7 +35,7 @@ export default class ResponseCache implements ResponseCacheBase {
private previousCacheItem?: { private previousCacheItem?: {
key: string key: string
entry: IncrementalCacheItem | null entry: IncrementalResponseCacheEntry | null
expiresAt: number expiresAt: number
} }
...@@ -56,7 +55,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -56,7 +55,7 @@ export default class ResponseCache implements ResponseCacheBase {
routeKind: RouteKind routeKind: RouteKind
isOnDemandRevalidate?: boolean isOnDemandRevalidate?: boolean
isPrefetch?: boolean isPrefetch?: boolean
incrementalCache: IncrementalCache incrementalCache: IncrementalResponseCache
isRoutePPREnabled?: boolean isRoutePPREnabled?: boolean
isFallback?: boolean isFallback?: boolean
} }
...@@ -91,7 +90,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -91,7 +90,7 @@ export default class ResponseCache implements ResponseCacheBase {
const kind = routeKindToIncrementalCacheKind(context.routeKind) const kind = routeKindToIncrementalCacheKind(context.routeKind)
let resolved = false let resolved = false
let cachedResponse: IncrementalCacheItem = null let cachedResponse: IncrementalResponseCacheEntry | null = null
try { try {
cachedResponse = !this.minimalMode cachedResponse = !this.minimalMode
? await incrementalCache.get(key, { ? await incrementalCache.get(key, {
...@@ -102,16 +101,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -102,16 +101,7 @@ export default class ResponseCache implements ResponseCacheBase {
: null : null
if (cachedResponse && !isOnDemandRevalidate) { if (cachedResponse && !isOnDemandRevalidate) {
if (cachedResponse.value?.kind === CachedRouteKind.FETCH) { resolve(cachedResponse)
throw new Error(
`invariant: unexpected cachedResponse of kind fetch in response cache`
)
}
resolve({
...cachedResponse,
revalidate: cachedResponse.curRevalidate,
})
resolved = true resolved = true
if (!cachedResponse.isStale || context.isPrefetch) { if (!cachedResponse.isStale || context.isPrefetch) {
......
...@@ -131,16 +131,33 @@ export interface IncrementalCachedPageValue { ...@@ -131,16 +131,33 @@ export interface IncrementalCachedPageValue {
status: number | undefined status: number | undefined
} }
export type IncrementalCacheEntry = { export interface IncrementalResponseCacheEntry {
curRevalidate?: Revalidate revalidate?: Revalidate
// milliseconds to revalidate after /**
revalidateAfter: Revalidate * timestamp in milliseconds to revalidate after
// -1 here dictates a blocking revalidate should be used */
revalidateAfter?: Revalidate
/**
* `-1` here dictates a blocking revalidate should be used
*/
isStale?: boolean | -1 isStale?: boolean | -1
value: IncrementalCacheValue | null isMiss?: boolean
isFallback: boolean | undefined isFallback: boolean | undefined
value: Exclude<IncrementalCacheValue, CachedFetchValue> | null
}
export interface IncrementalFetchCacheEntry {
/**
* `-1` here dictates a blocking revalidate should be used
*/
isStale?: boolean | -1
value: CachedFetchValue
} }
export type IncrementalCacheEntry =
| IncrementalResponseCacheEntry
| IncrementalFetchCacheEntry
export type IncrementalCacheValue = export type IncrementalCacheValue =
| CachedRedirectValue | CachedRedirectValue
| IncrementalCachedPageValue | IncrementalCachedPageValue
...@@ -170,20 +187,10 @@ export type ResponseCacheEntry = { ...@@ -170,20 +187,10 @@ export type ResponseCacheEntry = {
*/ */
export type ResponseGenerator = (state: { export type ResponseGenerator = (state: {
hasResolved: boolean hasResolved: boolean
previousCacheEntry?: IncrementalCacheItem previousCacheEntry?: IncrementalResponseCacheEntry | null
isRevalidating?: boolean isRevalidating?: boolean
}) => Promise<ResponseCacheEntry | null> }) => Promise<ResponseCacheEntry | null>
export type IncrementalCacheItem = {
revalidateAfter?: number | false
curRevalidate?: number | false
revalidate?: number | false
value: IncrementalCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
isFallback: boolean | undefined
} | null
export const enum IncrementalCacheKind { export const enum IncrementalCacheKind {
APP_PAGE = 'APP_PAGE', APP_PAGE = 'APP_PAGE',
APP_ROUTE = 'APP_ROUTE', APP_ROUTE = 'APP_ROUTE',
...@@ -192,38 +199,78 @@ export const enum IncrementalCacheKind { ...@@ -192,38 +199,78 @@ export const enum IncrementalCacheKind {
IMAGE = 'IMAGE', IMAGE = 'IMAGE',
} }
export interface IncrementalCache { export interface GetIncrementalFetchCacheContext {
get: ( kind: IncrementalCacheKind.FETCH
key: string, revalidate?: Revalidate
ctx: { fetchUrl?: string
kind: IncrementalCacheKind fetchIdx?: number
tags?: string[]
softTags?: string[]
}
/** export interface GetIncrementalResponseCacheContext {
* True if the route is enabled for PPR. kind: Exclude<IncrementalCacheKind, IncrementalCacheKind.FETCH>
*/ /**
isRoutePPREnabled?: boolean * True if the route is enabled for PPR.
*/
isRoutePPREnabled?: boolean
/** /**
* True if this is a fallback request. * True if this is a fallback request.
*/ */
isFallback: boolean isFallback: boolean
} }
) => Promise<IncrementalCacheItem>
set: (
key: string,
data: IncrementalCacheValue | null,
ctx: {
revalidate: Revalidate
/** export interface SetIncrementalFetchCacheContext {
* True if the route is enabled for PPR. fetchCache: true
*/ fetchUrl?: string
isRoutePPREnabled?: boolean fetchIdx?: number
tags?: string[]
}
/** export interface SetIncrementalResponseCacheContext {
* True if this is a fallback request. fetchCache?: false
*/ revalidate?: Revalidate
isFallback: boolean /**
} * True if the route is enabled for PPR.
) => Promise<void> */
isRoutePPREnabled?: boolean
/**
* True if this is a fallback request.
*/
isFallback?: boolean
}
export interface IncrementalResponseCache {
get(
cacheKey: string,
ctx: GetIncrementalResponseCacheContext
): Promise<IncrementalResponseCacheEntry | null>
set(
key: string,
data: Exclude<IncrementalCacheValue, CachedFetchValue> | null,
ctx: SetIncrementalResponseCacheContext
): Promise<void>
}
export interface IncrementalCache extends IncrementalResponseCache {
get(
cacheKey: string,
ctx: GetIncrementalFetchCacheContext
): Promise<IncrementalFetchCacheEntry | null>
get(
cacheKey: string,
ctx: GetIncrementalResponseCacheContext
): Promise<IncrementalResponseCacheEntry | null>
set(
key: string,
data: CachedFetchValue | null,
ctx: SetIncrementalFetchCacheContext
): Promise<void>
set(
key: string,
data: Exclude<IncrementalCacheValue, CachedFetchValue> | null,
ctx: SetIncrementalResponseCacheContext
): Promise<void>
} }
...@@ -3,7 +3,7 @@ import { ...@@ -3,7 +3,7 @@ import {
IncrementalCacheKind, IncrementalCacheKind,
type CachedAppPageValue, type CachedAppPageValue,
type CachedPageValue, type CachedPageValue,
type IncrementalCacheItem, type IncrementalResponseCacheEntry,
type ResponseCacheEntry, type ResponseCacheEntry,
} from './types' } from './types'
...@@ -12,7 +12,7 @@ import { RouteKind } from '../route-kind' ...@@ -12,7 +12,7 @@ import { RouteKind } from '../route-kind'
export async function fromResponseCacheEntry( export async function fromResponseCacheEntry(
cacheEntry: ResponseCacheEntry cacheEntry: ResponseCacheEntry
): Promise<IncrementalCacheItem> { ): Promise<IncrementalResponseCacheEntry> {
return { return {
...cacheEntry, ...cacheEntry,
value: value:
...@@ -39,16 +39,10 @@ export async function fromResponseCacheEntry( ...@@ -39,16 +39,10 @@ export async function fromResponseCacheEntry(
} }
export async function toResponseCacheEntry( export async function toResponseCacheEntry(
response: IncrementalCacheItem response: IncrementalResponseCacheEntry | null
): Promise<ResponseCacheEntry | null> { ): Promise<ResponseCacheEntry | null> {
if (!response) return null if (!response) return null
if (response.value?.kind === CachedRouteKind.FETCH) {
throw new Error(
'Invariant: unexpected cachedResponse of kind fetch in response cache'
)
}
return { return {
isMiss: response.isMiss, isMiss: response.isMiss,
isStale: response.isStale, isStale: response.isStale,
...@@ -79,7 +73,7 @@ export async function toResponseCacheEntry( ...@@ -79,7 +73,7 @@ export async function toResponseCacheEntry(
export function routeKindToIncrementalCacheKind( export function routeKindToIncrementalCacheKind(
routeKind: RouteKind routeKind: RouteKind
): IncrementalCacheKind { ): Exclude<IncrementalCacheKind, IncrementalCacheKind.FETCH> {
switch (routeKind) { switch (routeKind) {
case RouteKind.PAGES: case RouteKind.PAGES:
return IncrementalCacheKind.PAGES return IncrementalCacheKind.PAGES
......
...@@ -37,13 +37,7 @@ async function cacheNewResult<T>( ...@@ -37,13 +37,7 @@ async function cacheNewResult<T>(
} satisfies CachedFetchData, } satisfies CachedFetchData,
revalidate: typeof revalidate !== 'number' ? CACHE_ONE_YEAR : revalidate, revalidate: typeof revalidate !== 'number' ? CACHE_ONE_YEAR : revalidate,
}, },
{ { fetchCache: true, tags, fetchIdx, fetchUrl }
revalidate,
fetchCache: true,
tags,
fetchIdx,
fetchUrl,
}
) )
return return
} }
...@@ -208,7 +202,6 @@ export function unstable_cache<T extends Callback>( ...@@ -208,7 +202,6 @@ export function unstable_cache<T extends Callback>(
softTags: implicitTags, softTags: implicitTags,
fetchIdx, fetchIdx,
fetchUrl, fetchUrl,
isFallback: false,
}) })
if (cacheEntry && cacheEntry.value) { if (cacheEntry && cacheEntry.value) {
...@@ -312,7 +305,6 @@ export function unstable_cache<T extends Callback>( ...@@ -312,7 +305,6 @@ export function unstable_cache<T extends Callback>(
fetchIdx, fetchIdx,
fetchUrl, fetchUrl,
softTags: implicitTags, softTags: implicitTags,
isFallback: false,
}) })
if (cacheEntry && cacheEntry.value) { if (cacheEntry && cacheEntry.value) {
......
...@@ -5,7 +5,7 @@ import { ...@@ -5,7 +5,7 @@ import {
} from 'next/dist/server/image-optimizer' } from 'next/dist/server/image-optimizer'
import { import {
CachedRouteKind, CachedRouteKind,
IncrementalCacheItem, IncrementalCacheEntry,
} from 'next/dist/server/response-cache/types' } from 'next/dist/server/response-cache/types'
import { readFile } from 'fs-extra' import { readFile } from 'fs-extra'
import { join } from 'path' import { join } from 'path'
...@@ -37,7 +37,7 @@ const getPreviousCacheEntry = async ( ...@@ -37,7 +37,7 @@ const getPreviousCacheEntry = async (
) => { ) => {
const buffer = await readFile(join(__dirname, filepath)) const buffer = await readFile(join(__dirname, filepath))
const upstreamEtag = getImageEtag(buffer) const upstreamEtag = getImageEtag(buffer)
const result: IncrementalCacheItem = { const result: IncrementalCacheEntry = {
...baseCacheEntry, ...baseCacheEntry,
value: { value: {
kind: CachedRouteKind.IMAGE, kind: CachedRouteKind.IMAGE,
...@@ -62,7 +62,7 @@ describe('shouldUsePreviouslyCachedEntry', () => { ...@@ -62,7 +62,7 @@ describe('shouldUsePreviouslyCachedEntry', () => {
}) })
it('should return null if previous cache entry value is not of kind IMAGE', async () => { it('should return null if previous cache entry value is not of kind IMAGE', async () => {
const nonImageCacheEntry: IncrementalCacheItem = { const nonImageCacheEntry: IncrementalCacheEntry = {
...baseCacheEntry, ...baseCacheEntry,
value: { kind: CachedRouteKind.REDIRECT, props: {} }, value: { kind: CachedRouteKind.REDIRECT, props: {} },
} }
......
...@@ -79,7 +79,6 @@ describe('FileSystemCache (isrMemory 0)', () => { ...@@ -79,7 +79,6 @@ describe('FileSystemCache (isrMemory 0)', () => {
}, },
{ {
fetchCache: true, fetchCache: true,
revalidate: 30,
fetchUrl: 'http://my-api.local', fetchUrl: 'http://my-api.local',
fetchIdx: 5, fetchIdx: 5,
tags: ['server-time'], tags: ['server-time'],
...@@ -89,7 +88,6 @@ describe('FileSystemCache (isrMemory 0)', () => { ...@@ -89,7 +88,6 @@ describe('FileSystemCache (isrMemory 0)', () => {
const res = await fsCache.get('fetch-cache', { const res = await fsCache.get('fetch-cache', {
tags: ['server-time'], tags: ['server-time'],
kind: IncrementalCacheKind.FETCH, kind: IncrementalCacheKind.FETCH,
isFallback: undefined,
}) })
expect(res?.value).toEqual({ expect(res?.value).toEqual({
...@@ -113,13 +111,12 @@ describe('FileSystemCache (isrMemory 0)', () => { ...@@ -113,13 +111,12 @@ describe('FileSystemCache (isrMemory 0)', () => {
data: { headers: {}, body: '1700056381', status: 200, url: '' }, data: { headers: {}, body: '1700056381', status: 200, url: '' },
revalidate: 30, revalidate: 30,
}, },
{ revalidate: 30, fetchCache: true, tags: ['server-time2'] } { fetchCache: true, tags: ['server-time2'] }
) )
const res = await fsCache.get('unstable-cache', { const res = await fsCache.get('unstable-cache', {
tags: ['server-time'], tags: ['server-time'],
kind: IncrementalCacheKind.FETCH, kind: IncrementalCacheKind.FETCH,
isFallback: undefined,
}) })
expect(res?.value).toEqual({ expect(res?.value).toEqual({
......
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