From 926c1683d8173955fee75e5cd989c1e49820f6e4 Mon Sep 17 00:00:00 2001 From: Vladlen Grachev Date: Thu, 18 Mar 2021 18:18:19 +0300 Subject: [PATCH 1/3] Add _document and _app pre-import --- packages/next/next-server/server/next-server.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 3f928b43a73fb..0aabdfb8b9abe 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -242,6 +242,12 @@ export default class Server { this.pagesManifest = require(pagesManifestPath) } + // Modules cache pre-warm speed up initial pages render + if (this.pagesManifest && !this._isLikeServerless) { + require(join(this.serverBuildDir, this.pagesManifest['/_document'])) + require(join(this.serverBuildDir, this.pagesManifest['/_app'])) + } + this.customRoutes = this.getCustomRoutes() this.router = new Router(this.generateRoutes()) this.setAssetPrefix(assetPrefix) From e1e0c8f38e2b6c86765a3c4cbd0237cfa7b3cf42 Mon Sep 17 00:00:00 2001 From: Vladlen Grachev Date: Fri, 23 Jul 2021 10:59:23 +0300 Subject: [PATCH 2/3] Add experimantal prewarmRequiredPages config --- packages/next/server/config-shared.ts | 2 ++ packages/next/server/next-server.ts | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/next/server/config-shared.ts b/packages/next/server/config-shared.ts index 3ce60e14da863..723be6476c313 100644 --- a/packages/next/server/config-shared.ts +++ b/packages/next/server/config-shared.ts @@ -102,6 +102,7 @@ export type NextConfig = { [key: string]: any } & { staticPageGenerationTimeout?: number pageDataCollectionTimeout?: number isrMemoryCacheSize?: number + prewarmRequiredPages?: boolean } } @@ -169,6 +170,7 @@ export const defaultConfig: NextConfig = { pageDataCollectionTimeout: 60, // default to 50MB limit isrMemoryCacheSize: 50 * 1024 * 1024, + prewarmRequiredPages: false, }, future: { strictPostcssConfiguration: false, diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 918badc90fe4b..79074e14b7922 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -264,7 +264,11 @@ export default class Server { } // Modules cache pre-warm speed up initial pages render - if (this.pagesManifest && !this._isLikeServerless) { + if ( + this.nextConfig.experimental.prewarmRequiredPages && + this.pagesManifest && + !this._isLikeServerless + ) { require(join(this.serverBuildDir, this.pagesManifest['/_document'])) require(join(this.serverBuildDir, this.pagesManifest['/_app'])) } From 9f5b7a3a4157cb32e8d6a73578ae05d78c3ee391 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 9 Feb 2022 20:17:43 -0600 Subject: [PATCH 3/3] Add _app and _document pre-import in next-server --- packages/next/server/next-server.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 5930a26a6110f..805b0ae016417 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -128,6 +128,17 @@ export default class NextNodeServer extends BaseServer { }) ) } + + if (!this.renderOpts.dev) { + // pre-warm _document and _app as these will be + // needed for most requests + loadComponents(this.distDir, '/_document', this._isLikeServerless).catch( + () => {} + ) + loadComponents(this.distDir, '/_app', this._isLikeServerless).catch( + () => {} + ) + } } private compression =