Skip to content

Commit

Permalink
refactor: move fs based inc cache to next-serevr
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 18, 2022
1 parent 2e00eb6 commit 98b1da1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 41 deletions.
8 changes: 6 additions & 2 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import fs from 'fs'
import chalk from 'next/dist/compiled/chalk'
import { posix, join } from 'path'
import { stringify } from 'querystring'
import { API_ROUTE, DOT_NEXT_ALIAS, PAGES_DIR_ALIAS } from '../lib/constants'
import { MIDDLEWARE_ROUTE } from '../lib/constants'
import {
API_ROUTE,
DOT_NEXT_ALIAS,
PAGES_DIR_ALIAS,
MIDDLEWARE_ROUTE,
} from '../lib/constants'
import { __ApiPreviewProps } from '../server/api-utils'
import { isTargetLikeServerless } from '../server/utils'
import { normalizePagePath } from '../server/normalize-page-path'
Expand Down
41 changes: 2 additions & 39 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type { Rewrite } 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 type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
Expand All @@ -23,8 +22,6 @@ import { parse as parseQs } from 'querystring'
import { format as formatUrl, parse as parseUrl } from 'url'
import { getRedirectStatus } from '../lib/load-custom-routes'
import {
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
STATIC_STATUS_PAGES,
TEMPORARY_REDIRECT_STATUS,
} from '../shared/lib/constants'
Expand Down Expand Up @@ -224,6 +221,7 @@ export default abstract class Server {
protected abstract getRoutesManifest(): CustomRoutes
protected abstract getPrerenderManifest(): PrerenderManifest
protected abstract getServerComponentManifest(): any
protected abstract getIncrementalCache(dev: boolean): any

protected abstract sendRenderResult(
req: BaseNextRequest,
Expand Down Expand Up @@ -350,32 +348,7 @@ export default abstract class Server {
this.router = new Router(this.generateRoutes())
this.setAssetPrefix(assetPrefix)

this.incrementalCache = new IncrementalCache({
fs: this.getCacheFilesystem(),
dev,
distDir: this.distDir,
pagesDir: join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
'pages'
),
locales: this.nextConfig.i18n?.locales,
max: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: !minimalMode && this.nextConfig.experimental.isrFlushToDisk,
getPrerenderManifest: () => {
if (dev) {
return {
version: -1 as any, // letting us know this doesn't conform to spec
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: null as any, // `preview` is special case read in next-dev-server
}
} else {
return this.getPrerenderManifest()
}
},
})
this.incrementalCache = this.getIncrementalCache(dev)
this.responseCache = new ResponseCache(
this.incrementalCache,
this.minimalMode
Expand Down Expand Up @@ -1893,16 +1866,6 @@ 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
Expand Down
31 changes: 31 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
import ResponseCache from '../server/response-cache'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { clonableBodyForRequest } from './body-streams'
import { IncrementalCache } from './incremental-cache'

export * from './base-server'

Expand Down Expand Up @@ -1357,4 +1358,34 @@ export default class NextNodeServer extends BaseServer {
this.warnIfQueryParametersWereDeleted = () => {}
}
}

protected getIncrementalCache(dev: boolean) {
return new IncrementalCache({
fs: this.getCacheFilesystem(),
dev,
distDir: this.distDir,
pagesDir: join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
'pages'
),
locales: this.nextConfig.i18n?.locales,
max: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk:
!this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
getPrerenderManifest: () => {
if (dev) {
return {
version: -1 as any, // letting us know this doesn't conform to spec
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: null as any, // `preview` is special case read in next-dev-server
}
} else {
return this.getPrerenderManifest()
}
},
})
}
}
23 changes: 23 additions & 0 deletions packages/next/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NextParsedUrlQuery } from './request-meta'
import type { Params } from './router'
import type { PayloadOptions } from './send-payload'
import type { LoadComponentsReturnType } from './load-components'
import type { CacheFs } from '../shared/lib/utils'

import BaseServer, { Options } from './base-server'
import { renderToHTML } from './render'
Expand Down Expand Up @@ -207,4 +208,26 @@ export default class NextWebServer extends BaseServer {
components: result,
}
}

protected getIncrementalCache() {
return {
async set() {},
async get() {
return null
},
async getFallback() {
return ''
},
}
}

protected getCacheFilesystem(): CacheFs {
return {
readFile: () => Promise.resolve(''),
readFileSync: () => '',
writeFile: () => Promise.resolve(),
mkdir: () => Promise.resolve(),
stat: () => Promise.resolve({ mtime: new Date() }),
}
}
}
4 changes: 4 additions & 0 deletions test/integration/react-18/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export default function Index() {
</div>
)
}

export const config = {
// runtime: 'edge'
}
3 changes: 3 additions & 0 deletions test/integration/react-18/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import webdriver from 'next-webdriver'
const appDir = join(__dirname, '../app')
const nextConfig = new File(join(appDir, 'next.config.js'))
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
const indexPage = new File(join(appDir, 'pages/index.js'))

describe('Basics', () => {
runTests('default setting with react 18', basics)
Expand Down Expand Up @@ -67,11 +68,13 @@ function runTestsAgainstRuntime(runtime) {
invalidPage.write(`export const value = 1`)
}
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
indexPage.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
},
afterAll: (env) => {
if (env === 'dev') {
invalidPage.delete()
}
indexPage.restore()
nextConfig.restore()
},
}
Expand Down

0 comments on commit 98b1da1

Please sign in to comment.