Unverified Commit ca735270 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

refactor: incremental cache cleanup (#68602)

This replaces usage of string constant types within the cache layers to
instead use enum types. These enum types given names carry much more
meaning than passing around string constants making it easier to read
and understand. With these changes, due to now us knowing the correct
page type we're getting from the cache because of the routing layer,
makes the route kind a required option that's passed in.

Now that the route kind is known prior to cache access, first access of
pages no long call into the filesystem needlessly, and improves cache
performance for the filesystem based cache. This also lets us remove the
kind discovery system that was in place previously allowing it to get
simplified.
parent 016ba387
......@@ -1374,9 +1374,6 @@ export async function buildAppStaticPaths({
const incrementalCache = new IncrementalCache({
fs: nodeFs,
dev: true,
// Enabled both for build as we're only writing this cache, not reading it.
pagesDir: true,
appDir: true,
flushToDisk: isrFlushToDisk,
serverDistDir: path.join(distDir, 'server'),
fetchCacheKeyPrefix,
......
import type { NextEnabledDirectories } from '../../server/base-server'
import path from 'path'
import { IncrementalCache } from '../../server/lib/incremental-cache'
import { hasNextSupport } from '../../telemetry/ci-info'
......@@ -13,7 +11,6 @@ export async function createIncrementalCache({
fetchCacheKeyPrefix,
distDir,
dir,
enabledDirectories,
flushToDisk,
}: {
cacheHandler?: string
......@@ -21,7 +18,6 @@ export async function createIncrementalCache({
fetchCacheKeyPrefix?: string
distDir: string
dir: string
enabledDirectories: NextEnabledDirectories
flushToDisk?: boolean
}) {
// Custom cache handler overrides.
......@@ -53,8 +49,6 @@ export async function createIncrementalCache({
notFoundRoutes: [],
}),
fs: nodeFs,
pagesDir: enabledDirectories.pages,
appDir: enabledDirectories.app,
serverDistDir: path.join(distDir, 'server'),
CurCacheHandler: CacheHandler,
minimalMode: hasNextSupport,
......
......@@ -551,7 +551,6 @@ async function exportAppImpl(
dir,
distDir,
outDir,
enabledDirectories,
nextConfig,
cacheHandler: nextConfig.cacheHandler,
cacheMaxMemorySize: nextConfig.cacheMaxMemorySize,
......
......@@ -55,7 +55,6 @@ export interface ExportPagesInput {
fetchCache: boolean | undefined
cacheHandler: string | undefined
fetchCacheKeyPrefix: string | undefined
enabledDirectories: NextEnabledDirectories
options: ExportAppOptions
}
......
......@@ -328,7 +328,6 @@ export async function exportPages(
cacheHandler,
cacheMaxMemorySize,
fetchCacheKeyPrefix,
enabledDirectories,
pagesDataDir,
renderOpts,
nextConfig,
......@@ -343,7 +342,6 @@ export async function exportPages(
fetchCacheKeyPrefix,
distDir,
dir,
enabledDirectories: enabledDirectories,
// skip writing to disk in minimal mode for now, pending some
// changes to better support it
flushToDisk: !hasNextSupport,
......
......@@ -15,13 +15,15 @@ import type {
RenderOptsPartial as AppRenderOptsPartial,
ServerOnInstrumentationRequestError,
} from './app-render/types'
import type {
CachedAppPageValue,
CachedPageValue,
ServerComponentsHmrCache,
ResponseCacheBase,
ResponseCacheEntry,
ResponseGenerator,
import {
type CachedAppPageValue,
type CachedPageValue,
type ServerComponentsHmrCache,
type ResponseCacheBase,
type ResponseCacheEntry,
type ResponseGenerator,
CachedRouteKind,
type CachedRedirectValue,
} from './response-cache'
import type { UrlWithParsedQuery } from 'url'
import {
......@@ -159,6 +161,8 @@ import {
} from './after/builtin-request-context'
import { ENCODED_TAGS } from './stream-utils/encodedTags'
import { NextRequestHint } from './web/adapter'
import { RouteKind } from './route-kind'
import type { RouteModule } from './route-modules/route-module'
export type FindComponentsResult = {
components: LoadComponentsReturnType
......@@ -2043,7 +2047,10 @@ export default abstract class Server<
)
}
const { routeModule } = components
let routeModule: RouteModule | undefined
if (components.routeModule) {
routeModule = components.routeModule
}
/**
* If the route being rendered is an app page, and the ppr feature has been
......@@ -2493,7 +2500,7 @@ export default abstract class Server<
// Create the cache entry for the response.
const cacheEntry: ResponseCacheEntry = {
value: {
kind: 'ROUTE',
kind: CachedRouteKind.APP_ROUTE,
status: response.status,
body: Buffer.from(await blob.arrayBuffer()),
headers,
......@@ -2657,9 +2664,9 @@ export default abstract class Server<
if (metadata.isRedirect) {
return {
value: {
kind: 'REDIRECT',
kind: CachedRouteKind.REDIRECT,
props: metadata.pageData ?? metadata.flightData,
},
} satisfies CachedRedirectValue,
revalidate: metadata.revalidate,
}
}
......@@ -2673,7 +2680,7 @@ export default abstract class Server<
if (isAppPath) {
return {
value: {
kind: 'APP_PAGE',
kind: CachedRouteKind.APP_PAGE,
html: result,
headers,
rscData: metadata.flightData,
......@@ -2686,7 +2693,7 @@ export default abstract class Server<
return {
value: {
kind: 'PAGE',
kind: CachedRouteKind.PAGES,
html: result,
pageData: metadata.pageData ?? metadata.flightData,
headers,
......@@ -2802,11 +2809,11 @@ export default abstract class Server<
return {
value: {
kind: 'PAGE',
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(html),
pageData: {},
status: undefined,
headers: undefined,
pageData: {},
},
}
}
......@@ -2845,12 +2852,12 @@ export default abstract class Server<
return {
revalidate: 1,
value: {
kind: 'PAGE',
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(''),
pageData: {},
headers: undefined,
status: undefined,
},
} satisfies CachedPageValue,
}
}
......@@ -2871,7 +2878,11 @@ export default abstract class Server<
ssgCacheKey,
responseGenerator,
{
routeKind: routeModule?.definition.kind,
routeKind:
// If the route module is not defined, we can assume it's a page being
// rendered and thus check isAppPath.
routeModule?.definition.kind ??
(isAppPath ? RouteKind.APP_PAGE : RouteKind.PAGES),
incrementalCache,
isOnDemandRevalidate,
isPrefetch: req.headers.purpose === 'prefetch',
......@@ -2892,7 +2903,7 @@ export default abstract class Server<
}
const didPostpone =
cacheEntry.value?.kind === 'APP_PAGE' &&
cacheEntry.value?.kind === CachedRouteKind.APP_PAGE &&
typeof cacheEntry.value.postponed === 'string'
if (
......@@ -2924,7 +2935,7 @@ export default abstract class Server<
const { value: cachedData } = cacheEntry
// If the cache value is an image, we should error early.
if (cachedData?.kind === 'IMAGE') {
if (cachedData?.kind === CachedRouteKind.IMAGE) {
throw new Error('invariant SSG should not return an image cache value')
}
......@@ -3007,7 +3018,7 @@ export default abstract class Server<
value: {
...cacheEntry.value,
kind:
cacheEntry.value?.kind === 'APP_PAGE'
cacheEntry.value?.kind === CachedRouteKind.APP_PAGE
? 'PAGE'
: cacheEntry.value?.kind,
},
......@@ -3049,7 +3060,7 @@ export default abstract class Server<
}
await this.render404(req, res, { pathname, query }, false)
return null
} else if (cachedData.kind === 'REDIRECT') {
} else if (cachedData.kind === CachedRouteKind.REDIRECT) {
if (cacheEntry.revalidate) {
res.setHeader(
'Cache-Control',
......@@ -3073,7 +3084,7 @@ export default abstract class Server<
await handleRedirect(cachedData.props)
return null
}
} else if (cachedData.kind === 'ROUTE') {
} else if (cachedData.kind === CachedRouteKind.APP_ROUTE) {
const headers = { ...cachedData.headers }
if (!(this.minimalMode && isSSG)) {
......@@ -3089,7 +3100,7 @@ export default abstract class Server<
})
)
return null
} else if (cachedData.kind === 'APP_PAGE') {
} else if (cachedData.kind === CachedRouteKind.APP_PAGE) {
// If the request has a postponed state and it's a resume request we
// should error.
if (cachedData.postponed && minimalPostponed) {
......@@ -3224,7 +3235,7 @@ export default abstract class Server<
throw new Error('Invariant: expected a result to be returned')
}
if (result.value?.kind !== 'APP_PAGE') {
if (result.value?.kind !== CachedRouteKind.APP_PAGE) {
throw new Error(
`Invariant: expected a page response, got ${result.value?.kind}`
)
......
......@@ -14,9 +14,10 @@ import { hasMatch } from '../shared/lib/match-remote-pattern'
import type { NextConfigComplete } from './config-shared'
import { createRequestResponseMocks } from './lib/mock-request'
import type { NextUrlWithParsedQuery } from './request-meta'
import type {
IncrementalCacheEntry,
IncrementalCacheValue,
import {
CachedRouteKind,
type IncrementalCacheEntry,
type IncrementalCacheValue,
} from './response-cache'
import { sendEtagResponse } from './send-payload'
import { getContentType, getExtension } from './serve-static'
......@@ -359,7 +360,7 @@ export class ImageOptimizerCache {
return {
value: {
kind: 'IMAGE',
kind: CachedRouteKind.IMAGE,
etag,
buffer,
extension,
......@@ -385,7 +386,7 @@ export class ImageOptimizerCache {
revalidate?: number | false
}
) {
if (value?.kind !== 'IMAGE') {
if (value?.kind !== CachedRouteKind.IMAGE) {
throw new Error('invariant attempted to set non-image to image-cache')
}
......
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
import type { IncrementalCacheValue } from '../../response-cache'
import {
CachedRouteKind,
IncrementalCacheKind,
type IncrementalCacheValue,
} from '../../response-cache'
import LRUCache from 'next/dist/compiled/lru-cache'
import {
......@@ -128,20 +132,22 @@ export default class FetchCache implements CacheHandler {
length({ value }) {
if (!value) {
return 25
} else if (value.kind === 'REDIRECT') {
} else if (value.kind === CachedRouteKind.REDIRECT) {
return JSON.stringify(value.props).length
} else if (value.kind === 'IMAGE') {
} else if (value.kind === CachedRouteKind.IMAGE) {
throw new Error('invariant image should not be incremental-cache')
} else if (value.kind === 'FETCH') {
} else if (value.kind === CachedRouteKind.FETCH) {
return JSON.stringify(value.data || '').length
} else if (value.kind === 'ROUTE') {
} else if (value.kind === CachedRouteKind.APP_ROUTE) {
return value.body.length
}
// rough estimate of size of cache value
return (
value.html.length +
(JSON.stringify(
value.kind === 'APP_PAGE' ? value.rscData : value.pageData
value.kind === CachedRouteKind.APP_PAGE
? value.rscData
: value.pageData
)?.length || 0)
)
},
......@@ -203,10 +209,10 @@ export default class FetchCache implements CacheHandler {
}
public async get(...args: Parameters<CacheHandler['get']>) {
const [key, ctx = {}] = args
const { tags, softTags, kindHint, fetchIdx, fetchUrl } = ctx
const [key, ctx] = args
const { tags, softTags, kind: kindHint, fetchIdx, fetchUrl } = ctx
if (kindHint !== 'fetch') {
if (kindHint !== IncrementalCacheKind.FETCH) {
return null
}
......@@ -223,7 +229,7 @@ export default class FetchCache implements CacheHandler {
let data = memoryCache?.get(key)
const hasFetchKindAndMatchingTags =
data?.value?.kind === 'FETCH' &&
data?.value?.kind === CachedRouteKind.FETCH &&
this.hasMatchingTags(tags ?? [], data.value.tags ?? [])
// Get data from fetch cache. Also check if new tags have been
......@@ -274,13 +280,13 @@ export default class FetchCache implements CacheHandler {
const cached: IncrementalCacheValue = await res.json()
if (!cached || cached.kind !== 'FETCH') {
if (!cached || cached.kind !== CachedRouteKind.FETCH) {
DEBUG && console.log({ cached })
throw new Error('invalid cache value')
}
// if new tags were specified, merge those tags to the existing tags
if (cached.kind === 'FETCH') {
if (cached.kind === CachedRouteKind.FETCH) {
cached.tags ??= []
for (const tag of tags ?? []) {
if (!cached.tags.includes(tag)) {
......@@ -331,11 +337,12 @@ export default class FetchCache implements CacheHandler {
public async set(...args: Parameters<CacheHandler['set']>) {
const [key, data, ctx] = args
const newValue = data?.kind === 'FETCH' ? data.data : undefined
const newValue =
data?.kind === CachedRouteKind.FETCH ? data.data : undefined
const existingCache = memoryCache?.get(key)
const existingValue = existingCache?.value
if (
existingValue?.kind === 'FETCH' &&
existingValue?.kind === CachedRouteKind.FETCH &&
Object.keys(existingValue.data).every(
(field) =>
JSON.stringify(
......
import type { CacheFs } from '../../../shared/lib/utils'
import type { PrerenderManifest } from '../../../build'
import type {
IncrementalCacheValue,
IncrementalCacheEntry,
IncrementalCache as IncrementalCacheType,
IncrementalCacheKindHint,
import {
type IncrementalCacheValue,
type IncrementalCacheEntry,
type IncrementalCache as IncrementalCacheType,
IncrementalCacheKind,
CachedRouteKind,
} from '../../response-cache'
import type { Revalidate } from '../revalidate'
import type { DeepReadonly } from '../../../shared/lib/deep-readonly'
......@@ -31,8 +32,6 @@ export interface CacheHandlerContext {
fetchCacheKeyPrefix?: string
prerenderManifest?: PrerenderManifest
revalidatedTags: string[]
_appDir: boolean
_pagesDir: boolean
_requestHeaders: IncrementalCache['requestHeaders']
}
......@@ -90,8 +89,6 @@ export class IncrementalCache implements IncrementalCacheType {
constructor({
fs,
dev,
appDir,
pagesDir,
flushToDisk,
fetchCache,
minimalMode,
......@@ -106,8 +103,6 @@ export class IncrementalCache implements IncrementalCacheType {
}: {
fs?: CacheFs
dev: boolean
appDir?: boolean
pagesDir?: boolean
fetchCache?: boolean
minimalMode?: boolean
serverDistDir?: string
......@@ -186,8 +181,6 @@ export class IncrementalCache implements IncrementalCacheType {
serverDistDir,
revalidatedTags,
maxMemoryCacheSize,
_pagesDir: !!pagesDir,
_appDir: !!appDir,
_requestHeaders: requestHeaders,
fetchCacheKeyPrefix,
})
......@@ -384,33 +377,36 @@ export class IncrementalCache implements IncrementalCacheType {
async get(
cacheKey: string,
ctx: {
kindHint?: IncrementalCacheKindHint
kind: IncrementalCacheKind
revalidate?: Revalidate
fetchUrl?: string
fetchIdx?: number
tags?: string[]
softTags?: string[]
isRoutePPREnabled?: boolean
} = {}
}
): Promise<IncrementalCacheEntry | null> {
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if (
this.disableForTestmode ||
(this.dev &&
(ctx.kindHint !== 'fetch' ||
(ctx.kind !== IncrementalCacheKind.FETCH ||
this.requestHeaders['cache-control'] === 'no-cache'))
) {
return null
}
cacheKey = this._getPathname(cacheKey, ctx.kindHint === 'fetch')
cacheKey = this._getPathname(
cacheKey,
ctx.kind === IncrementalCacheKind.FETCH
)
let entry: IncrementalCacheEntry | null = null
let revalidate = ctx.revalidate
const cacheData = await this.cacheHandler?.get(cacheKey, ctx)
if (cacheData?.value?.kind === 'FETCH') {
if (cacheData?.value?.kind === CachedRouteKind.FETCH) {
const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])]
// if a tag was revalidated we don't return stale data
if (
......@@ -430,7 +426,7 @@ export class IncrementalCache implements IncrementalCacheType {
return {
isStale: isStale,
value: {
kind: 'FETCH',
kind: CachedRouteKind.FETCH,
data,
revalidate: revalidate,
},
......@@ -450,7 +446,7 @@ export class IncrementalCache implements IncrementalCacheType {
revalidateAfter = this.calculateRevalidate(
cacheKey,
cacheData?.lastModified || Date.now(),
this.dev && ctx.kindHint !== 'fetch'
this.dev && ctx.kind !== IncrementalCacheKind.FETCH
)
isStale =
revalidateAfter !== false && revalidateAfter < Date.now()
......
......@@ -18,7 +18,11 @@ import type {
RequestAsyncStorage,
RequestStore,
} from '../../client/components/request-async-storage.external'
import type { CachedFetchData } from '../response-cache'
import {
CachedRouteKind,
IncrementalCacheKind,
type CachedFetchData,
} from '../response-cache'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
......@@ -615,7 +619,7 @@ export function createPatchedFetcher(
await incrementalCache.set(
cacheKey,
{
kind: 'FETCH',
kind: CachedRouteKind.FETCH,
data: fetchedData,
revalidate: normalizedRevalidate,
},
......@@ -666,7 +670,7 @@ export function createPatchedFetcher(
const entry = staticGenerationStore.isOnDemandRevalidate
? null
: await incrementalCache.get(cacheKey, {
kindHint: 'fetch',
kind: IncrementalCacheKind.FETCH,
revalidate: finalRevalidate,
fetchUrl,
fetchIdx,
......@@ -681,7 +685,7 @@ export function createPatchedFetcher(
cacheReasonOverride = 'cache-control: no-cache (hard refresh)'
}
if (entry?.value && entry.value.kind === 'FETCH') {
if (entry?.value && entry.value.kind === CachedRouteKind.FETCH) {
// when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data
if (staticGenerationStore.isRevalidate && entry.isStale) {
......
......@@ -74,7 +74,7 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'
import { getCloneableBody } from './body-streams'
import { checkIsOnDemandRevalidate } from './api-utils'
import ResponseCache from './response-cache'
import ResponseCache, { CachedRouteKind } from './response-cache'
import { IncrementalCache } from './lib/incremental-cache'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
......@@ -101,6 +101,7 @@ import { formatDynamicImportPath } from '../lib/format-dynamic-import-path'
import type { NextFontManifest } from '../build/webpack/plugins/next-font-manifest-plugin'
import { isInterceptionRouteRewrite } from '../lib/generate-interception-routes-rewrites'
import type { ServerOnInstrumentationRequestError } from './app-render/types'
import { RouteKind } from './route-kind'
export * from './base-server'
......@@ -374,8 +375,6 @@ export default class NextNodeServer extends BaseServer<
dev,
requestHeaders,
requestProtocol,
pagesDir: this.enabledDirectories.pages,
appDir: this.enabledDirectories.app,
allowedRevalidateHeaderKeys:
this.nextConfig.experimental.allowedRevalidateHeaderKeys,
minimalMode: this.minimalMode,
......@@ -876,7 +875,7 @@ export default class NextNodeServer extends BaseServer<
return {
value: {
kind: 'IMAGE',
kind: CachedRouteKind.IMAGE,
buffer,
etag,
extension: getExtension(contentType) as string,
......@@ -885,11 +884,12 @@ export default class NextNodeServer extends BaseServer<
}
},
{
routeKind: RouteKind.IMAGE,
incrementalCache: imageOptimizerCache,
}
)
if (cacheEntry?.value?.kind !== 'IMAGE') {
if (cacheEntry?.value?.kind !== CachedRouteKind.IMAGE) {
throw new Error(
'invariant did not get entry from image response cache'
)
......
import type {
IncrementalCache,
ResponseCacheEntry,
ResponseGenerator,
IncrementalCacheItem,
ResponseCacheBase,
IncrementalCacheKindHint,
import {
type IncrementalCache,
type ResponseCacheEntry,
type ResponseGenerator,
type IncrementalCacheItem,
type ResponseCacheBase,
CachedRouteKind,
} from './types'
import { RouteKind } from '../route-kind'
import { Batcher } from '../../lib/batcher'
import { scheduleOnNextTick } from '../../lib/scheduler'
import { fromResponseCacheEntry, toResponseCacheEntry } from './utils'
import {
fromResponseCacheEntry,
routeKindToIncrementalCacheKind,
toResponseCacheEntry,
} from './utils'
import type { RouteKind } from '../route-kind'
export * from './types'
......@@ -49,7 +53,7 @@ export default class ResponseCache implements ResponseCacheBase {
key: string | null,
responseGenerator: ResponseGenerator,
context: {
routeKind?: RouteKind
routeKind: RouteKind
isOnDemandRevalidate?: boolean
isPrefetch?: boolean
incrementalCache: IncrementalCache
......@@ -76,28 +80,20 @@ export default class ResponseCache implements ResponseCacheBase {
}
// Coerce the kindHint into a given kind for the incremental cache.
let kindHint: IncrementalCacheKindHint | undefined
if (
context.routeKind === RouteKind.APP_PAGE ||
context.routeKind === RouteKind.APP_ROUTE
) {
kindHint = 'app'
} else if (context.routeKind === RouteKind.PAGES) {
kindHint = 'pages'
}
const kind = routeKindToIncrementalCacheKind(context.routeKind)
let resolved = false
let cachedResponse: IncrementalCacheItem = null
try {
cachedResponse = !this.minimalMode
? await incrementalCache.get(key, {
kindHint,
kind,
isRoutePPREnabled: context.isRoutePPREnabled,
})
: null
if (cachedResponse && !isOnDemandRevalidate) {
if (cachedResponse.value?.kind === 'FETCH') {
if (cachedResponse.value?.kind === CachedRouteKind.FETCH) {
throw new Error(
`invariant: unexpected cachedResponse of kind fetch in response cache`
)
......
......@@ -16,7 +16,7 @@ export interface ResponseCacheBase {
* this is so it knows where to look up the cache entry from. If not
* provided it will test the filesystem to check.
*/
routeKind?: RouteKind
routeKind: RouteKind
isRoutePPREnabled?: boolean
}
......@@ -37,8 +37,17 @@ export type CachedFetchData = {
status?: number
}
export const enum CachedRouteKind {
APP_PAGE = 'APP_PAGE',
APP_ROUTE = 'APP_ROUTE',
PAGES = 'PAGES',
FETCH = 'FETCH',
REDIRECT = 'REDIRECT',
IMAGE = 'IMAGE',
}
export interface CachedFetchValue {
kind: 'FETCH'
kind: CachedRouteKind.FETCH
data: CachedFetchData
// tags are only present with file-system-cache
// fetch cache stores tags outside of cache entry
......@@ -47,12 +56,12 @@ export interface CachedFetchValue {
}
export interface CachedRedirectValue {
kind: 'REDIRECT'
kind: CachedRouteKind.REDIRECT
props: Object
}
export interface CachedAppPageValue {
kind: 'APP_PAGE'
kind: CachedRouteKind.APP_PAGE
// this needs to be a RenderResult so since renderResponse
// expects that type instead of a string
html: RenderResult
......@@ -63,7 +72,7 @@ export interface CachedAppPageValue {
}
export interface CachedPageValue {
kind: 'PAGE'
kind: CachedRouteKind.PAGES
// this needs to be a RenderResult so since renderResponse
// expects that type instead of a string
html: RenderResult
......@@ -73,7 +82,7 @@ export interface CachedPageValue {
}
export interface CachedRouteValue {
kind: 'ROUTE'
kind: CachedRouteKind.APP_ROUTE
// this needs to be a RenderResult so since renderResponse
// expects that type instead of a string
body: Buffer
......@@ -82,7 +91,7 @@ export interface CachedRouteValue {
}
export interface CachedImageValue {
kind: 'IMAGE'
kind: CachedRouteKind.IMAGE
etag: string
buffer: Buffer
extension: string
......@@ -91,7 +100,7 @@ export interface CachedImageValue {
}
export interface IncrementalCachedAppPageValue {
kind: 'APP_PAGE'
kind: CachedRouteKind.APP_PAGE
// this needs to be a string since the cache expects to store
// the string value
html: string
......@@ -102,7 +111,7 @@ export interface IncrementalCachedAppPageValue {
}
export interface IncrementalCachedPageValue {
kind: 'PAGE'
kind: CachedRouteKind.PAGES
// this needs to be a string since the cache expects to store
// the string value
html: string
......@@ -161,17 +170,19 @@ export type IncrementalCacheItem = {
isMiss?: boolean
} | null
export type IncrementalCacheKindHint = 'app' | 'pages' | 'fetch'
export const enum IncrementalCacheKind {
APP_PAGE = 'APP_PAGE',
APP_ROUTE = 'APP_ROUTE',
PAGES = 'PAGES',
FETCH = 'FETCH',
IMAGE = 'IMAGE',
}
export interface IncrementalCache {
get: (
key: string,
ctx?: {
/**
* The kind of cache entry to get. If not provided it will try to
* determine the kind from the filesystem.
*/
kindHint?: IncrementalCacheKindHint
ctx: {
kind: IncrementalCacheKind
isRoutePPREnabled?: boolean
}
......
import type { IncrementalCacheItem, ResponseCacheEntry } from './types'
import {
CachedRouteKind,
IncrementalCacheKind,
type CachedAppPageValue,
type CachedPageValue,
type IncrementalCacheItem,
type ResponseCacheEntry,
} from './types'
import RenderResult from '../render-result'
import { RouteKind } from '../route-kind'
export async function fromResponseCacheEntry(
cacheEntry: ResponseCacheEntry
......@@ -8,17 +16,17 @@ export async function fromResponseCacheEntry(
return {
...cacheEntry,
value:
cacheEntry.value?.kind === 'PAGE'
cacheEntry.value?.kind === CachedRouteKind.PAGES
? {
kind: 'PAGE',
kind: CachedRouteKind.PAGES,
html: await cacheEntry.value.html.toUnchunkedString(true),
pageData: cacheEntry.value.pageData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status,
}
: cacheEntry.value?.kind === 'APP_PAGE'
: cacheEntry.value?.kind === CachedRouteKind.APP_PAGE
? {
kind: 'APP_PAGE',
kind: CachedRouteKind.APP_PAGE,
html: await cacheEntry.value.html.toUnchunkedString(true),
postponed: cacheEntry.value.postponed,
rscData: cacheEntry.value.rscData,
......@@ -34,7 +42,7 @@ export async function toResponseCacheEntry(
): Promise<ResponseCacheEntry | null> {
if (!response) return null
if (response.value?.kind === 'FETCH') {
if (response.value?.kind === CachedRouteKind.FETCH) {
throw new Error(
'Invariant: unexpected cachedResponse of kind fetch in response cache'
)
......@@ -45,23 +53,40 @@ export async function toResponseCacheEntry(
isStale: response.isStale,
revalidate: response.revalidate,
value:
response.value?.kind === 'PAGE'
? {
kind: 'PAGE',
response.value?.kind === CachedRouteKind.PAGES
? ({
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(response.value.html),
pageData: response.value.pageData,
headers: response.value.headers,
status: response.value.status,
}
: response.value?.kind === 'APP_PAGE'
? {
kind: 'APP_PAGE',
} satisfies CachedPageValue)
: response.value?.kind === CachedRouteKind.APP_PAGE
? ({
kind: CachedRouteKind.APP_PAGE,
html: RenderResult.fromStatic(response.value.html),
rscData: response.value.rscData,
headers: response.value.headers,
status: response.value.status,
postponed: response.value.postponed,
}
} satisfies CachedAppPageValue)
: response.value,
}
}
export function routeKindToIncrementalCacheKind(
routeKind: RouteKind
): IncrementalCacheKind {
switch (routeKind) {
case RouteKind.PAGES:
return IncrementalCacheKind.PAGES
case RouteKind.APP_PAGE:
return IncrementalCacheKind.APP_PAGE
case RouteKind.IMAGE:
return IncrementalCacheKind.IMAGE
case RouteKind.APP_ROUTE:
return IncrementalCacheKind.APP_ROUTE
default:
throw new Error(`Unexpected route kind ${routeKind}`)
}
}
......@@ -17,4 +17,9 @@ export const enum RouteKind {
* filename of `route.{j,t}s{,x}`.
*/
APP_ROUTE = 'APP_ROUTE',
/**
* `IMAGE` represents all the images that are generated by `next/image`.
*/
IMAGE = 'IMAGE',
}
......@@ -81,8 +81,6 @@ export default class NextWebServer extends BaseServer<
dev,
requestHeaders,
requestProtocol: 'https',
pagesDir: this.enabledDirectories.pages,
appDir: this.enabledDirectories.app,
allowedRevalidateHeaderKeys:
this.nextConfig.experimental.allowedRevalidateHeaderKeys,
minimalMode: this.minimalMode,
......
......@@ -8,6 +8,11 @@ import {
} from '../../lib/patch-fetch'
import { staticGenerationAsyncStorage } from '../../../client/components/static-generation-async-storage.external'
import { requestAsyncStorage } from '../../../client/components/request-async-storage.external'
import {
CachedRouteKind,
IncrementalCacheKind,
type CachedFetchData,
} from '../../response-cache'
type Callback = (...args: any[]) => Promise<any>
......@@ -25,14 +30,14 @@ async function cacheNewResult<T>(
await incrementalCache.set(
cacheKey,
{
kind: 'FETCH',
kind: CachedRouteKind.FETCH,
data: {
headers: {},
// TODO: handle non-JSON values?
body: JSON.stringify(result),
status: 200,
url: '',
},
} satisfies CachedFetchData,
revalidate: typeof revalidate !== 'number' ? CACHE_ONE_YEAR : revalidate,
},
{
......@@ -185,7 +190,7 @@ export function unstable_cache<T extends Callback>(
) {
// We attempt to get the current cache entry from the incremental cache.
const cacheEntry = await incrementalCache.get(cacheKey, {
kindHint: 'fetch',
kind: IncrementalCacheKind.FETCH,
revalidate: options.revalidate,
tags,
softTags: implicitTags,
......@@ -195,7 +200,7 @@ export function unstable_cache<T extends Callback>(
if (cacheEntry && cacheEntry.value) {
// The entry exists and has a value
if (cacheEntry.value.kind !== 'FETCH') {
if (cacheEntry.value.kind !== CachedRouteKind.FETCH) {
// The entry is invalid and we need a special warning
// @TODO why do we warn this way? Should this just be an error? How are these errors surfaced
// so bugs can be reported
......@@ -298,7 +303,7 @@ export function unstable_cache<T extends Callback>(
addImplicitTags(staticGenerationStore, requestStore)
const cacheEntry = await incrementalCache.get(cacheKey, {
kindHint: 'fetch',
kind: IncrementalCacheKind.FETCH,
revalidate: options.revalidate,
tags,
fetchIdx,
......@@ -308,7 +313,7 @@ export function unstable_cache<T extends Callback>(
if (cacheEntry && cacheEntry.value) {
// The entry exists and has a value
if (cacheEntry.value.kind !== 'FETCH') {
if (cacheEntry.value.kind !== CachedRouteKind.FETCH) {
// The entry is invalid and we need a special warning
// @TODO why do we warn this way? Should this just be an error? How are these errors surfaced
// so bugs can be reported
......
......@@ -2,14 +2,16 @@ import { promises as fs } from 'node:fs'
import { fileURLToPath } from 'node:url'
import FileSystemCache from 'next/dist/server/lib/incremental-cache/file-system-cache'
import { nodeFs } from 'next/dist/server/lib/node-fs-methods'
import {
CachedRouteKind,
IncrementalCacheKind,
} from 'next/dist/server/response-cache'
const cacheDir = fileURLToPath(new URL('./cache', import.meta.url))
describe('FileSystemCache', () => {
it('set image route', async () => {
const fsCache = new FileSystemCache({
_appDir: true,
_pagesDir: true,
_requestHeaders: {},
flushToDisk: true,
fs: nodeFs,
......@@ -29,26 +31,30 @@ describe('FileSystemCache', () => {
'Content-Type': 'image/png',
},
status: 200,
kind: 'ROUTE',
kind: CachedRouteKind.APP_ROUTE,
},
{}
)
expect((await fsCache.get('icon.png'))?.value).toEqual({
expect(
(
await fsCache.get('icon.png', {
kind: IncrementalCacheKind.APP_ROUTE,
})
)?.value
).toEqual({
body: binary,
headers: {
'Content-Type': 'image/png',
},
status: 200,
kind: 'ROUTE',
kind: IncrementalCacheKind.APP_ROUTE,
})
})
})
describe('FileSystemCache (isrMemory 0)', () => {
const fsCache = new FileSystemCache({
_appDir: true,
_pagesDir: true,
_requestHeaders: {},
flushToDisk: true,
fs: nodeFs,
......@@ -61,7 +67,7 @@ describe('FileSystemCache (isrMemory 0)', () => {
await fsCache.set(
'fetch-cache',
{
kind: 'FETCH',
kind: CachedRouteKind.FETCH,
data: {
headers: {},
body: 'MTcwMDA1NjM4MQ==',
......@@ -81,7 +87,7 @@ describe('FileSystemCache (isrMemory 0)', () => {
const res = await fsCache.get('fetch-cache', {
tags: ['server-time'],
kindHint: 'fetch',
kind: IncrementalCacheKind.FETCH,
})
expect(res?.value).toEqual({
......@@ -101,7 +107,7 @@ describe('FileSystemCache (isrMemory 0)', () => {
await fsCache.set(
'unstable-cache',
{
kind: 'FETCH',
kind: CachedRouteKind.FETCH,
data: { headers: {}, body: '1700056381', status: 200, url: '' },
revalidate: 30,
},
......@@ -110,7 +116,7 @@ describe('FileSystemCache (isrMemory 0)', () => {
const res = await fsCache.get('unstable-cache', {
tags: ['server-time'],
kindHint: 'fetch',
kind: IncrementalCacheKind.FETCH,
})
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