Unverified Commit 15354e9b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Migrate prerender lru cache into fs cache handler (#37326)

Continuation of https://github.com/vercel/next.js/pull/37258 this moves the LRU cache to the `file-system-cache` so that any different cache handlers don't need to disable the LRU cache as this may cause inconsistencies with some cache handlers that need to be the source of truth.  
parent b173abaf
...@@ -353,8 +353,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -353,8 +353,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.incrementalCache = new IncrementalCache({ this.incrementalCache = new IncrementalCache({
fs: this.getCacheFilesystem(), fs: this.getCacheFilesystem(),
dev, dev,
distDir: this.distDir, serverDistDir: this.serverDistDir,
pagesDir: join(this.serverDistDir, 'pages'),
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: !minimalMode && this.nextConfig.experimental.isrFlushToDisk, flushToDisk: !minimalMode && this.nextConfig.experimental.isrFlushToDisk,
getPrerenderManifest: () => { getPrerenderManifest: () => {
......
import { CacheFs } from '../../../shared/lib/utils' import LRUCache from 'next/dist/compiled/lru-cache'
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import type { CacheHandler, CacheHandlerContext } from './' import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
export default class FileSystemCache implements CacheHandler { export default class FileSystemCache implements CacheHandler {
private flushToDisk?: boolean private fs: CacheHandlerContext['fs']
private pagesDir: string private flushToDisk?: CacheHandlerContext['flushToDisk']
private fs: CacheFs private serverDistDir: CacheHandlerContext['serverDistDir']
private memoryCache?: LRUCache<string, CacheHandlerValue>
constructor(ctx: CacheHandlerContext) { constructor(ctx: CacheHandlerContext) {
this.flushToDisk = ctx.flushToDisk
this.pagesDir = ctx.pagesDir
this.fs = ctx.fs this.fs = ctx.fs
this.flushToDisk = ctx.flushToDisk
this.serverDistDir = ctx.serverDistDir
if (ctx.maxMemoryCacheSize) {
this.memoryCache = new LRUCache({
max: ctx.maxMemoryCacheSize,
length({ value }) {
if (!value) {
return 25
} else if (value.kind === 'REDIRECT') {
return JSON.stringify(value.props).length
} else if (value.kind === 'IMAGE') {
throw new Error('invariant image should not be incremental-cache')
}
// rough estimate of size of cache value
return value.html.length + JSON.stringify(value.pageData).length
},
})
}
} }
public async get(key: string) { public async get(key: string) {
return this.fs.readFile(this.getSeedPath(key)) let data = this.memoryCache?.get(key)
}
public async getMeta(key: string) { // let's check the disk for seed data
const stat = await this.fs.stat(this.getSeedPath(key)) if (!data) {
return { try {
mtime: stat.mtime.getTime(), const htmlPath = this.getFsPath(`${key}.html`)
const html = await this.fs.readFile(htmlPath)
const pageData = JSON.parse(
await this.fs.readFile(this.getFsPath(`${key}.json`))
)
const { mtime } = await this.fs.stat(htmlPath)
data = {
lastModified: mtime.getTime(),
value: {
kind: 'PAGE',
html,
pageData,
},
}
this.memoryCache?.set(key, data)
} catch (_) {
// unable to get data from disk
}
} }
return data || null
} }
public async set(key: string, data: string) { public async set(key: string, data: CacheHandlerValue['value']) {
if (!this.flushToDisk) return if (!this.flushToDisk) return
const pathname = this.getSeedPath(key)
await this.fs.mkdir(path.dirname(pathname)) this.memoryCache?.set(key, {
return this.fs.writeFile(pathname, data) value: data,
lastModified: Date.now(),
})
if (data?.kind === 'PAGE') {
await this.fs.writeFile(this.getFsPath(`${key}.html`), data.html)
await this.fs.writeFile(
this.getFsPath(`${key}.json`),
JSON.stringify(data.pageData)
)
}
} }
private getSeedPath(pathname: string): string { private getFsPath(pathname: string): string {
return path.join(this.pagesDir, pathname) return path.join(this.serverDistDir, 'pages', pathname)
} }
} }
import type { CacheFs } from '../../../shared/lib/utils' import type { CacheFs } from '../../../shared/lib/utils'
import FileSystemCache from './file-system-cache' import FileSystemCache from './file-system-cache'
import LRUCache from 'next/dist/compiled/lru-cache'
import path from '../../../shared/lib/isomorphic/path'
import { PrerenderManifest } from '../../../build' import { PrerenderManifest } from '../../../build'
import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { import {
IncrementalCacheValue, IncrementalCacheValue,
...@@ -15,49 +13,49 @@ function toRoute(pathname: string): string { ...@@ -15,49 +13,49 @@ function toRoute(pathname: string): string {
} }
export interface CacheHandlerContext { export interface CacheHandlerContext {
flushToDisk?: boolean
pagesDir: string
distDir: string
dev?: boolean
fs: CacheFs fs: CacheFs
dev?: boolean
flushToDisk?: boolean
serverDistDir: string
maxMemoryCacheSize?: number
}
export interface CacheHandlerValue {
lastModified?: number
value: IncrementalCacheValue | null
} }
export class CacheHandler { export class CacheHandler {
// eslint-disable-next-line // eslint-disable-next-line
constructor(_ctx: CacheHandlerContext) {} constructor(_ctx: CacheHandlerContext) {}
public async get(_key: string): Promise<string> { public async get(_key: string): Promise<CacheHandlerValue | null> {
return ''
}
public async getMeta(_key: string): Promise<{
// time in epoch e.g. Date.now()
mtime: number
}> {
return {} as any return {} as any
} }
public async set(_key: string, _data: string): Promise<void> {}
public async set(
_key: string,
_data: IncrementalCacheValue | null
): Promise<void> {}
} }
export class IncrementalCache { export class IncrementalCache {
prerenderManifest: PrerenderManifest
cache?: LRUCache<string, IncrementalCacheEntry>
dev?: boolean dev?: boolean
cacheHandler: CacheHandler cacheHandler: CacheHandler
prerenderManifest: PrerenderManifest
constructor({ constructor({
fs, fs,
dev, dev,
distDir,
pagesDir,
flushToDisk, flushToDisk,
serverDistDir,
maxMemoryCacheSize, maxMemoryCacheSize,
getPrerenderManifest, getPrerenderManifest,
incrementalCacheHandlerPath, incrementalCacheHandlerPath,
}: { }: {
fs: CacheFs fs: CacheFs
dev: boolean dev: boolean
distDir: string serverDistDir: string
pagesDir: string
flushToDisk?: boolean flushToDisk?: boolean
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
incrementalCacheHandlerPath?: string incrementalCacheHandlerPath?: string
...@@ -69,52 +67,34 @@ export class IncrementalCache { ...@@ -69,52 +67,34 @@ export class IncrementalCache {
cacheHandlerMod = require(incrementalCacheHandlerPath) cacheHandlerMod = require(incrementalCacheHandlerPath)
cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod
} }
this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({
dev,
distDir,
fs,
pagesDir,
flushToDisk,
})
this.dev = dev
this.prerenderManifest = getPrerenderManifest()
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) { if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
// Allow cache size to be overridden for testing purposes // Allow cache size to be overridden for testing purposes
maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10) maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10)
} }
this.dev = dev
if (maxMemoryCacheSize) { this.prerenderManifest = getPrerenderManifest()
this.cache = new LRUCache({ this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({
max: maxMemoryCacheSize, dev,
length({ value }) { fs,
if (!value) { flushToDisk,
return 25 serverDistDir,
} else if (value.kind === 'REDIRECT') { maxMemoryCacheSize,
return JSON.stringify(value.props).length })
} else if (value.kind === 'IMAGE') {
throw new Error('invariant image should not be incremental-cache')
}
// rough estimate of size of cache value
return value.html.length + JSON.stringify(value.pageData).length
},
})
}
} }
private calculateRevalidate( private calculateRevalidate(
pathname: string, pathname: string,
fromTime: number fromTime: number
): number | false { ): number | false {
pathname = toRoute(pathname)
// in development we don't have a prerender-manifest // in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging // and default to always revalidating to allow easier debugging
if (this.dev) return new Date().getTime() - 1000 if (this.dev) return new Date().getTime() - 1000
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second
const { initialRevalidateSeconds } = this.prerenderManifest.routes[ const { initialRevalidateSeconds } = this.prerenderManifest.routes[
pathname toRoute(pathname)
] || { ] || {
initialRevalidateSeconds: 1, initialRevalidateSeconds: 1,
} }
...@@ -126,66 +106,58 @@ export class IncrementalCache { ...@@ -126,66 +106,58 @@ export class IncrementalCache {
return revalidateAfter return revalidateAfter
} }
_getPathname(pathname: string) {
return normalizePagePath(pathname)
}
// get data from cache if available // get data from cache if available
async get(pathname: string): Promise<IncrementalCacheEntry | null> { async get(pathname: string): Promise<IncrementalCacheEntry | null> {
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if (this.dev) return null if (this.dev) return null
pathname = normalizePagePath(pathname)
let data = this.cache && this.cache.get(pathname)
// let's check the disk for seed data
if (!data) {
if (this.prerenderManifest.notFoundRoutes.includes(pathname)) {
const now = Date.now()
const revalidateAfter = this.calculateRevalidate(pathname, now)
data = {
value: null,
revalidateAfter: revalidateAfter !== false ? now : false,
}
}
try { pathname = this._getPathname(pathname)
const htmlPath = `${pathname}.html` let entry: IncrementalCacheEntry | null = null
const html = await this.cacheHandler.get(htmlPath) const cacheData = await this.cacheHandler.get(pathname)
const pageData = JSON.parse(
await this.cacheHandler.get(`${pathname}.json`) const curRevalidate =
) this.prerenderManifest.routes[toRoute(pathname)]?.initialRevalidateSeconds
const { mtime } = await this.cacheHandler.getMeta(htmlPath) const revalidateAfter = this.calculateRevalidate(
pathname,
data = { cacheData?.lastModified || Date.now()
revalidateAfter: this.calculateRevalidate(pathname, mtime), )
value: { const isStale =
kind: 'PAGE', revalidateAfter !== false && revalidateAfter < Date.now()
html, ? true
pageData, : undefined
},
} if (cacheData) {
if (this.cache) { entry = {
this.cache.set(pathname, data) isStale,
} curRevalidate,
} catch (_) { revalidateAfter,
// unable to get data from disk value: cacheData.value,
} }
} }
if (!data) {
return null
}
if ( if (
data && !cacheData &&
data.revalidateAfter !== false && this.prerenderManifest.notFoundRoutes.includes(pathname)
data.revalidateAfter < new Date().getTime()
) { ) {
data.isStale = true // for the first hit after starting the server the cache
} // may not have a way to save notFound: true so if
// the prerender-manifest marks this as notFound then we
const manifestPath = toRoute(pathname) // return that entry and trigger a cache set to give it a
const manifestEntry = this.prerenderManifest.routes[manifestPath] // chance to update in-memory entries
entry = {
if (data && manifestEntry) { isStale,
data.curRevalidate = manifestEntry.initialRevalidateSeconds value: null,
curRevalidate,
revalidateAfter,
}
this.set(pathname, entry.value, curRevalidate)
} }
return data return entry
} }
// populate the incremental cache with new data // populate the incremental cache with new data
...@@ -195,39 +167,25 @@ export class IncrementalCache { ...@@ -195,39 +167,25 @@ export class IncrementalCache {
revalidateSeconds?: number | false revalidateSeconds?: number | false
) { ) {
if (this.dev) return if (this.dev) return
if (typeof revalidateSeconds !== 'undefined') { pathname = this._getPathname(pathname)
this.prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join( try {
'/_next/data', // we use the prerender manifest memory instance
`${normalizePagePath(pathname)}.json` // to store revalidate timings for calculating
), // revalidateAfter values so we update this on set
srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter if (typeof revalidateSeconds !== 'undefined') {
initialRevalidateSeconds: revalidateSeconds, this.prerenderManifest.routes[pathname] = {
} dataRoute: path.posix.join(
} '/_next/data',
`${normalizePagePath(pathname)}.json`
pathname = normalizePagePath(pathname) ),
if (this.cache) { srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter
this.cache.set(pathname, { initialRevalidateSeconds: revalidateSeconds,
revalidateAfter: this.calculateRevalidate( }
pathname,
new Date().getTime()
),
value: data,
})
}
if (data?.kind === 'PAGE') {
try {
await this.cacheHandler.set(`${pathname}.html`, data.html)
await this.cacheHandler.set(
`${pathname}.json`,
JSON.stringify(data.pageData)
)
} catch (error) {
// failed to set to cache handler
console.warn('Failed to update prerender files for', pathname, error)
} }
await this.cacheHandler.set(pathname, data)
} catch (error) {
console.warn('Failed to update prerender cache for', pathname, error)
} }
} }
} }
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