Unverified Commit bb6d4d71 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Move fs API for inc cache to node server (#32604)

Move fs API for incremental cache to node-server, leave a small group of mock file system for base server
parent d7811d32
......@@ -18,6 +18,7 @@ import type { Redirect, Rewrite, RouteType } from '../lib/load-custom-routes'
import type { RenderOpts, RenderOptsPartial } from './render'
import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache'
import type { UrlWithParsedQuery } from 'url'
import type { CacheFs } from '../shared/lib/utils'
import compression from 'next/dist/compiled/compression'
import Proxy from 'next/dist/compiled/http-proxy'
......@@ -314,6 +315,7 @@ export default abstract class Server {
this.setAssetPrefix(assetPrefix)
this.incrementalCache = new IncrementalCache({
fs: this.getCacheFilesystem(),
dev,
distDir: this.distDir,
pagesDir: join(
......@@ -1331,7 +1333,7 @@ export default abstract class Server {
if (result.response.headers.has('x-middleware-refresh')) {
res.writeHead(result.response.status)
for await (const chunk of result.response.body || []) {
for await (const chunk of result.response.body || ([] as any)) {
res.write(chunk)
}
res.end()
......@@ -2482,6 +2484,16 @@ export default abstract class Server {
})
}
protected getCacheFilesystem(): CacheFs {
return {
readFile: () => Promise.resolve(''),
readFileSync: () => '',
writeFile: () => Promise.resolve(),
mkdir: () => Promise.resolve(),
stat: () => Promise.resolve({ mtime: new Date() }),
}
}
protected async getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null> {
// The development server will provide an implementation for this
return null
......
import { promises, readFileSync } from 'fs'
import LRUCache from 'next/dist/compiled/lru-cache'
import path from 'path'
import { PrerenderManifest } from '../build'
import { PRERENDER_MANIFEST } from '../shared/lib/constants'
import { normalizePagePath } from './normalize-page-path'
import type { CacheFs } from '../shared/lib/utils'
function toRoute(pathname: string): string {
return pathname.replace(/\/$/, '').replace(/\/index$/, '') || '/'
......@@ -41,8 +41,10 @@ export class IncrementalCache {
prerenderManifest: PrerenderManifest
cache?: LRUCache<string, IncrementalCacheEntry>
locales?: string[]
fs: CacheFs
constructor({
fs,
max,
dev,
distDir,
......@@ -50,6 +52,7 @@ export class IncrementalCache {
flushToDisk,
locales,
}: {
fs: CacheFs
dev: boolean
max?: number
distDir: string
......@@ -57,6 +60,7 @@ export class IncrementalCache {
flushToDisk?: boolean
locales?: string[]
}) {
this.fs = fs
this.incrementalOptions = {
dev,
distDir,
......@@ -75,9 +79,10 @@ export class IncrementalCache {
preview: null as any, // `preview` is special case read in next-dev-server
}
} else {
this.prerenderManifest = JSON.parse(
readFileSync(path.join(distDir, PRERENDER_MANIFEST), 'utf8')
const manifestJson = this.fs.readFileSync(
path.join(distDir, PRERENDER_MANIFEST)
)
this.prerenderManifest = JSON.parse(manifestJson)
}
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
......@@ -126,7 +131,7 @@ export class IncrementalCache {
getFallback(page: string): Promise<string> {
page = normalizePagePath(page)
return promises.readFile(this.getSeedPath(page, 'html'), 'utf8')
return this.fs.readFile(this.getSeedPath(page, 'html'))
}
// get data from cache if available
......@@ -149,11 +154,10 @@ export class IncrementalCache {
try {
const htmlPath = this.getSeedPath(pathname, 'html')
const html = await promises.readFile(htmlPath, 'utf8')
const { mtime } = await promises.stat(htmlPath)
const pageData = JSON.parse(
await promises.readFile(this.getSeedPath(pathname, 'json'), 'utf8')
)
const jsonPath = this.getSeedPath(pathname, 'json')
const html = await this.fs.readFile(htmlPath)
const pageData = JSON.parse(await this.fs.readFile(jsonPath))
const { mtime } = await this.fs.stat(htmlPath)
data = {
revalidateAfter: this.calculateRevalidate(pathname, mtime.getTime()),
......@@ -226,14 +230,11 @@ export class IncrementalCache {
// `next build` output's manifest.
if (this.incrementalOptions.flushToDisk && data?.kind === 'PAGE') {
try {
const seedPath = this.getSeedPath(pathname, 'html')
await promises.mkdir(path.dirname(seedPath), { recursive: true })
await promises.writeFile(seedPath, data.html, 'utf8')
await promises.writeFile(
this.getSeedPath(pathname, 'json'),
JSON.stringify(data.pageData),
'utf8'
)
const seedHtmlPath = this.getSeedPath(pathname, 'html')
const seedJsonPath = this.getSeedPath(pathname, 'json')
await this.fs.mkdir(path.dirname(seedHtmlPath))
await this.fs.writeFile(seedHtmlPath, data.html)
await this.fs.writeFile(seedJsonPath, JSON.stringify(data.pageData))
} catch (error) {
// failed to flush to disk
console.warn('Failed to update prerender files for', pathname, error)
......
import type { Route } from './router'
import type { CacheFs } from '../shared/lib/utils'
import fs from 'fs'
import { join, relative } from 'path'
import { PAGES_MANIFEST, BUILD_ID_FILE } from '../shared/lib/constants'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { Route } from './router'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
import { route } from './router'
import BaseServer from './base-server'
export * from './base-server'
export default class NextNodeServer extends BaseServer {
......@@ -133,4 +134,14 @@ export default class NextNodeServer extends BaseServer {
...userFilesStatic,
]))
}
protected getCacheFilesystem(): CacheFs {
return {
readFile: (f) => fs.promises.readFile(f, 'utf8'),
readFileSync: (f) => fs.readFileSync(f, 'utf8'),
writeFile: (f, d) => fs.promises.writeFile(f, d, 'utf8'),
mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => fs.promises.stat(f),
}
}
}
......@@ -451,3 +451,11 @@ export const HtmlContext = createContext<HtmlProps>(null as any)
if (process.env.NODE_ENV !== 'production') {
HtmlContext.displayName = 'HtmlContext'
}
export interface CacheFs {
readFile(f: string): Promise<string>
readFileSync(f: string): string
writeFile(f: string, d: any): Promise<void>
mkdir(dir: string): Promise<void>
stat(f: string): Promise<{ mtime: Date }>
}
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