Skip to content

Commit

Permalink
Add configuration for ISR Memory Cache Size (#21535) (#27337)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stefanb2 authored Jul 21, 2021
1 parent e65c56e commit dbfce34
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
3 changes: 3 additions & 0 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export type NextConfig = { [key: string]: any } & {
esmExternals?: boolean | 'loose'
staticPageGenerationTimeout?: number
pageDataCollectionTimeout?: number
isrMemoryCacheSize?: number
}
}

Expand Down Expand Up @@ -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,
Expand Down
40 changes: 24 additions & 16 deletions packages/next/server/incremental-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class IncrementalCache {
}

prerenderManifest: PrerenderManifest
cache: LRUCache<string, IncrementalCacheEntry>
cache?: LRUCache<string, IncrementalCacheEntry>
locales?: string[]

constructor({
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dbfce34

Please sign in to comment.