diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 3f928b43a73fb..88838c03b8202 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -3,7 +3,7 @@ import fs from 'fs' import chalk from 'chalk' import { IncomingMessage, ServerResponse } from 'http' import Proxy from 'next/dist/compiled/http-proxy' -import { join, relative, resolve, sep } from 'path' +import { join, relative, resolve, sep, extname } from 'path' import { parse as parseQs, stringify as stringifyQs, @@ -242,6 +242,21 @@ export default class Server { this.pagesManifest = require(pagesManifestPath) } + // Pages pre-import speed up initial pages render + if (this.pagesManifest && !this._isLikeServerless) { + [ + this.pagesManifest['/_document'], + this.pagesManifest['/_app'], + ...Object.values(this.pagesManifest), + ].forEach((pageModule) => { + if (extname(pageModule) !== '.js') { + return + } + + require(join(this.serverBuildDir, pageModule)) + }) + } + this.customRoutes = this.getCustomRoutes() this.router = new Router(this.generateRoutes()) this.setAssetPrefix(assetPrefix)