Skip to content

Commit

Permalink
Implement abstract methods to get manifest files in the base server (v…
Browse files Browse the repository at this point in the history
…ercel#33537)

Related to vercel#31506, this PR adds `getPrerenderManifest` and `getRoutesManifest` methods to provide manifest files to the base server. In the future, we can consider making these methods async, or provide necessary manifests from the very top.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] 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 by running `yarn lint`
  • Loading branch information
shuding authored Jan 21, 2022
1 parent 8959292 commit 76b9a54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
15 changes: 3 additions & 12 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import { format as formatUrl, parse as parseUrl } from 'url'
import { getRedirectStatus, modifyRouteRegex } from '../lib/load-custom-routes'
import {
CLIENT_PUBLIC_FILES_PATH,
PRERENDER_MANIFEST,
ROUTES_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
STATIC_STATUS_PAGES,
Expand Down Expand Up @@ -213,6 +211,8 @@ export default abstract class Server {
protected abstract getPagePath(pathname: string, locales?: string[]): string
protected abstract getFontManifest(): FontManifest | undefined
protected abstract getMiddlewareManifest(): MiddlewareManifest | undefined
protected abstract getPrerenderManifest(): PrerenderManifest
protected abstract getRoutesManifest(): CustomRoutes

protected abstract sendRenderResult(
req: BaseNextRequest,
Expand Down Expand Up @@ -623,7 +623,7 @@ export default abstract class Server {
}

protected getCustomRoutes(): CustomRoutes {
const customRoutes = require(join(this.distDir, ROUTES_MANIFEST))
const customRoutes = this.getRoutesManifest()
let rewrites: CustomRoutes['rewrites']

// rewrites can be stored as an array when an array is
Expand All @@ -641,15 +641,6 @@ export default abstract class Server {
return Object.assign(customRoutes, { rewrites })
}

private _cachedPreviewManifest: PrerenderManifest | undefined
protected getPrerenderManifest(): PrerenderManifest {
if (this._cachedPreviewManifest) {
return this._cachedPreviewManifest
}
const manifest = require(join(this.distDir, PRERENDER_MANIFEST))
return (this._cachedPreviewManifest = manifest)
}

protected getPreviewProps(): __ApiPreviewProps {
return this.getPrerenderManifest().preview
}
Expand Down
16 changes: 16 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plu
import type RenderResult from './render-result'
import type { FetchEventResult } from './web/types'
import type { ParsedNextUrl } from '../shared/lib/router/utils/parse-next-url'
import type { PrerenderManifest } from '../build'

import { execOnce } from '../shared/lib/utils'
import {
Expand All @@ -24,6 +25,8 @@ import {
MIDDLEWARE_MANIFEST,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME,
PRERENDER_MANIFEST,
ROUTES_MANIFEST,
} from '../shared/lib/constants'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
Expand Down Expand Up @@ -1069,4 +1072,17 @@ export default class NextNodeServer extends BaseServer {

return result
}

private _cachedPreviewManifest: PrerenderManifest | undefined
protected getPrerenderManifest(): PrerenderManifest {
if (this._cachedPreviewManifest) {
return this._cachedPreviewManifest
}
const manifest = require(join(this.distDir, PRERENDER_MANIFEST))
return (this._cachedPreviewManifest = manifest)
}

protected getRoutesManifest() {
return require(join(this.distDir, ROUTES_MANIFEST))
}
}

0 comments on commit 76b9a54

Please sign in to comment.