From d3ad6aed929d6d6c5dc58384c26270d0c258617c Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 12 Jan 2022 17:45:23 +0100 Subject: [PATCH] remove require from base-server --- packages/next/lib/load-custom-routes.ts | 3 +- packages/next/server/base-server.ts | 136 ++++-------------------- packages/next/server/next-server.ts | 127 +++++++++++++++++++++- packages/next/shared/lib/utils.ts | 2 +- 4 files changed, 150 insertions(+), 118 deletions(-) diff --git a/packages/next/lib/load-custom-routes.ts b/packages/next/lib/load-custom-routes.ts index 1cad863a23274..87765abc4267e 100644 --- a/packages/next/lib/load-custom-routes.ts +++ b/packages/next/lib/load-custom-routes.ts @@ -1,6 +1,7 @@ +import type { NextConfig } from '../server/config' + import chalk from 'next/dist/compiled/chalk' import { parse as parseUrl } from 'url' -import { NextConfig } from '../server/config' import * as pathToRegexp from 'next/dist/compiled/path-to-regexp' import { escapeStringRegexp } from '../shared/lib/escape-regexp' import { diff --git a/packages/next/server/base-server.ts b/packages/next/server/base-server.ts index 44a39d9fe60db..de408ad8ecbcf 100644 --- a/packages/next/server/base-server.ts +++ b/packages/next/server/base-server.ts @@ -60,10 +60,7 @@ import { } from './api-utils' import { isTargetLikeServerless } from './config' 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 { getPagePath, requireFontManifest } from './require' import Router, { replaceBasePath, route } from './router' import { compileNonPath, @@ -90,7 +87,6 @@ import { PreviewData } from 'next/types' import ResponseCache from './response-cache' import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url' import isError, { getProperError } from '../lib/is-error' -import { getMiddlewareInfo } from './require' import { MIDDLEWARE_ROUTE } from '../lib/constants' import { run } from './web/sandbox' import { addRequestMeta, getRequestMeta } from './request-meta' @@ -193,7 +189,7 @@ export default abstract class Server { basePath: string optimizeFonts: boolean images: string - fontManifest: FontManifest + fontManifest?: FontManifest optimizeImages: boolean disableOptimizedLoading?: boolean optimizeCss: any @@ -220,7 +216,21 @@ export default abstract class Server { protected abstract getPagesManifest(): PagesManifest | undefined protected abstract getBuildId(): string protected abstract generatePublicRoutes(): Route[] + protected abstract generateImageRoutes(): Route[] protected abstract getFilesystemPaths(): Set + protected abstract findPageComponents( + pathname: string, + query?: NextParsedUrlQuery, + params?: Params | null + ): Promise + 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({ dir = '.', @@ -272,8 +282,8 @@ export default abstract class Server { optimizeFonts: !!this.nextConfig.optimizeFonts && !dev, fontManifest: this.nextConfig.optimizeFonts && !dev - ? requireFontManifest(this.distDir, this._isLikeServerless) - : null, + ? this.getFontManifest() + : undefined, optimizeImages: !!this.nextConfig.experimental.optimizeImages, optimizeCss: this.nextConfig.experimental.optimizeCss, disableOptimizedLoading: @@ -652,7 +662,7 @@ export default abstract class Server { ): Promise { try { return ( - getMiddlewareInfo({ + this.getMiddlewareInfo({ dev: this.renderOpts.dev, distDir: this.distDir, page: pathname, @@ -715,7 +725,7 @@ export default abstract class Server { await this.ensureMiddleware(middleware.page, middleware.ssr) - const middlewareInfo = getMiddlewareInfo({ + const middlewareInfo = this.getMiddlewareInfo({ dev: this.renderOpts.dev, distDir: this.distDir, page: middleware.page, @@ -792,8 +802,8 @@ export default abstract class Server { dynamicRoutes: DynamicRoutes | undefined locales: string[] } { - const server: Server = this const publicRoutes = this.generatePublicRoutes() + const imageRoutes = this.generateImageRoutes() const staticFilesRoute = this.hasStaticDir ? [ @@ -926,32 +936,7 @@ export default abstract class Server { } }, }, - { - 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 - ) - }, - }, + ...imageRoutes, { match: route('/_next/:path*'), type: 'route', @@ -1442,26 +1427,10 @@ export default abstract class Server { } } - private async getPagePath( - pathname: string, - locales?: string[] - ): Promise { - return getPagePath( - pathname, - this.distDir, - this._isLikeServerless, - this.renderOpts.dev, - locales - ) - } - protected async hasPage(pathname: string): Promise { let found = false try { - found = !!(await this.getPagePath( - pathname, - this.nextConfig.i18n?.locales - )) + found = !!this.getPagePath(pathname, this.nextConfig.i18n?.locales) } catch (_) {} return found @@ -1515,7 +1484,7 @@ export default abstract class Server { let builtPagePath try { - builtPagePath = await this.getPagePath(page) + builtPagePath = this.getPagePath(page) } catch (err) { if (isError(err) && err.code === 'ENOENT') { return false @@ -1715,65 +1684,6 @@ export default abstract class Server { }) } - protected async findPageComponents( - pathname: string, - query: NextParsedUrlQuery = {}, - params: Params | null = null - ): Promise { - 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<{ staticPaths: string[] | undefined fallbackMode: 'static' | 'blocking' | false diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 1778e9f112316..1ee7e77538297 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -1,13 +1,20 @@ -import type { Route } from './router' +import type { Route, Params } from './router' import type { CacheFs } from '../shared/lib/utils' +import type { NextParsedUrlQuery } from './request-meta' +import type { FontManifest } from './font-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 { recursiveReadDirSync } from './lib/recursive-readdir-sync' 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' @@ -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[] { if (!fs.existsSync(this.publicDir)) return [] @@ -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 { + 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 { return { readFile: (f) => fs.promises.readFile(f, 'utf8'), @@ -144,4 +256,13 @@ export default class NextNodeServer extends BaseServer { stat: (f) => fs.promises.stat(f), } } + + protected getMiddlewareInfo(params: { + dev?: boolean + distDir: string + page: string + serverless: boolean + }) { + return getMiddlewareInfo(params) + } } diff --git a/packages/next/shared/lib/utils.ts b/packages/next/shared/lib/utils.ts index bbee6dc34aa6c..62bf0bc356a29 100644 --- a/packages/next/shared/lib/utils.ts +++ b/packages/next/shared/lib/utils.ts @@ -1,4 +1,3 @@ -import { formatUrl } from './router/utils/format-url' import type { BuildManifest } from '../../server/get-page-files' import type { ComponentType } from 'react' import type { DomainLocale } from '../../server/config' @@ -9,6 +8,7 @@ import type { ParsedUrlQuery } from 'querystring' import type { PreviewData } from 'next/types' import type { UrlObject } from 'url' import { createContext } from 'react' +import { formatUrl } from './router/utils/format-url' export type NextComponentType< C extends BaseContext = NextPageContext,