Unverified Commit fc2a7d29 authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Make cacheLife profiles configurable (#71232)

This should really be a global config but we don't have an easy way to
get the config from a module. Might need a loader. So this just plumbs
it through a bunch of contexts.

I'm also generate the enum type based on the config.
parent 300bfe52
...@@ -4,3 +4,5 @@ export { ...@@ -4,3 +4,5 @@ export {
revalidatePath, revalidatePath,
} from 'next/dist/server/web/spec-extension/revalidate' } from 'next/dist/server/web/spec-extension/revalidate'
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store' export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { cacheLife as unstable_cacheLife } from 'next/dist/server/use-cache/cache-life'
export { cacheTag as unstable_cacheTag } from 'next/dist/server/use-cache/cache-tag'
...@@ -8,8 +8,9 @@ const cacheExports = { ...@@ -8,8 +8,9 @@ const cacheExports = {
unstable_noStore: unstable_noStore:
require('next/dist/server/web/spec-extension/unstable-no-store') require('next/dist/server/web/spec-extension/unstable-no-store')
.unstable_noStore, .unstable_noStore,
unstable_cacheLife: require('next/dist/server/use-cache/cache-life'), unstable_cacheLife: require('next/dist/server/use-cache/cache-life')
unstable_cacheTag: require('next/dist/server/use-cache/cache-tag'), .cacheLife,
unstable_cacheTag: require('next/dist/server/use-cache/cache-tag').cacheTag,
} }
// https://nodejs.org/api/esm.html#commonjs-namespaces // https://nodejs.org/api/esm.html#commonjs-namespaces
...@@ -21,3 +22,5 @@ exports.unstable_cache = cacheExports.unstable_cache ...@@ -21,3 +22,5 @@ exports.unstable_cache = cacheExports.unstable_cache
exports.revalidatePath = cacheExports.revalidatePath exports.revalidatePath = cacheExports.revalidatePath
exports.revalidateTag = cacheExports.revalidateTag exports.revalidateTag = cacheExports.revalidateTag
exports.unstable_noStore = cacheExports.unstable_noStore exports.unstable_noStore = cacheExports.unstable_noStore
exports.unstable_cacheLife = cacheExports.unstable_cacheLife
exports.unstable_cacheTag = cacheExports.unstable_cacheTag
...@@ -1217,6 +1217,7 @@ export async function buildAppStaticPaths({ ...@@ -1217,6 +1217,7 @@ export async function buildAppStaticPaths({
segments, segments,
isrFlushToDisk, isrFlushToDisk,
cacheHandler, cacheHandler,
cacheLifeProfiles,
requestHeaders, requestHeaders,
maxMemoryCacheSize, maxMemoryCacheSize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
...@@ -1235,6 +1236,9 @@ export async function buildAppStaticPaths({ ...@@ -1235,6 +1236,9 @@ export async function buildAppStaticPaths({
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
cacheHandler?: string cacheHandler?: string
cacheLifeProfiles?: {
[profile: string]: import('../server/use-cache/cache-life').CacheLife
}
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
...@@ -1305,6 +1309,7 @@ export async function buildAppStaticPaths({ ...@@ -1305,6 +1309,7 @@ export async function buildAppStaticPaths({
fallbackRouteParams: null, fallbackRouteParams: null,
renderOpts: { renderOpts: {
incrementalCache, incrementalCache,
cacheLifeProfiles,
supportsDynamicResponse: true, supportsDynamicResponse: true,
isRevalidate: false, isRevalidate: false,
experimental: { experimental: {
...@@ -1489,6 +1494,7 @@ export async function isPageStatic({ ...@@ -1489,6 +1494,7 @@ export async function isPageStatic({
maxMemoryCacheSize, maxMemoryCacheSize,
nextConfigOutput, nextConfigOutput,
cacheHandler, cacheHandler,
cacheLifeProfiles,
pprConfig, pprConfig,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId, buildId,
...@@ -1510,6 +1516,9 @@ export async function isPageStatic({ ...@@ -1510,6 +1516,9 @@ export async function isPageStatic({
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
cacheHandler?: string cacheHandler?: string
cacheLifeProfiles?: {
[profile: string]: import('../server/use-cache/cache-life').CacheLife
}
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
pprConfig: ExperimentalPPRConfig | undefined pprConfig: ExperimentalPPRConfig | undefined
isAppPPRFallbacksEnabled: boolean | undefined isAppPPRFallbacksEnabled: boolean | undefined
...@@ -1632,6 +1641,7 @@ export async function isPageStatic({ ...@@ -1632,6 +1641,7 @@ export async function isPageStatic({
isrFlushToDisk, isrFlushToDisk,
maxMemoryCacheSize, maxMemoryCacheSize,
cacheHandler, cacheHandler,
cacheLifeProfiles,
ComponentMod, ComponentMod,
nextConfigOutput, nextConfigOutput,
isRoutePPREnabled, isRoutePPREnabled,
......
...@@ -1931,6 +1931,7 @@ export default async function getBaseWebpackConfig( ...@@ -1931,6 +1931,7 @@ export default async function getBaseWebpackConfig(
isEdgeServer, isEdgeServer,
pageExtensions: config.pageExtensions, pageExtensions: config.pageExtensions,
typedRoutes: enableTypedRoutes, typedRoutes: enableTypedRoutes,
cacheLifeConfig: config.experimental.cacheLife,
originalRewrites, originalRewrites,
originalRedirects, originalRedirects,
}), }),
......
...@@ -12,6 +12,7 @@ describe('next-types-plugin', () => { ...@@ -12,6 +12,7 @@ describe('next-types-plugin', () => {
isEdgeServer: false, isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'], pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
typedRoutes: false, typedRoutes: false,
cacheLifeConfig: undefined,
originalRewrites: undefined, originalRewrites: undefined,
originalRedirects: undefined, originalRedirects: undefined,
}) })
...@@ -40,6 +41,7 @@ describe('next-types-plugin', () => { ...@@ -40,6 +41,7 @@ describe('next-types-plugin', () => {
isEdgeServer: false, isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'], pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
typedRoutes: false, typedRoutes: false,
cacheLifeConfig: undefined,
originalRewrites: undefined, originalRewrites: undefined,
originalRedirects: undefined, originalRedirects: undefined,
}) })
...@@ -60,6 +62,7 @@ describe('next-types-plugin', () => { ...@@ -60,6 +62,7 @@ describe('next-types-plugin', () => {
isEdgeServer: false, isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'], pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
typedRoutes: false, typedRoutes: false,
cacheLifeConfig: undefined,
originalRewrites: undefined, originalRewrites: undefined,
originalRedirects: undefined, originalRedirects: undefined,
}) })
......
...@@ -17,6 +17,7 @@ import { getPageFromPath } from '../../../entries' ...@@ -17,6 +17,7 @@ import { getPageFromPath } from '../../../entries'
import type { PageExtensions } from '../../../page-extensions-type' import type { PageExtensions } from '../../../page-extensions-type'
import { devPageFiles } from './shared' import { devPageFiles } from './shared'
import { getProxiedPluginState } from '../../../build-context' import { getProxiedPluginState } from '../../../build-context'
import type { CacheLife } from '../../../../server/use-cache/cache-life'
const PLUGIN_NAME = 'NextTypesPlugin' const PLUGIN_NAME = 'NextTypesPlugin'
...@@ -34,6 +35,7 @@ interface Options { ...@@ -34,6 +35,7 @@ interface Options {
isEdgeServer: boolean isEdgeServer: boolean
pageExtensions: PageExtensions pageExtensions: PageExtensions
typedRoutes: boolean typedRoutes: boolean
cacheLifeConfig: undefined | { [profile: string]: CacheLife }
originalRewrites: Rewrites | undefined originalRewrites: Rewrites | undefined
originalRedirects: Redirect[] | undefined originalRedirects: Redirect[] | undefined
} }
...@@ -525,6 +527,40 @@ declare module 'next/form' { ...@@ -525,6 +527,40 @@ declare module 'next/form' {
` `
} }
function createCustomCacheLifeDefinitions(cacheLife: {
[profile: string]: CacheLife
}) {
const profiles = Object.keys(cacheLife)
if (!profiles.includes('default')) {
profiles.push('default')
}
for (let i = 0; i < profiles.length; i++) {
profiles[i] = JSON.stringify(profiles[i])
}
// TODO: Annotate each option with their expanded values for IDE support.
const profilesEnum = profiles.join(' | ')
// Redefine the cacheLife() accepted arguments.
return `// Type definitions for Next.js cacheLife configs
declare module 'next/cache' {
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export {
revalidateTag,
revalidatePath,
} from 'next/dist/server/web/spec-extension/revalidate'
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
import type { CacheLife } from 'next/dist/server/use-cache/cache-life'
export function unstable_cacheLife(profile: ${profilesEnum} | CacheLife): void
export { cacheTag as unstable_cacheTag } from 'next/dist/server/use-cache/cache-tag'
}
`
}
const appTypesBasePath = path.join('types', 'app') const appTypesBasePath = path.join('types', 'app')
export class NextTypesPlugin { export class NextTypesPlugin {
...@@ -536,6 +572,7 @@ export class NextTypesPlugin { ...@@ -536,6 +572,7 @@ export class NextTypesPlugin {
pageExtensions: string[] pageExtensions: string[]
pagesDir: string pagesDir: string
typedRoutes: boolean typedRoutes: boolean
cacheLifeConfig: undefined | { [profile: string]: CacheLife }
distDirAbsolutePath: string distDirAbsolutePath: string
constructor(options: Options) { constructor(options: Options) {
...@@ -547,6 +584,7 @@ export class NextTypesPlugin { ...@@ -547,6 +584,7 @@ export class NextTypesPlugin {
this.pageExtensions = options.pageExtensions this.pageExtensions = options.pageExtensions
this.pagesDir = path.join(this.appDir, '..', 'pages') this.pagesDir = path.join(this.appDir, '..', 'pages')
this.typedRoutes = options.typedRoutes this.typedRoutes = options.typedRoutes
this.cacheLifeConfig = options.cacheLifeConfig
this.distDirAbsolutePath = path.join(this.dir, this.distDir) this.distDirAbsolutePath = path.join(this.dir, this.distDir)
if (this.typedRoutes && !redirectsRewritesTypesProcessed) { if (this.typedRoutes && !redirectsRewritesTypesProcessed) {
redirectsRewritesTypesProcessed = true redirectsRewritesTypesProcessed = true
...@@ -780,6 +818,17 @@ export class NextTypesPlugin { ...@@ -780,6 +818,17 @@ export class NextTypesPlugin {
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
} }
if (this.cacheLifeConfig) {
const cacheLifeAssetPath = path.join(
assetDirRelative,
'types/cache-life.d.ts'
)
assets[cacheLifeAssetPath] = new sources.RawSource(
createCustomCacheLifeDefinitions(this.cacheLifeConfig)
) as unknown as webpack.sources.RawSource
}
callback() callback()
} }
) )
......
...@@ -341,6 +341,7 @@ async function exportAppImpl( ...@@ -341,6 +341,7 @@ async function exportAppImpl(
largePageDataBytes: nextConfig.experimental.largePageDataBytes, largePageDataBytes: nextConfig.experimental.largePageDataBytes,
serverActions: nextConfig.experimental.serverActions, serverActions: nextConfig.experimental.serverActions,
serverComponents: enabledDirectories.app, serverComponents: enabledDirectories.app,
cacheLifeProfiles: nextConfig.experimental.cacheLife,
nextFontManifest: require( nextFontManifest: require(
join(distDir, 'server', `${NEXT_FONT_MANIFEST}.json`) join(distDir, 'server', `${NEXT_FONT_MANIFEST}.json`)
), ),
......
...@@ -40,6 +40,11 @@ export async function exportAppRoute( ...@@ -40,6 +40,11 @@ export async function exportAppRoute(
page: string, page: string,
module: AppRouteRouteModule, module: AppRouteRouteModule,
incrementalCache: IncrementalCache | undefined, incrementalCache: IncrementalCache | undefined,
cacheLifeProfiles:
| undefined
| {
[profile: string]: import('../../server/use-cache/cache-life').CacheLife
},
htmlFilepath: string, htmlFilepath: string,
fileWriter: FileWriter, fileWriter: FileWriter,
experimental: Required<Pick<ExperimentalConfig, 'after' | 'dynamicIO'>>, experimental: Required<Pick<ExperimentalConfig, 'after' | 'dynamicIO'>>,
...@@ -79,6 +84,7 @@ export async function exportAppRoute( ...@@ -79,6 +84,7 @@ export async function exportAppRoute(
waitUntil: afterRunner.context.waitUntil, waitUntil: afterRunner.context.waitUntil,
onClose: afterRunner.context.onClose, onClose: afterRunner.context.onClose,
onAfterTaskError: afterRunner.context.onTaskError, onAfterTaskError: afterRunner.context.onTaskError,
cacheLifeProfiles,
buildId, buildId,
}, },
} }
......
...@@ -251,6 +251,7 @@ async function exportPageImpl( ...@@ -251,6 +251,7 @@ async function exportPageImpl(
page, page,
components.routeModule as AppRouteRouteModule, components.routeModule as AppRouteRouteModule,
input.renderOpts.incrementalCache, input.renderOpts.incrementalCache,
input.renderOpts.cacheLifeProfiles,
htmlFilepath, htmlFilepath,
fileWriter, fileWriter,
input.renderOpts.experimental, input.renderOpts.experimental,
......
...@@ -147,6 +147,9 @@ export interface RenderOptsPartial { ...@@ -147,6 +147,9 @@ export interface RenderOptsPartial {
nextFontManifest?: DeepReadonly<NextFontManifest> nextFontManifest?: DeepReadonly<NextFontManifest>
isBot?: boolean isBot?: boolean
incrementalCache?: import('../lib/incremental-cache').IncrementalCache incrementalCache?: import('../lib/incremental-cache').IncrementalCache
cacheLifeProfiles?: {
[profile: string]: import('../use-cache/cache-life').CacheLife
}
setAppIsrStatus?: (key: string, value: boolean | null) => void setAppIsrStatus?: (key: string, value: boolean | null) => void
isRevalidate?: boolean isRevalidate?: boolean
nextExport?: boolean nextExport?: boolean
......
...@@ -6,6 +6,7 @@ import type { FallbackRouteParams } from '../request/fallback-params' ...@@ -6,6 +6,7 @@ import type { FallbackRouteParams } from '../request/fallback-params'
import type { DeepReadonly } from '../../shared/lib/deep-readonly' import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config' import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
import type { AfterContext } from '../after/after-context' import type { AfterContext } from '../after/after-context'
import type { CacheLife } from '../use-cache/cache-life'
// Share the instance module in the next-shared layer // Share the instance module in the next-shared layer
import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' } import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
...@@ -31,6 +32,8 @@ export interface WorkStore { ...@@ -31,6 +32,8 @@ export interface WorkStore {
readonly fallbackRouteParams: FallbackRouteParams | null readonly fallbackRouteParams: FallbackRouteParams | null
readonly incrementalCache?: IncrementalCache readonly incrementalCache?: IncrementalCache
readonly cacheLifeProfiles?: { [profile: string]: CacheLife }
readonly isOnDemandRevalidate?: boolean readonly isOnDemandRevalidate?: boolean
readonly isPrerendering?: boolean readonly isPrerendering?: boolean
readonly isRevalidate?: boolean readonly isRevalidate?: boolean
......
...@@ -7,6 +7,7 @@ import type { FetchMetric } from '../base-http' ...@@ -7,6 +7,7 @@ import type { FetchMetric } from '../base-http'
import type { RequestLifecycleOpts } from '../base-server' import type { RequestLifecycleOpts } from '../base-server'
import type { FallbackRouteParams } from '../request/fallback-params' import type { FallbackRouteParams } from '../request/fallback-params'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config' import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
import type { CacheLife } from '../use-cache/cache-life'
import { AfterContext } from '../after/after-context' import { AfterContext } from '../after/after-context'
...@@ -26,6 +27,7 @@ export type WorkStoreContext = { ...@@ -26,6 +27,7 @@ export type WorkStoreContext = {
requestEndedState?: { ended?: boolean } requestEndedState?: { ended?: boolean }
isPrefetchRequest?: boolean isPrefetchRequest?: boolean
renderOpts: { renderOpts: {
cacheLifeProfiles?: { [profile: string]: CacheLife }
incrementalCache?: IncrementalCache incrementalCache?: IncrementalCache
isOnDemandRevalidate?: boolean isOnDemandRevalidate?: boolean
fetchCache?: AppSegmentConfig['fetchCache'] fetchCache?: AppSegmentConfig['fetchCache']
...@@ -108,6 +110,7 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>( ...@@ -108,6 +110,7 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>(
// we fallback to a global incremental cache for edge-runtime locally // we fallback to a global incremental cache for edge-runtime locally
// so that it can access the fs cache without mocks // so that it can access the fs cache without mocks
renderOpts.incrementalCache || (globalThis as any).__incrementalCache, renderOpts.incrementalCache || (globalThis as any).__incrementalCache,
cacheLifeProfiles: renderOpts.cacheLifeProfiles,
isRevalidate: renderOpts.isRevalidate, isRevalidate: renderOpts.isRevalidate,
isPrerendering: renderOpts.nextExport, isPrerendering: renderOpts.nextExport,
fetchCache: renderOpts.fetchCache, fetchCache: renderOpts.fetchCache,
......
...@@ -581,6 +581,7 @@ export default abstract class Server< ...@@ -581,6 +581,7 @@ export default abstract class Server<
domainLocales: this.nextConfig.i18n?.domains, domainLocales: this.nextConfig.i18n?.domains,
distDir: this.distDir, distDir: this.distDir,
serverComponents: this.enabledDirectories.app, serverComponents: this.enabledDirectories.app,
cacheLifeProfiles: this.nextConfig.experimental.cacheLife,
enableTainting: this.nextConfig.experimental.taint, enableTainting: this.nextConfig.experimental.taint,
crossOrigin: this.nextConfig.crossOrigin crossOrigin: this.nextConfig.crossOrigin
? this.nextConfig.crossOrigin ? this.nextConfig.crossOrigin
...@@ -2513,6 +2514,7 @@ export default abstract class Server< ...@@ -2513,6 +2514,7 @@ export default abstract class Server<
}, },
supportsDynamicResponse, supportsDynamicResponse,
incrementalCache, incrementalCache,
cacheLifeProfiles: this.nextConfig.experimental?.cacheLife,
isRevalidate: isSSG, isRevalidate: isSSG,
waitUntil: this.getWaitUntil(), waitUntil: this.getWaitUntil(),
onClose: res.onClose.bind(res), onClose: res.onClose.bind(res),
......
...@@ -270,6 +270,15 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -270,6 +270,15 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
static: z.number().optional(), static: z.number().optional(),
}) })
.optional(), .optional(),
cacheLife: z
.record(
z.object({
stale: z.number().optional(),
revalidate: z.number().optional(),
expire: z.number().optional(),
})
)
.optional(),
clientRouterFilter: z.boolean().optional(), clientRouterFilter: z.boolean().optional(),
clientRouterFilterRedirects: z.boolean().optional(), clientRouterFilterRedirects: z.boolean().optional(),
clientRouterFilterAllowedRate: z.number().optional(), clientRouterFilterAllowedRate: z.number().optional(),
......
...@@ -13,6 +13,7 @@ import type { SizeLimit } from '../types' ...@@ -13,6 +13,7 @@ import type { SizeLimit } from '../types'
import type { ExpireTime } from './lib/revalidate' import type { ExpireTime } from './lib/revalidate'
import type { SupportedTestRunners } from '../cli/next-test' import type { SupportedTestRunners } from '../cli/next-test'
import type { ExperimentalPPRConfig } from './lib/experimental/ppr' import type { ExperimentalPPRConfig } from './lib/experimental/ppr'
import { INFINITE_CACHE } from '../lib/constants'
export type NextConfigComplete = Required<NextConfig> & { export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete> images: Required<ImageConfigComplete>
...@@ -258,6 +259,19 @@ export interface ExperimentalConfig { ...@@ -258,6 +259,19 @@ export interface ExperimentalConfig {
dynamic?: number dynamic?: number
static?: number static?: number
} }
cacheLife?: {
[profile: string]: {
// How long the client can cache a value without checking with the server.
stale?: number
// How frequently you want the cache to refresh on the server.
// Stale values may be served while revalidating.
revalidate?: number
// In the worst case scenario, where you haven't had traffic in a while,
// how stale can a value be until you prefer deopting to dynamic.
// Must be longer than revalidate.
expire?: number
}
}
// decimal for percent for possible false positives // decimal for percent for possible false positives
// e.g. 0.01 for 10% potential false matches lower // e.g. 0.01 for 10% potential false matches lower
// percent increases size of the filter // percent increases size of the filter
...@@ -1014,6 +1028,13 @@ export const defaultConfig: NextConfig = { ...@@ -1014,6 +1028,13 @@ export const defaultConfig: NextConfig = {
modularizeImports: undefined, modularizeImports: undefined,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '', outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '',
experimental: { experimental: {
cacheLife: {
default: {
stale: undefined, // defaults to staleTimes.static
revalidate: 900,
expire: INFINITE_CACHE,
},
},
multiZoneDraftMode: false, multiZoneDraftMode: false,
appNavFailHandling: Boolean(process.env.NEXT_PRIVATE_FLYING_SHUTTLE), appNavFailHandling: Boolean(process.env.NEXT_PRIVATE_FLYING_SHUTTLE),
flyingShuttle: Boolean(process.env.NEXT_PRIVATE_FLYING_SHUTTLE) flyingShuttle: Boolean(process.env.NEXT_PRIVATE_FLYING_SHUTTLE)
......
...@@ -807,6 +807,35 @@ function assignDefaults( ...@@ -807,6 +807,35 @@ function assignDefaults(
} }
} }
if (result.experimental?.cacheLife) {
const defaultDefault = defaultConfig.experimental?.cacheLife?.['default']
if (
!defaultDefault ||
defaultDefault.revalidate === undefined ||
defaultDefault.expire === undefined ||
!defaultConfig.experimental?.staleTimes?.static
) {
throw new Error('No default cacheLife profile.')
}
const defaultCacheLifeProfile = result.experimental.cacheLife['default']
if (!defaultCacheLifeProfile) {
result.experimental.cacheLife['default'] = defaultDefault
} else {
if (defaultCacheLifeProfile.stale === undefined) {
const staticStaleTime = result.experimental.staleTimes?.static
defaultCacheLifeProfile.stale =
staticStaleTime ?? defaultConfig.experimental?.staleTimes?.static
}
if (defaultCacheLifeProfile.revalidate === undefined) {
defaultCacheLifeProfile.revalidate = defaultDefault.revalidate
}
if (defaultCacheLifeProfile.expire === undefined) {
defaultCacheLifeProfile.expire =
result.expireTime ?? defaultDefault.expire
}
}
}
const userProvidedModularizeImports = result.modularizeImports const userProvidedModularizeImports = result.modularizeImports
// Unfortunately these packages end up re-exporting 10600 modules, for example: https://unpkg.com/browse/@mui/icons-material@5.11.16/esm/index.js. // Unfortunately these packages end up re-exporting 10600 modules, for example: https://unpkg.com/browse/@mui/icons-material@5.11.16/esm/index.js.
// Leveraging modularizeImports tremendously reduces compile times for these. // Leveraging modularizeImports tremendously reduces compile times for these.
......
...@@ -46,6 +46,7 @@ export async function loadStaticPaths({ ...@@ -46,6 +46,7 @@ export async function loadStaticPaths({
maxMemoryCacheSize, maxMemoryCacheSize,
requestHeaders, requestHeaders,
cacheHandler, cacheHandler,
cacheLifeProfiles,
nextConfigOutput, nextConfigOutput,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId, buildId,
...@@ -64,6 +65,9 @@ export async function loadStaticPaths({ ...@@ -64,6 +65,9 @@ export async function loadStaticPaths({
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
cacheHandler?: string cacheHandler?: string
cacheLifeProfiles?: {
[profile: string]: import('../../server/use-cache/cache-life').CacheLife
}
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
isAppPPRFallbacksEnabled: boolean | undefined isAppPPRFallbacksEnabled: boolean | undefined
buildId: string buildId: string
...@@ -97,6 +101,7 @@ export async function loadStaticPaths({ ...@@ -97,6 +101,7 @@ export async function loadStaticPaths({
distDir, distDir,
requestHeaders, requestHeaders,
cacheHandler, cacheHandler,
cacheLifeProfiles,
isrFlushToDisk, isrFlushToDisk,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
maxMemoryCacheSize, maxMemoryCacheSize,
......
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external' import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
export type CacheLife = { export type CacheLife = {
...@@ -15,20 +16,10 @@ export type CacheLife = { ...@@ -15,20 +16,10 @@ export type CacheLife = {
// Cache-Control: max-age=[stale],s-max-age=[revalidate],stale-while-revalidate=[expire-revalidate],stale-if-error=[expire-revalidate] // Cache-Control: max-age=[stale],s-max-age=[revalidate],stale-while-revalidate=[expire-revalidate],stale-if-error=[expire-revalidate]
// Except that stale-while-revalidate/stale-if-error only applies to shared caches - not private caches. // Except that stale-while-revalidate/stale-if-error only applies to shared caches - not private caches.
const cacheLifeProfileMap: Map<string, CacheLife> = new Map()
// The default revalidates relatively frequently but doesn't expire to ensure it's always // The default revalidates relatively frequently but doesn't expire to ensure it's always
// able to serve fast results but by default doesn't hang. // able to serve fast results but by default doesn't hang.
export const defaultCacheLife = { type CacheLifeProfiles = string // This gets overridden by the next-types-plugin
stale: Number(process.env.__NEXT_CLIENT_ROUTER_STATIC_STALETIME),
revalidate: 15 * 60, // Note: This is a new take on the defaults.
expire: Infinity,
}
cacheLifeProfileMap.set('default', defaultCacheLife)
type CacheLifeProfiles = 'default' // TODO: Generate from the config
function validateCacheLife(profile: CacheLife) { function validateCacheLife(profile: CacheLife) {
if (profile.stale !== undefined) { if (profile.stale !== undefined) {
...@@ -99,9 +90,22 @@ export function cacheLife(profile: CacheLifeProfiles | CacheLife): void { ...@@ -99,9 +90,22 @@ export function cacheLife(profile: CacheLifeProfiles | CacheLife): void {
} }
if (typeof profile === 'string') { if (typeof profile === 'string') {
const configuredProfile = cacheLifeProfileMap.get(profile) const workStore = workAsyncStorage.getStore()
if (!workStore) {
throw new Error(
'cacheLife() can only be called during App Router rendering at the moment.'
)
}
if (!workStore.cacheLifeProfiles) {
throw new Error(
'cacheLifeProfiles should always be provided. This is a bug in Next.js.'
)
}
// TODO: This should be globally available and not require an AsyncLocalStorage.
const configuredProfile = workStore.cacheLifeProfiles[profile]
if (configuredProfile === undefined) { if (configuredProfile === undefined) {
if (cacheLifeProfileMap.has(profile.trim())) { if (workStore.cacheLifeProfiles[profile.trim()]) {
throw new Error( throw new Error(
`Unknown cacheLife profile "${profile}" is not configured in next.config.js\n` + `Unknown cacheLife profile "${profile}" is not configured in next.config.js\n` +
`Did you mean "${profile.trim()}" without the spaces?` `Did you mean "${profile.trim()}" without the spaces?`
......
...@@ -29,7 +29,6 @@ import { ...@@ -29,7 +29,6 @@ import {
getClientReferenceManifestSingleton, getClientReferenceManifestSingleton,
getServerModuleMap, getServerModuleMap,
} from '../app-render/encryption-utils' } from '../app-render/encryption-utils'
import { defaultCacheLife } from './cache-life'
import type { CacheScopeStore } from '../async-storage/cache-scope.external' import type { CacheScopeStore } from '../async-storage/cache-scope.external'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
...@@ -60,8 +59,9 @@ const defaultCacheStorage: Map<string, Promise<CacheEntry>> = new Map() ...@@ -60,8 +59,9 @@ const defaultCacheStorage: Map<string, Promise<CacheEntry>> = new Map()
cacheHandlerMap.set('default', { cacheHandlerMap.set('default', {
async get(cacheKey: string): Promise<undefined | CacheEntry> { async get(cacheKey: string): Promise<undefined | CacheEntry> {
// TODO: Implement proper caching. // TODO: Implement proper caching.
const entry = await defaultCacheStorage.get(cacheKey) const promiseOfEntry = defaultCacheStorage.get(cacheKey)
if (entry !== undefined) { if (promiseOfEntry !== undefined) {
const entry = await promiseOfEntry
if ( if (
performance.timeOrigin + performance.now() > performance.timeOrigin + performance.now() >
entry.timestamp + entry.revalidate * 1000 entry.timestamp + entry.revalidate * 1000
...@@ -76,7 +76,7 @@ cacheHandlerMap.set('default', { ...@@ -76,7 +76,7 @@ cacheHandlerMap.set('default', {
value: returnStream, value: returnStream,
timestamp: entry.timestamp, timestamp: entry.timestamp,
revalidate: entry.revalidate, revalidate: entry.revalidate,
expire: entry.revalidate, expire: entry.expire,
stale: entry.stale, stale: entry.stale,
tags: entry.tags, tags: entry.tags,
} }
...@@ -172,6 +172,22 @@ function generateCacheEntryWithCacheContext( ...@@ -172,6 +172,22 @@ function generateCacheEntryWithCacheContext(
encodedArguments: FormData | string, encodedArguments: FormData | string,
fn: any fn: any
) { ) {
if (!workStore.cacheLifeProfiles) {
throw new Error(
'cacheLifeProfiles should always be provided. This is a bug in Next.js.'
)
}
const defaultCacheLife = workStore.cacheLifeProfiles['default']
if (
!defaultCacheLife ||
defaultCacheLife.revalidate == null ||
defaultCacheLife.expire == null ||
defaultCacheLife.stale == null
) {
throw new Error(
'A default cacheLife profile must always be provided. This is a bug in Next.js.'
)
}
// Initialize the Store for this Cache entry. // Initialize the Store for this Cache entry.
const cacheStore: UseCacheStore = { const cacheStore: UseCacheStore = {
type: 'cache', type: 'cache',
...@@ -434,7 +450,7 @@ async function loadCacheEntry( ...@@ -434,7 +450,7 @@ async function loadCacheEntry(
} else { } else {
propagateCacheLifeAndTags(workUnitStore, entry) propagateCacheLifeAndTags(workUnitStore, entry)
if (currentTime > entry.timestamp + entry.revalidate) { if (currentTime > entry.timestamp + entry.revalidate * 1000) {
// If this is stale, and we're not in a prerender (i.e. this is dynamic render), // If this is stale, and we're not in a prerender (i.e. this is dynamic render),
// then we should warm up the cache with a fresh revalidated entry. // then we should warm up the cache with a fresh revalidated entry.
const ignoredStream = await generateCacheEntry( const ignoredStream = await generateCacheEntry(
...@@ -530,9 +546,10 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -530,9 +546,10 @@ export function cache(kind: string, id: string, fn: any) {
let stream: ReadableStream let stream: ReadableStream
const disable = true // TODO: Reenable the cache scope
const cacheScope: undefined | CacheScopeStore = const cacheScope: undefined | CacheScopeStore =
cacheScopeAsyncLocalStorage.getStore() cacheScopeAsyncLocalStorage.getStore()
if (cacheScope) { if (cacheScope && !disable) {
// String cache key for easier hash mapping. // String cache key for easier hash mapping.
// Note that we're not worried about collisions between string and base64 // Note that we're not worried about collisions between string and base64
// since the string form will always be JSON which doesn't overlap with base64. // since the string form will always be JSON which doesn't overlap with base64.
......
...@@ -255,6 +255,8 @@ export async function adapter( ...@@ -255,6 +255,8 @@ export async function adapter(
page: '/', // Fake Work page: '/', // Fake Work
fallbackRouteParams: null, fallbackRouteParams: null,
renderOpts: { renderOpts: {
cacheLifeProfiles:
params.request.nextConfig?.experimental?.cacheLife,
experimental: { experimental: {
after: isAfterEnabled, after: isAfterEnabled,
isRoutePPREnabled: false, isRoutePPREnabled: false,
......
...@@ -15,7 +15,7 @@ export interface RequestData { ...@@ -15,7 +15,7 @@ export interface RequestData {
basePath?: string basePath?: string
i18n?: I18NConfig | null i18n?: I18NConfig | null
trailingSlash?: boolean trailingSlash?: boolean
experimental?: Pick<ExperimentalConfig, 'after'> experimental?: Pick<ExperimentalConfig, 'after' | 'cacheLife'>
} }
page?: { page?: {
name?: string name?: string
......
...@@ -5,6 +5,8 @@ async function getCachedRandom() { ...@@ -5,6 +5,8 @@ async function getCachedRandom() {
export async function GET() { export async function GET() {
const rand1 = await getCachedRandom() const rand1 = await getCachedRandom()
// TODO: Remove this extra micro task when bug in use cache wrapper is fixed.
await Promise.resolve()
const rand2 = await getCachedRandom() const rand2 = await getCachedRandom()
const response = JSON.stringify({ rand1, rand2 }) const response = JSON.stringify({ rand1, rand2 })
......
import { unstable_cacheLife as cacheLife } from 'next/cache'
async function getCachedRandom() {
'use cache'
cacheLife('frequent')
return Math.random()
}
export default async function Page() {
const x = await getCachedRandom()
return <p id="x">{x}</p>
}
import { unstable_cacheTag as cacheTag } from 'next/cache'
async function getCachedWithTag(tag) {
'use cache'
cacheTag(tag, 'c')
return Math.random()
}
export default async function Page() {
const x = await getCachedWithTag('a')
const y = await getCachedWithTag('b')
return (
<p>
{x}
{y}
</p>
)
}
...@@ -4,6 +4,11 @@ ...@@ -4,6 +4,11 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
cacheLife: {
frequent: {
revalidate: 100,
},
},
}, },
} }
......
...@@ -5,7 +5,8 @@ const GENERIC_RSC_ERROR = ...@@ -5,7 +5,8 @@ const GENERIC_RSC_ERROR =
'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.' 'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.'
describe('use-cache', () => { describe('use-cache', () => {
const { next, isNextDev, isNextDeploy, isTurbopack } = nextTestSetup({ const { next, isNextDev, isNextDeploy, isNextStart, isTurbopack } =
nextTestSetup({
files: __dirname, files: __dirname,
}) })
...@@ -81,4 +82,30 @@ describe('use-cache', () => { ...@@ -81,4 +82,30 @@ describe('use-cache', () => {
expect(rand1).toEqual(rand2) expect(rand1).toEqual(rand2)
}) })
if (isNextStart) {
itSkipTurbopack(
'should match the expected revalidate config on the prerender manifest',
async () => {
const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
expect(prerenderManifest.version).toBe(4)
expect(
prerenderManifest.routes['/cache-life'].initialRevalidateSeconds
).toBe(100)
}
)
itSkipTurbopack(
'should propagate unstable_cache tags correctly',
async () => {
const meta = JSON.parse(
await next.readFile('.next/server/app/cache-tag.meta')
)
expect(meta.headers['x-next-cache-tags']).toContain('a,c,b')
}
)
}
}) })
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