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