Unverified Commit 5572eb39 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Abstract out native filesystem usage from the base server (#33226)

This PR moves `require` and `fs` usage from the base server to the node server. Closes #32322.

## 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`
parent 6c8808e9
import type { NextConfig } from '../server/config'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import { parse as parseUrl } from 'url' import { parse as parseUrl } from 'url'
import { NextConfig } from '../server/config'
import * as pathToRegexp from 'next/dist/compiled/path-to-regexp' import * as pathToRegexp from 'next/dist/compiled/path-to-regexp'
import { escapeStringRegexp } from '../shared/lib/escape-regexp' import { escapeStringRegexp } from '../shared/lib/escape-regexp'
import { import {
......
...@@ -60,10 +60,7 @@ import { ...@@ -60,10 +60,7 @@ import {
} from './api-utils' } from './api-utils'
import { isTargetLikeServerless } from './config' import { isTargetLikeServerless } from './config'
import pathMatch from '../shared/lib/router/utils/path-match' import pathMatch from '../shared/lib/router/utils/path-match'
import { loadComponents } from './load-components'
import { normalizePagePath } from './normalize-page-path'
import { renderToHTML } from './render' import { renderToHTML } from './render'
import { getPagePath, requireFontManifest } from './require'
import Router, { replaceBasePath, route } from './router' import Router, { replaceBasePath, route } from './router'
import { import {
compileNonPath, compileNonPath,
...@@ -90,7 +87,6 @@ import { PreviewData } from 'next/types' ...@@ -90,7 +87,6 @@ import { PreviewData } from 'next/types'
import ResponseCache from './response-cache' import ResponseCache from './response-cache'
import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url' import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url'
import isError, { getProperError } from '../lib/is-error' import isError, { getProperError } from '../lib/is-error'
import { getMiddlewareInfo } from './require'
import { MIDDLEWARE_ROUTE } from '../lib/constants' import { MIDDLEWARE_ROUTE } from '../lib/constants'
import { run } from './web/sandbox' import { run } from './web/sandbox'
import { addRequestMeta, getRequestMeta } from './request-meta' import { addRequestMeta, getRequestMeta } from './request-meta'
...@@ -193,7 +189,7 @@ export default abstract class Server { ...@@ -193,7 +189,7 @@ export default abstract class Server {
basePath: string basePath: string
optimizeFonts: boolean optimizeFonts: boolean
images: string images: string
fontManifest: FontManifest fontManifest?: FontManifest
optimizeImages: boolean optimizeImages: boolean
disableOptimizedLoading?: boolean disableOptimizedLoading?: boolean
optimizeCss: any optimizeCss: any
...@@ -220,7 +216,21 @@ export default abstract class Server { ...@@ -220,7 +216,21 @@ export default abstract class Server {
protected abstract getPagesManifest(): PagesManifest | undefined protected abstract getPagesManifest(): PagesManifest | undefined
protected abstract getBuildId(): string protected abstract getBuildId(): string
protected abstract generatePublicRoutes(): Route[] protected abstract generatePublicRoutes(): Route[]
protected abstract generateImageRoutes(): Route[]
protected abstract getFilesystemPaths(): Set<string> protected abstract getFilesystemPaths(): Set<string>
protected abstract findPageComponents(
pathname: string,
query?: NextParsedUrlQuery,
params?: Params | null
): Promise<FindComponentsResult | null>
protected abstract getMiddlewareInfo(params: {
dev?: boolean
distDir: string
page: string
serverless: boolean
}): { name: string; paths: string[]; env: string[] }
protected abstract getPagePath(pathname: string, locales?: string[]): string
protected abstract getFontManifest(): FontManifest | undefined
public constructor({ public constructor({
dir = '.', dir = '.',
...@@ -272,8 +282,8 @@ export default abstract class Server { ...@@ -272,8 +282,8 @@ export default abstract class Server {
optimizeFonts: !!this.nextConfig.optimizeFonts && !dev, optimizeFonts: !!this.nextConfig.optimizeFonts && !dev,
fontManifest: fontManifest:
this.nextConfig.optimizeFonts && !dev this.nextConfig.optimizeFonts && !dev
? requireFontManifest(this.distDir, this._isLikeServerless) ? this.getFontManifest()
: null, : undefined,
optimizeImages: !!this.nextConfig.experimental.optimizeImages, optimizeImages: !!this.nextConfig.experimental.optimizeImages,
optimizeCss: this.nextConfig.experimental.optimizeCss, optimizeCss: this.nextConfig.experimental.optimizeCss,
disableOptimizedLoading: disableOptimizedLoading:
...@@ -652,7 +662,7 @@ export default abstract class Server { ...@@ -652,7 +662,7 @@ export default abstract class Server {
): Promise<boolean> { ): Promise<boolean> {
try { try {
return ( return (
getMiddlewareInfo({ this.getMiddlewareInfo({
dev: this.renderOpts.dev, dev: this.renderOpts.dev,
distDir: this.distDir, distDir: this.distDir,
page: pathname, page: pathname,
...@@ -715,7 +725,7 @@ export default abstract class Server { ...@@ -715,7 +725,7 @@ export default abstract class Server {
await this.ensureMiddleware(middleware.page, middleware.ssr) await this.ensureMiddleware(middleware.page, middleware.ssr)
const middlewareInfo = getMiddlewareInfo({ const middlewareInfo = this.getMiddlewareInfo({
dev: this.renderOpts.dev, dev: this.renderOpts.dev,
distDir: this.distDir, distDir: this.distDir,
page: middleware.page, page: middleware.page,
...@@ -792,8 +802,8 @@ export default abstract class Server { ...@@ -792,8 +802,8 @@ export default abstract class Server {
dynamicRoutes: DynamicRoutes | undefined dynamicRoutes: DynamicRoutes | undefined
locales: string[] locales: string[]
} { } {
const server: Server = this
const publicRoutes = this.generatePublicRoutes() const publicRoutes = this.generatePublicRoutes()
const imageRoutes = this.generateImageRoutes()
const staticFilesRoute = this.hasStaticDir const staticFilesRoute = this.hasStaticDir
? [ ? [
...@@ -926,32 +936,7 @@ export default abstract class Server { ...@@ -926,32 +936,7 @@ export default abstract class Server {
} }
}, },
}, },
{ ...imageRoutes,
match: route('/_next/image'),
type: 'route',
name: '_next/image catchall',
fn: (req, res, _params, parsedUrl) => {
if (this.minimalMode) {
res.statusCode = 400
res.end('Bad Request')
return {
finished: true,
}
}
const { imageOptimizer } =
require('./image-optimizer') as typeof import('./image-optimizer')
return imageOptimizer(
server,
req,
res,
parsedUrl,
server.nextConfig,
server.distDir,
this.renderOpts.dev
)
},
},
{ {
match: route('/_next/:path*'), match: route('/_next/:path*'),
type: 'route', type: 'route',
...@@ -1442,26 +1427,10 @@ export default abstract class Server { ...@@ -1442,26 +1427,10 @@ export default abstract class Server {
} }
} }
private async getPagePath(
pathname: string,
locales?: string[]
): Promise<string> {
return getPagePath(
pathname,
this.distDir,
this._isLikeServerless,
this.renderOpts.dev,
locales
)
}
protected async hasPage(pathname: string): Promise<boolean> { protected async hasPage(pathname: string): Promise<boolean> {
let found = false let found = false
try { try {
found = !!(await this.getPagePath( found = !!this.getPagePath(pathname, this.nextConfig.i18n?.locales)
pathname,
this.nextConfig.i18n?.locales
))
} catch (_) {} } catch (_) {}
return found return found
...@@ -1515,7 +1484,7 @@ export default abstract class Server { ...@@ -1515,7 +1484,7 @@ export default abstract class Server {
let builtPagePath let builtPagePath
try { try {
builtPagePath = await this.getPagePath(page) builtPagePath = this.getPagePath(page)
} catch (err) { } catch (err) {
if (isError(err) && err.code === 'ENOENT') { if (isError(err) && err.code === 'ENOENT') {
return false return false
...@@ -1715,65 +1684,6 @@ export default abstract class Server { ...@@ -1715,65 +1684,6 @@ export default abstract class Server {
}) })
} }
protected async findPageComponents(
pathname: string,
query: NextParsedUrlQuery = {},
params: Params | null = null
): Promise<FindComponentsResult | null> {
let paths = [
// try serving a static AMP version first
query.amp ? normalizePagePath(pathname) + '.amp' : null,
pathname,
].filter(Boolean)
if (query.__nextLocale) {
paths = [
...paths.map(
(path) => `/${query.__nextLocale}${path === '/' ? '' : path}`
),
...paths,
]
}
for (const pagePath of paths) {
try {
const components = await loadComponents(
this.distDir,
pagePath!,
!this.renderOpts.dev && this._isLikeServerless
)
if (
query.__nextLocale &&
typeof components.Component === 'string' &&
!pagePath?.startsWith(`/${query.__nextLocale}`)
) {
// if loading an static HTML file the locale is required
// to be present since all HTML files are output under their locale
continue
}
return {
components,
query: {
...(components.getStaticProps
? ({
amp: query.amp,
_nextDataReq: query._nextDataReq,
__nextLocale: query.__nextLocale,
__nextDefaultLocale: query.__nextDefaultLocale,
} as NextParsedUrlQuery)
: query),
...(params || {}),
},
}
} catch (err) {
if (isError(err) && err.code !== 'ENOENT') throw err
}
}
return null
}
protected async getStaticPaths(pathname: string): Promise<{ protected async getStaticPaths(pathname: string): Promise<{
staticPaths: string[] | undefined staticPaths: string[] | undefined
fallbackMode: 'static' | 'blocking' | false fallbackMode: 'static' | 'blocking' | false
......
import type { Route } from './router' import type { Route, Params } from './router'
import type { CacheFs } from '../shared/lib/utils' import type { CacheFs } from '../shared/lib/utils'
import type { NextParsedUrlQuery } from './request-meta'
import type { FontManifest } from './font-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 { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
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, { FindComponentsResult } from './base-server'
import { getMiddlewareInfo } from './require'
import { loadComponents } from './load-components'
import isError from '../lib/is-error'
import { normalizePagePath } from './normalize-page-path'
import { getPagePath, requireFontManifest } from './require'
import { BUILD_ID_FILE, PAGES_MANIFEST } from '../shared/lib/constants'
export * from './base-server' export * from './base-server'
...@@ -36,6 +43,38 @@ export default class NextNodeServer extends BaseServer { ...@@ -36,6 +43,38 @@ export default class NextNodeServer extends BaseServer {
} }
} }
protected generateImageRoutes(): Route[] {
const server = this
return [
{
match: route('/_next/image'),
type: 'route',
name: '_next/image catchall',
fn: (req, res, _params, parsedUrl) => {
if (this.minimalMode) {
res.statusCode = 400
res.end('Bad Request')
return {
finished: true,
}
}
const { imageOptimizer } =
require('./image-optimizer') as typeof import('./image-optimizer')
return imageOptimizer(
server,
req,
res,
parsedUrl,
server.nextConfig,
server.distDir,
this.renderOpts.dev
)
},
},
]
}
protected generatePublicRoutes(): Route[] { protected generatePublicRoutes(): Route[] {
if (!fs.existsSync(this.publicDir)) return [] if (!fs.existsSync(this.publicDir)) return []
...@@ -135,6 +174,79 @@ export default class NextNodeServer extends BaseServer { ...@@ -135,6 +174,79 @@ export default class NextNodeServer extends BaseServer {
])) ]))
} }
protected getPagePath(pathname: string, locales?: string[]): string {
return getPagePath(
pathname,
this.distDir,
this._isLikeServerless,
this.renderOpts.dev,
locales
)
}
protected async findPageComponents(
pathname: string,
query: NextParsedUrlQuery = {},
params: Params | null = null
): Promise<FindComponentsResult | null> {
let paths = [
// try serving a static AMP version first
query.amp ? normalizePagePath(pathname) + '.amp' : null,
pathname,
].filter(Boolean)
if (query.__nextLocale) {
paths = [
...paths.map(
(path) => `/${query.__nextLocale}${path === '/' ? '' : path}`
),
...paths,
]
}
for (const pagePath of paths) {
try {
const components = await loadComponents(
this.distDir,
pagePath!,
!this.renderOpts.dev && this._isLikeServerless
)
if (
query.__nextLocale &&
typeof components.Component === 'string' &&
!pagePath?.startsWith(`/${query.__nextLocale}`)
) {
// if loading an static HTML file the locale is required
// to be present since all HTML files are output under their locale
continue
}
return {
components,
query: {
...(components.getStaticProps
? ({
amp: query.amp,
_nextDataReq: query._nextDataReq,
__nextLocale: query.__nextLocale,
__nextDefaultLocale: query.__nextDefaultLocale,
} as NextParsedUrlQuery)
: query),
...(params || {}),
},
}
} catch (err) {
if (isError(err) && err.code !== 'ENOENT') throw err
}
}
return null
}
protected getFontManifest(): FontManifest {
return requireFontManifest(this.distDir, this._isLikeServerless)
}
protected getCacheFilesystem(): CacheFs { protected getCacheFilesystem(): CacheFs {
return { return {
readFile: (f) => fs.promises.readFile(f, 'utf8'), readFile: (f) => fs.promises.readFile(f, 'utf8'),
...@@ -144,4 +256,13 @@ export default class NextNodeServer extends BaseServer { ...@@ -144,4 +256,13 @@ export default class NextNodeServer extends BaseServer {
stat: (f) => fs.promises.stat(f), stat: (f) => fs.promises.stat(f),
} }
} }
protected getMiddlewareInfo(params: {
dev?: boolean
distDir: string
page: string
serverless: boolean
}) {
return getMiddlewareInfo(params)
}
} }
import { formatUrl } from './router/utils/format-url'
import type { BuildManifest } from '../../server/get-page-files' import type { BuildManifest } from '../../server/get-page-files'
import type { ComponentType } from 'react' import type { ComponentType } from 'react'
import type { DomainLocale } from '../../server/config' import type { DomainLocale } from '../../server/config'
...@@ -9,6 +8,7 @@ import type { ParsedUrlQuery } from 'querystring' ...@@ -9,6 +8,7 @@ import type { ParsedUrlQuery } from 'querystring'
import type { PreviewData } from 'next/types' import type { PreviewData } from 'next/types'
import type { UrlObject } from 'url' import type { UrlObject } from 'url'
import { createContext } from 'react' import { createContext } from 'react'
import { formatUrl } from './router/utils/format-url'
export type NextComponentType< export type NextComponentType<
C extends BaseContext = NextPageContext, C extends BaseContext = NextPageContext,
......
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