From dbfce34ead5125ed9b0b952d50f7b0e1ae43154e Mon Sep 17 00:00:00 2001 From: Stefan Becker Date: Wed, 21 Jul 2021 20:39:38 +0300 Subject: [PATCH] Add configuration for ISR Memory Cache Size (#21535) (#27337) This allows the user to adjust the LRU cache size according to the application need: - optimize the cache size based on average page HTML & JSON size - disable the memory cache by setting the size to 0 The hard-coded default of 50MB has been moved from the code to the default configuration object. Fixes #21535 See also #27325 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [X] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [X] Related issues linked using `fixes #number` - [ ] Integration tests added - [X] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes --- packages/next/server/config-shared.ts | 3 ++ packages/next/server/incremental-cache.ts | 40 ++++++++++++++--------- packages/next/server/next-server.ts | 1 + 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/next/server/config-shared.ts b/packages/next/server/config-shared.ts index 395d5edc4fef9..77b4ae299bb0f 100644 --- a/packages/next/server/config-shared.ts +++ b/packages/next/server/config-shared.ts @@ -101,6 +101,7 @@ export type NextConfig = { [key: string]: any } & { esmExternals?: boolean | 'loose' staticPageGenerationTimeout?: number pageDataCollectionTimeout?: number + isrMemoryCacheSize?: number } } @@ -166,6 +167,8 @@ export const defaultConfig: NextConfig = { esmExternals: false, staticPageGenerationTimeout: 60, pageDataCollectionTimeout: 60, + // default to 50MB limit + isrMemoryCacheSize: 50 * 1024 * 1024, }, future: { strictPostcssConfiguration: false, diff --git a/packages/next/server/incremental-cache.ts b/packages/next/server/incremental-cache.ts index f97cc1f9852bd..e12c120a6b671 100644 --- a/packages/next/server/incremental-cache.ts +++ b/packages/next/server/incremental-cache.ts @@ -39,7 +39,7 @@ export class IncrementalCache { } prerenderManifest: PrerenderManifest - cache: LRUCache + cache?: LRUCache locales?: string[] constructor({ @@ -85,15 +85,16 @@ export class IncrementalCache { max = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10) } - this.cache = new LRUCache({ - // default to 50MB limit - max: max || 50 * 1024 * 1024, - length({ value }) { - if (!value || value.kind === 'REDIRECT') return 25 - // rough estimate of size of cache value - return value.html.length + JSON.stringify(value.pageData).length - }, - }) + if (max) { + this.cache = new LRUCache({ + max, + length({ value }) { + if (!value || value.kind === 'REDIRECT') return 25 + // rough estimate of size of cache value + return value.html.length + JSON.stringify(value.pageData).length + }, + }) + } } private getSeedPath(pathname: string, ext: string): string { @@ -133,7 +134,7 @@ export class IncrementalCache { if (this.incrementalOptions.dev) return null pathname = normalizePagePath(pathname) - let data = this.cache.get(pathname) + let data = this.cache && this.cache.get(pathname) // let's check the disk for seed data if (!data) { @@ -157,7 +158,9 @@ export class IncrementalCache { pageData, }, } - this.cache.set(pathname, data) + if (this.cache) { + this.cache.set(pathname, data) + } } catch (_) { // unable to get data from disk } @@ -204,10 +207,15 @@ export class IncrementalCache { } pathname = normalizePagePath(pathname) - this.cache.set(pathname, { - revalidateAfter: this.calculateRevalidate(pathname, new Date().getTime()), - value: data, - }) + if (this.cache) { + this.cache.set(pathname, { + revalidateAfter: this.calculateRevalidate( + pathname, + new Date().getTime() + ), + value: data, + }) + } // TODO: This option needs to cease to exist unless it stops mutating the // `next build` output's manifest. diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 9347e06b70ce0..bbaba6e9f3317 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -276,6 +276,7 @@ export default class Server { 'pages' ), locales: this.nextConfig.i18n?.locales, + max: this.nextConfig.experimental.isrMemoryCacheSize, flushToDisk: !minimalMode && this.nextConfig.experimental.sprFlushToDisk, }) this.responseCache = new ResponseCache(this.incrementalCache)