Unverified Commit 16dcebe7 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Stabilize custom cache handlers and changing memory size. (#57953)

This PR stabilizes the previously introduced experimental config options
for providing a custom cache handler (for both ISR as well as the Data
Cache) and for disabling or configuring the in-memory cache size. The
example usage would be as follows:

```js
// next.config.js
module.exports = {
  cacheHandler: require.resolve('./cache-handler.js'),
  cacheMaxMemorySize: 0 // disable default in-memory caching
}
```

This PR also updates the documentation to better reflect how to use the
custom cache handler when self-hosting. Further information will be
added in a following PR that also includes a full example of a custom
cache handler that implements `revalidateTag` as well as passing in
custom cache tags. The API reference docs have been updated here, as
well as a version history added.

I also noticed that we currently have two duplicated versions of the ISR
docs in the Pages Router docs: both for rendering and for data fetching.
Data Fetching is the correct location for this page. There were no other
references to the rendering version in the docs, so that must have been
an accident. I'll need to a get a redirect going for that regardless.

Tests have been updated for `cacheHandler` and I added a new test for
`cacheMaxMemorySize`.

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 8e216ecb
...@@ -146,10 +146,8 @@ To configure the ISR/Data Cache location when self-hosting, you can configure a ...@@ -146,10 +146,8 @@ To configure the ISR/Data Cache location when self-hosting, you can configure a
```jsx filename="next.config.js" ```jsx filename="next.config.js"
module.exports = { module.exports = {
experimental: { cacheHandler: require.resolve('./cache-handler.js'),
incrementalCacheHandlerPath: require.resolve('./cache-handler.js'), cacheMaxMemorySize: 0, // disable default in-memory caching
isrMemoryCacheSize: 0, // disable default in-memory caching
},
} }
``` ```
......
--- ---
title: incrementalCacheHandlerPath title: Custom Next.js Cache Handler
description: Configure the Next.js cache used for storing and revalidating data. nav_title: cacheHandler
description: Configure the Next.js cache used for storing and revalidating data to use any external service like Redis, Memcached, or others.
--- ---
In Next.js, the [default cache handler](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) uses the filesystem cache. This requires no configuration, however, you can customize the cache handler by using the `incrementalCacheHandlerPath` field in `next.config.js`. In Next.js, the [default cache handler](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) for the Pages and App Router uses the filesystem cache. This requires no configuration, however, you can customize the cache handler by using the `cacheHandler` field in `next.config.js`.
```js filename="next.config.js" ```js filename="next.config.js"
module.exports = { module.exports = {
experimental: { cacheHandler: require.resolve('./cache-handler.js'),
incrementalCacheHandlerPath: require.resolve('./cache-handler.js'), cacheMaxMemorySize: 0, // disable default in-memory caching
},
} }
``` ```
Here's an example of a custom cache handler: View an example of a [custom cache handler](/docs/app/building-your-application/deploying#configuring-caching) and learn more about implementation.
```js filename="cache-handler.js"
const cache = new Map()
module.exports = class CacheHandler {
constructor(options) {
this.options = options
this.cache = {}
}
async get(key) {
return cache.get(key)
}
async set(key, data) {
cache.set(key, {
value: data,
lastModified: Date.now(),
})
}
}
```
## API Reference ## API Reference
...@@ -55,6 +33,7 @@ Returns the cached value or `null` if not found. ...@@ -55,6 +33,7 @@ Returns the cached value or `null` if not found.
| --------- | -------------- | -------------------------------- | | --------- | -------------- | -------------------------------- |
| `key` | `string` | The key to store the data under. | | `key` | `string` | The key to store the data under. |
| `data` | Data or `null` | The data to be cached. | | `data` | Data or `null` | The data to be cached. |
| `ctx` | `{ tags: [] }` | The cache tags provided. |
Returns `Promise<void>`. Returns `Promise<void>`.
...@@ -65,3 +44,16 @@ Returns `Promise<void>`. ...@@ -65,3 +44,16 @@ Returns `Promise<void>`.
| `tag` | `string` | The cache tag to revalidate. | | `tag` | `string` | The cache tag to revalidate. |
Returns `Promise<void>`. Learn more about [revalidating data](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) or the [`revalidateTag()`](/docs/app/api-reference/functions/revalidateTag) function. Returns `Promise<void>`. Learn more about [revalidating data](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) or the [`revalidateTag()`](/docs/app/api-reference/functions/revalidateTag) function.
**Good to know:**
- `revalidatePath` is a convenience layer on top of cache tags. Calling `revalidatePath` will call your `revalidateTag` function, which you can then choose if you want to tag cache keys based on the path.
## Version History
| Version | Changes |
| --------- | ------------------------------------------------------------------------ |
| `v14.1.0` | Renamed `cacheHandler` is stable. |
| `v13.4.0` | `incrementalCacheHandlerPath` (experimental) supports `revalidateTag`. |
| `v13.4.0` | `incrementalCacheHandlerPath` (experimental) supports standalone output. |
| `v12.2.0` | `incrementalCacheHandlerPath` (experimental) is added. |
--- ---
title: Incremental Static Regeneration title: Incremental Static Regeneration (ISR)
description: 'Learn how to create or update static pages at runtime with Incremental Static Regeneration.' description: Learn how to create or update static pages at runtime with Incremental Static Regeneration.
--- ---
<details> <details>
...@@ -169,29 +169,13 @@ export async function getStaticProps() { ...@@ -169,29 +169,13 @@ export async function getStaticProps() {
Incremental Static Regeneration (ISR) works on [self-hosted Next.js sites](/docs/pages/building-your-application/deploying#self-hosting) out of the box when you use `next start`. Incremental Static Regeneration (ISR) works on [self-hosted Next.js sites](/docs/pages/building-your-application/deploying#self-hosting) out of the box when you use `next start`.
You can use this approach when deploying to container orchestrators such as [Kubernetes](https://kubernetes.io/) or [HashiCorp Nomad](https://www.nomadproject.io/). By default, generated assets will be stored in-memory on each pod. This means that each pod will have its own copy of the static files. Stale data may be shown until that specific pod is hit by a request. Learn more about [self-hosting Next.js](/docs/pages/building-your-application/deploying#self-hosting).
To ensure consistency across all pods, you can disable in-memory caching. This will inform the Next.js server to only leverage assets generated by ISR in the file system.
You can use a shared network mount in your Kubernetes pods (or similar setup) to reuse the same file-system cache between different containers. By sharing the same mount, the `.next` folder which contains the `next/image` cache will also be shared and re-used.
To disable in-memory caching, set `isrMemoryCacheSize` to `0` in your `next.config.js` file:
```js filename="next.config.js"
module.exports = {
experimental: {
// Defaults to 50MB
isrMemoryCacheSize: 0, // cache size in bytes
},
}
```
> **Good to know**: You might need to consider a race condition between multiple pods trying to update the cache at the same time, depending on how your shared mount is configured.
## Version History ## Version History
| Version | Changes | | Version | Changes |
| --------- | --------------------------------------------------------------------------------------- | | --------- | --------------------------------------------------------------------------------------- |
| `v14.1.0` | Custom `cacheHandler` is stable. |
| `v12.2.0` | On-Demand ISR is stable | | `v12.2.0` | On-Demand ISR is stable |
| `v12.1.0` | On-Demand ISR added (beta). | | `v12.1.0` | On-Demand ISR added (beta). |
| `v12.0.0` | [Bot-aware ISR fallback](https://nextjs.org/blog/next-12#bot-aware-isr-fallback) added. | | `v12.0.0` | [Bot-aware ISR fallback](https://nextjs.org/blog/next-12#bot-aware-isr-fallback) added. |
......
...@@ -239,16 +239,16 @@ export async function collectBuildTraces({ ...@@ -239,16 +239,16 @@ export async function collectBuildTraces({
)), )),
] ]
const { incrementalCacheHandlerPath } = config.experimental const { cacheHandler } = config
// ensure we trace any dependencies needed for custom // ensure we trace any dependencies needed for custom
// incremental cache handler // incremental cache handler
if (incrementalCacheHandlerPath) { if (cacheHandler) {
sharedEntriesSet.push( sharedEntriesSet.push(
require.resolve( require.resolve(
path.isAbsolute(incrementalCacheHandlerPath) path.isAbsolute(cacheHandler)
? incrementalCacheHandlerPath ? cacheHandler
: path.join(dir, incrementalCacheHandlerPath) : path.join(dir, cacheHandler)
) )
) )
} }
......
...@@ -414,8 +414,7 @@ export function getEdgeServerEntry(opts: { ...@@ -414,8 +414,7 @@ export function getEdgeServerEntry(opts: {
pagesType: opts.pagesType, pagesType: opts.pagesType,
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'), appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm, sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm,
incrementalCacheHandlerPath: cacheHandler: opts.config.cacheHandler,
opts.config.experimental.incrementalCacheHandlerPath,
preferredRegion: opts.preferredRegion, preferredRegion: opts.preferredRegion,
middlewareConfig: Buffer.from( middlewareConfig: Buffer.from(
JSON.stringify(opts.middlewareConfig || {}) JSON.stringify(opts.middlewareConfig || {})
......
...@@ -1245,7 +1245,7 @@ export default async function build( ...@@ -1245,7 +1245,7 @@ export default async function build(
PAGES_MANIFEST PAGES_MANIFEST
) )
const { incrementalCacheHandlerPath } = config.experimental const { cacheHandler } = config
const requiredServerFilesManifest = nextBuildSpan const requiredServerFilesManifest = nextBuildSpan
.traceChild('generate-required-server-files') .traceChild('generate-required-server-files')
...@@ -1260,12 +1260,12 @@ export default async function build( ...@@ -1260,12 +1260,12 @@ export default async function build(
compress: false, compress: false,
} }
: {}), : {}),
cacheHandler: cacheHandler
? path.relative(distDir, cacheHandler)
: config.cacheHandler,
experimental: { experimental: {
...config.experimental, ...config.experimental,
trustHostHeader: ciEnvironment.hasNextSupport, trustHostHeader: ciEnvironment.hasNextSupport,
incrementalCacheHandlerPath: incrementalCacheHandlerPath
? path.relative(distDir, incrementalCacheHandlerPath)
: undefined,
// @ts-expect-error internal field TODO: fix this, should use a separate mechanism to pass the info. // @ts-expect-error internal field TODO: fix this, should use a separate mechanism to pass the info.
isExperimentalCompile: isCompileMode, isExperimentalCompile: isCompileMode,
...@@ -1523,11 +1523,11 @@ export default async function build( ...@@ -1523,11 +1523,11 @@ export default async function build(
if (config.experimental.staticWorkerRequestDeduping) { if (config.experimental.staticWorkerRequestDeduping) {
let CacheHandler let CacheHandler
if (incrementalCacheHandlerPath) { if (cacheHandler) {
CacheHandler = interopDefault( CacheHandler = interopDefault(
await import( await import(formatDynamicImportPath(dir, cacheHandler)).then(
formatDynamicImportPath(dir, incrementalCacheHandlerPath) (mod) => mod.default || mod
).then((mod) => mod.default || mod) )
) )
} }
...@@ -1542,7 +1542,7 @@ export default async function build( ...@@ -1542,7 +1542,7 @@ export default async function build(
: config.experimental.isrFlushToDisk, : config.experimental.isrFlushToDisk,
serverDistDir: path.join(distDir, 'server'), serverDistDir: path.join(distDir, 'server'),
fetchCacheKeyPrefix: config.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: config.experimental.fetchCacheKeyPrefix,
maxMemoryCacheSize: config.experimental.isrMemoryCacheSize, maxMemoryCacheSize: config.cacheMaxMemorySize,
getPrerenderManifest: () => ({ getPrerenderManifest: () => ({
version: -1 as any, // letting us know this doesn't conform to spec version: -1 as any, // letting us know this doesn't conform to spec
routes: {}, routes: {},
...@@ -1840,13 +1840,11 @@ export default async function build( ...@@ -1840,13 +1840,11 @@ export default async function build(
pageRuntime, pageRuntime,
edgeInfo, edgeInfo,
pageType, pageType,
incrementalCacheHandlerPath: cacheHandler: config.cacheHandler,
config.experimental.incrementalCacheHandlerPath,
isrFlushToDisk: ciEnvironment.hasNextSupport isrFlushToDisk: ciEnvironment.hasNextSupport
? false ? false
: config.experimental.isrFlushToDisk, : config.experimental.isrFlushToDisk,
maxMemoryCacheSize: maxMemoryCacheSize: config.cacheMaxMemorySize,
config.experimental.isrMemoryCacheSize,
nextConfigOutput: config.output, nextConfigOutput: config.output,
ppr: config.experimental.ppr === true, ppr: config.experimental.ppr === true,
}) })
......
...@@ -1304,7 +1304,7 @@ export async function buildAppStaticPaths({ ...@@ -1304,7 +1304,7 @@ export async function buildAppStaticPaths({
configFileName, configFileName,
generateParams, generateParams,
isrFlushToDisk, isrFlushToDisk,
incrementalCacheHandlerPath, cacheHandler,
requestHeaders, requestHeaders,
maxMemoryCacheSize, maxMemoryCacheSize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
...@@ -1315,10 +1315,10 @@ export async function buildAppStaticPaths({ ...@@ -1315,10 +1315,10 @@ export async function buildAppStaticPaths({
page: string page: string
configFileName: string configFileName: string
generateParams: GenerateParamsResults generateParams: GenerateParamsResults
incrementalCacheHandlerPath?: string
distDir: string distDir: string
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
cacheHandler?: string
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
ppr: boolean ppr: boolean
...@@ -1328,11 +1328,11 @@ export async function buildAppStaticPaths({ ...@@ -1328,11 +1328,11 @@ export async function buildAppStaticPaths({
let CacheHandler: any let CacheHandler: any
if (incrementalCacheHandlerPath) { if (cacheHandler) {
CacheHandler = interopDefault( CacheHandler = interopDefault(
await import( await import(formatDynamicImportPath(dir, cacheHandler)).then(
formatDynamicImportPath(dir, incrementalCacheHandlerPath) (mod) => mod.default || mod
).then((mod) => mod.default || mod) )
) )
} }
...@@ -1483,7 +1483,7 @@ export async function isPageStatic({ ...@@ -1483,7 +1483,7 @@ export async function isPageStatic({
originalAppPath, originalAppPath,
isrFlushToDisk, isrFlushToDisk,
maxMemoryCacheSize, maxMemoryCacheSize,
incrementalCacheHandlerPath, cacheHandler,
ppr, ppr,
}: { }: {
dir: string dir: string
...@@ -1501,7 +1501,7 @@ export async function isPageStatic({ ...@@ -1501,7 +1501,7 @@ export async function isPageStatic({
originalAppPath?: string originalAppPath?: string
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
incrementalCacheHandlerPath?: string cacheHandler?: string
nextConfigOutput: 'standalone' | 'export' nextConfigOutput: 'standalone' | 'export'
ppr: boolean ppr: boolean
}): Promise<{ }): Promise<{
...@@ -1667,7 +1667,7 @@ export async function isPageStatic({ ...@@ -1667,7 +1667,7 @@ export async function isPageStatic({
requestHeaders: {}, requestHeaders: {},
isrFlushToDisk, isrFlushToDisk,
maxMemoryCacheSize, maxMemoryCacheSize,
incrementalCacheHandlerPath, cacheHandler,
ppr, ppr,
ComponentMod, ComponentMod,
})) }))
......
...@@ -24,7 +24,7 @@ export type EdgeSSRLoaderQuery = { ...@@ -24,7 +24,7 @@ export type EdgeSSRLoaderQuery = {
appDirLoader?: string appDirLoader?: string
pagesType: PAGE_TYPES pagesType: PAGE_TYPES
sriEnabled: boolean sriEnabled: boolean
incrementalCacheHandlerPath?: string cacheHandler?: string
preferredRegion: string | string[] | undefined preferredRegion: string | string[] | undefined
middlewareConfig: string middlewareConfig: string
serverActions?: { serverActions?: {
...@@ -76,7 +76,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -76,7 +76,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
appDirLoader: appDirLoaderBase64, appDirLoader: appDirLoaderBase64,
pagesType, pagesType,
sriEnabled, sriEnabled,
incrementalCacheHandlerPath, cacheHandler,
preferredRegion, preferredRegion,
middlewareConfig: middlewareConfigBase64, middlewareConfig: middlewareConfigBase64,
serverActions, serverActions,
...@@ -158,7 +158,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -158,7 +158,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
: JSON.stringify(serverActions), : JSON.stringify(serverActions),
}, },
{ {
incrementalCacheHandler: incrementalCacheHandlerPath ?? null, incrementalCacheHandler: cacheHandler ?? null,
} }
) )
} else { } else {
...@@ -187,7 +187,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -187,7 +187,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
}, },
{ {
userland500Page: userland500Path, userland500Page: userland500Path,
incrementalCacheHandler: incrementalCacheHandlerPath ?? null, incrementalCacheHandler: cacheHandler ?? null,
} }
) )
} }
......
...@@ -8,8 +8,8 @@ import { interopDefault } from '../../lib/interop-default' ...@@ -8,8 +8,8 @@ import { interopDefault } from '../../lib/interop-default'
import { formatDynamicImportPath } from '../../lib/format-dynamic-import-path' import { formatDynamicImportPath } from '../../lib/format-dynamic-import-path'
export async function createIncrementalCache({ export async function createIncrementalCache({
incrementalCacheHandlerPath, cacheHandler,
isrMemoryCacheSize, cacheMaxMemorySize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
distDir, distDir,
dir, dir,
...@@ -17,8 +17,8 @@ export async function createIncrementalCache({ ...@@ -17,8 +17,8 @@ export async function createIncrementalCache({
experimental, experimental,
flushToDisk, flushToDisk,
}: { }: {
incrementalCacheHandlerPath?: string cacheHandler?: string
isrMemoryCacheSize?: number cacheMaxMemorySize?: number
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
distDir: string distDir: string
dir: string dir: string
...@@ -28,11 +28,11 @@ export async function createIncrementalCache({ ...@@ -28,11 +28,11 @@ export async function createIncrementalCache({
}) { }) {
// Custom cache handler overrides. // Custom cache handler overrides.
let CacheHandler: any let CacheHandler: any
if (incrementalCacheHandlerPath) { if (cacheHandler) {
CacheHandler = interopDefault( CacheHandler = interopDefault(
await import( await import(formatDynamicImportPath(dir, cacheHandler)).then(
formatDynamicImportPath(dir, incrementalCacheHandlerPath) (mod) => mod.default || mod
).then((mod) => mod.default || mod) )
) )
} }
...@@ -41,7 +41,7 @@ export async function createIncrementalCache({ ...@@ -41,7 +41,7 @@ export async function createIncrementalCache({
requestHeaders: {}, requestHeaders: {},
flushToDisk, flushToDisk,
fetchCache: true, fetchCache: true,
maxMemoryCacheSize: isrMemoryCacheSize, maxMemoryCacheSize: cacheMaxMemorySize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
getPrerenderManifest: () => ({ getPrerenderManifest: () => ({
version: 4, version: 4,
......
...@@ -701,11 +701,10 @@ export async function exportAppImpl( ...@@ -701,11 +701,10 @@ export async function exportAppImpl(
parentSpanId: pageExportSpan.getId(), parentSpanId: pageExportSpan.getId(),
httpAgentOptions: nextConfig.httpAgentOptions, httpAgentOptions: nextConfig.httpAgentOptions,
debugOutput: options.debugOutput, debugOutput: options.debugOutput,
isrMemoryCacheSize: nextConfig.experimental.isrMemoryCacheSize, cacheMaxMemorySize: nextConfig.cacheMaxMemorySize,
fetchCache: true, fetchCache: true,
fetchCacheKeyPrefix: nextConfig.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: nextConfig.experimental.fetchCacheKeyPrefix,
incrementalCacheHandlerPath: cacheHandler: nextConfig.cacheHandler,
nextConfig.experimental.incrementalCacheHandlerPath,
enableExperimentalReact: needsExperimentalReact(nextConfig), enableExperimentalReact: needsExperimentalReact(nextConfig),
enabledDirectories, enabledDirectories,
}) })
......
...@@ -53,9 +53,9 @@ export interface ExportPageInput { ...@@ -53,9 +53,9 @@ export interface ExportPageInput {
parentSpanId: any parentSpanId: any
httpAgentOptions: NextConfigComplete['httpAgentOptions'] httpAgentOptions: NextConfigComplete['httpAgentOptions']
debugOutput?: boolean debugOutput?: boolean
isrMemoryCacheSize?: NextConfigComplete['experimental']['isrMemoryCacheSize'] cacheMaxMemorySize?: NextConfigComplete['cacheMaxMemorySize']
fetchCache?: boolean fetchCache?: boolean
incrementalCacheHandlerPath?: string cacheHandler?: string
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
nextConfigOutput?: NextConfigComplete['output'] nextConfigOutput?: NextConfigComplete['output']
enableExperimentalReact?: boolean enableExperimentalReact?: boolean
......
...@@ -60,10 +60,10 @@ async function exportPageImpl( ...@@ -60,10 +60,10 @@ async function exportPageImpl(
optimizeCss, optimizeCss,
disableOptimizedLoading, disableOptimizedLoading,
debugOutput = false, debugOutput = false,
isrMemoryCacheSize, cacheMaxMemorySize,
fetchCache, fetchCache,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
incrementalCacheHandlerPath, cacheHandler,
enableExperimentalReact, enableExperimentalReact,
ampValidatorPath, ampValidatorPath,
trailingSlash, trailingSlash,
...@@ -222,8 +222,8 @@ async function exportPageImpl( ...@@ -222,8 +222,8 @@ async function exportPageImpl(
const incrementalCache = const incrementalCache =
isAppDir && fetchCache isAppDir && fetchCache
? await createIncrementalCache({ ? await createIncrementalCache({
incrementalCacheHandlerPath, cacheHandler,
isrMemoryCacheSize, cacheMaxMemorySize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
distDir, distDir,
dir, dir,
......
...@@ -117,6 +117,8 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -117,6 +117,8 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
analyticsId: z.string().optional(), analyticsId: z.string().optional(),
assetPrefix: z.string().optional(), assetPrefix: z.string().optional(),
basePath: z.string().optional(), basePath: z.string().optional(),
cacheHandler: z.string().min(1).optional(),
cacheMaxMemorySize: z.number().optional(),
cleanDistDir: z.boolean().optional(), cleanDistDir: z.boolean().optional(),
compiler: z compiler: z
.strictObject({ .strictObject({
...@@ -266,9 +268,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -266,9 +268,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
forceSwcTransforms: z.boolean().optional(), forceSwcTransforms: z.boolean().optional(),
fullySpecified: z.boolean().optional(), fullySpecified: z.boolean().optional(),
gzipSize: z.boolean().optional(), gzipSize: z.boolean().optional(),
incrementalCacheHandlerPath: z.string().optional(),
isrFlushToDisk: z.boolean().optional(), isrFlushToDisk: z.boolean().optional(),
isrMemoryCacheSize: z.number().optional(),
largePageDataBytes: z.number().optional(), largePageDataBytes: z.number().optional(),
manualClientBasePath: z.boolean().optional(), manualClientBasePath: z.boolean().optional(),
middlewarePrefetch: z.enum(['strict', 'flexible']).optional(), middlewarePrefetch: z.enum(['strict', 'flexible']).optional(),
......
...@@ -178,8 +178,6 @@ export interface ExperimentalConfig { ...@@ -178,8 +178,6 @@ export interface ExperimentalConfig {
optimisticClientCache?: boolean optimisticClientCache?: boolean
middlewarePrefetch?: 'strict' | 'flexible' middlewarePrefetch?: 'strict' | 'flexible'
manualClientBasePath?: boolean manualClientBasePath?: boolean
// custom path to a cache handler to use
incrementalCacheHandlerPath?: string
disablePostcssPresetEnv?: boolean disablePostcssPresetEnv?: boolean
swcMinify?: boolean swcMinify?: boolean
cpus?: number cpus?: number
...@@ -203,12 +201,6 @@ export interface ExperimentalConfig { ...@@ -203,12 +201,6 @@ export interface ExperimentalConfig {
gzipSize?: boolean gzipSize?: boolean
craCompat?: boolean craCompat?: boolean
esmExternals?: boolean | 'loose' esmExternals?: boolean | 'loose'
/**
* In-memory cache size in bytes.
*
* If `isrMemoryCacheSize: 0` disables in-memory caching.
*/
isrMemoryCacheSize?: number
fullySpecified?: boolean fullySpecified?: boolean
urlImports?: NonNullable<webpack.Configuration['experiments']>['buildHttp'] urlImports?: NonNullable<webpack.Configuration['experiments']>['buildHttp']
outputFileTracingRoot?: string outputFileTracingRoot?: string
...@@ -507,6 +499,21 @@ export interface NextConfig extends Record<string, any> { ...@@ -507,6 +499,21 @@ export interface NextConfig extends Record<string, any> {
*/ */
assetPrefix?: string assetPrefix?: string
/**
* The default cache handler for the Pages and App Router uses the filesystem cache. This requires no configuration, however, you can customize the cache handler if you prefer.
*
* @see [Configuring Caching](https://nextjs.org/docs/app/building-your-application/deploying#configuring-caching) and the [API Reference](https://nextjs.org/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath).
*/
cacheHandler?: string | undefined
/**
* Configure the in-memory cache size in bytes. Defaults to 50 MB.
* If `cacheMaxMemorySize: 0`, this disables in-memory caching entirely.
*
* @see [Configuring Caching](https://nextjs.org/docs/app/building-your-application/deploying#configuring-caching).
*/
cacheMaxMemorySize?: number
/** /**
* By default, `Next` will serve each file in the `pages` folder under a pathname matching the filename. * By default, `Next` will serve each file in the `pages` folder under a pathname matching the filename.
* To disable this behavior and prevent routing based set this to `true`. * To disable this behavior and prevent routing based set this to `true`.
...@@ -749,6 +756,9 @@ export const defaultConfig: NextConfig = { ...@@ -749,6 +756,9 @@ export const defaultConfig: NextConfig = {
distDir: '.next', distDir: '.next',
cleanDistDir: true, cleanDistDir: true,
assetPrefix: '', assetPrefix: '',
cacheHandler: undefined,
// default to 50MB limit
cacheMaxMemorySize: 50 * 1024 * 1024,
configOrigin: 'default', configOrigin: 'default',
useFileSystemPublicRoutes: true, useFileSystemPublicRoutes: true,
generateBuildId: () => null, generateBuildId: () => null,
...@@ -819,9 +829,6 @@ export const defaultConfig: NextConfig = { ...@@ -819,9 +829,6 @@ export const defaultConfig: NextConfig = {
gzipSize: true, gzipSize: true,
craCompat: false, craCompat: false,
esmExternals: true, esmExternals: true,
// default to 50MB limit
isrMemoryCacheSize: 50 * 1024 * 1024,
incrementalCacheHandlerPath: undefined,
fullySpecified: false, fullySpecified: false,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '', outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '',
swcTraceProfiling: false, swcTraceProfiling: false,
......
...@@ -695,11 +695,10 @@ export default class DevServer extends Server { ...@@ -695,11 +695,10 @@ export default class DevServer extends Server {
page, page,
isAppPath, isAppPath,
requestHeaders, requestHeaders,
incrementalCacheHandlerPath: cacheHandler: this.nextConfig.cacheHandler,
this.nextConfig.experimental.incrementalCacheHandlerPath,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk, isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize,
ppr: this.nextConfig.experimental.ppr === true, ppr: this.nextConfig.experimental.ppr === true,
}) })
return pathsResult return pathsResult
......
...@@ -37,7 +37,7 @@ export async function loadStaticPaths({ ...@@ -37,7 +37,7 @@ export async function loadStaticPaths({
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
maxMemoryCacheSize, maxMemoryCacheSize,
requestHeaders, requestHeaders,
incrementalCacheHandlerPath, cacheHandler,
ppr, ppr,
}: { }: {
dir: string dir: string
...@@ -53,7 +53,7 @@ export async function loadStaticPaths({ ...@@ -53,7 +53,7 @@ export async function loadStaticPaths({
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
incrementalCacheHandlerPath?: string cacheHandler?: string
ppr: boolean ppr: boolean
}): Promise<{ }): Promise<{
paths?: string[] paths?: string[]
...@@ -105,7 +105,7 @@ export async function loadStaticPaths({ ...@@ -105,7 +105,7 @@ export async function loadStaticPaths({
configFileName: config.configFileName, configFileName: config.configFileName,
distDir, distDir,
requestHeaders, requestHeaders,
incrementalCacheHandlerPath, cacheHandler,
isrFlushToDisk, isrFlushToDisk,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
maxMemoryCacheSize, maxMemoryCacheSize,
......
...@@ -308,12 +308,12 @@ export default class NextNodeServer extends BaseServer { ...@@ -308,12 +308,12 @@ export default class NextNodeServer extends BaseServer {
}) { }) {
const dev = !!this.renderOpts.dev const dev = !!this.renderOpts.dev
let CacheHandler: any let CacheHandler: any
const { incrementalCacheHandlerPath } = this.nextConfig.experimental const { cacheHandler } = this.nextConfig
if (incrementalCacheHandlerPath) { if (cacheHandler) {
CacheHandler = interopDefault( CacheHandler = interopDefault(
await dynamicImportEsmDefault( await dynamicImportEsmDefault(
formatDynamicImportPath(this.distDir, incrementalCacheHandlerPath) formatDynamicImportPath(this.distDir, cacheHandler)
) )
) )
} }
...@@ -334,7 +334,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -334,7 +334,7 @@ export default class NextNodeServer extends BaseServer {
serverDistDir: this.serverDistDir, serverDistDir: this.serverDistDir,
fetchCache: true, fetchCache: true,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize,
flushToDisk: flushToDisk:
!this.minimalMode && this.nextConfig.experimental.isrFlushToDisk, !this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
getPrerenderManifest: () => this.getPrerenderManifest(), getPrerenderManifest: () => this.getPrerenderManifest(),
......
...@@ -75,7 +75,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -75,7 +75,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
minimalMode: this.minimalMode, minimalMode: this.minimalMode,
fetchCache: true, fetchCache: true,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize,
flushToDisk: false, flushToDisk: false,
CurCacheHandler: CurCacheHandler:
this.serverOptions.webServerConfig.incrementalCacheHandler, this.serverOptions.webServerConfig.incrementalCacheHandler,
......
module.exports = { module.exports = {
experimental: { cacheHandler: process.cwd() + '/' + process.env.CUSTOM_CACHE_HANDLER,
incrementalCacheHandlerPath:
process.cwd() + '/' + process.env.CUSTOM_CACHE_HANDLER,
},
} }
...@@ -3,9 +3,7 @@ module.exports = { ...@@ -3,9 +3,7 @@ module.exports = {
logging: { logging: {
fetches: {}, fetches: {},
}, },
experimental: { cacheHandler: process.env.CUSTOM_CACHE_HANDLER,
incrementalCacheHandlerPath: process.env.CUSTOM_CACHE_HANDLER,
},
rewrites: async () => { rewrites: async () => {
return { return {
......
...@@ -38,9 +38,8 @@ describe('required server files app router', () => { ...@@ -38,9 +38,8 @@ describe('required server files app router', () => {
'.env.production': new FileRef(join(__dirname, '.env.production')), '.env.production': new FileRef(join(__dirname, '.env.production')),
}, },
nextConfig: { nextConfig: {
experimental: { cacheHandler: './cache-handler.js',
incrementalCacheHandlerPath: './cache-handler.js', cacheMaxMemorySize: 0,
},
eslint: { eslint: {
ignoreDuringBuilds: true, ignoreDuringBuilds: true,
}, },
......
...@@ -39,8 +39,8 @@ describe('required server files app router', () => { ...@@ -39,8 +39,8 @@ describe('required server files app router', () => {
'.env.production': new FileRef(join(__dirname, '.env.production')), '.env.production': new FileRef(join(__dirname, '.env.production')),
}, },
nextConfig: { nextConfig: {
cacheHandler: './cache-handler.js',
experimental: { experimental: {
incrementalCacheHandlerPath: './cache-handler.js',
ppr: true, ppr: true,
}, },
eslint: { eslint: {
......
...@@ -45,9 +45,8 @@ describe('required server files', () => { ...@@ -45,9 +45,8 @@ describe('required server files', () => {
'.env.production': new FileRef(join(__dirname, '.env.production')), '.env.production': new FileRef(join(__dirname, '.env.production')),
}, },
nextConfig: { nextConfig: {
experimental: { cacheHandler: './cache-handler.js',
incrementalCacheHandlerPath: './cache-handler.js', cacheMaxMemorySize: 0,
},
eslint: { eslint: {
ignoreDuringBuilds: true, ignoreDuringBuilds: true,
}, },
...@@ -303,14 +302,20 @@ describe('required server files', () => { ...@@ -303,14 +302,20 @@ describe('required server files', () => {
).toContain('"compress":false') ).toContain('"compress":false')
}) })
it('`incrementalCacheHandlerPath` should have correct path', async () => { it('`cacheHandler` should have correct path', async () => {
expect( expect(
await fs.pathExists(join(next.testDir, 'standalone/cache-handler.js')) await fs.pathExists(join(next.testDir, 'standalone/cache-handler.js'))
).toBe(true) ).toBe(true)
expect( expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8') await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
).toContain('"incrementalCacheHandlerPath":"../cache-handler.js"') ).toContain('"cacheHandler":"../cache-handler.js"')
})
it('`cacheMaxMemorySize` should be disabled by setting to 0', async () => {
expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
).toContain('"cacheMaxMemorySize":0')
}) })
it('should output middleware correctly', async () => { it('should output middleware correctly', async () => {
......
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