From 6506c1ce96cf44ab3917c9b2952a5aa3afbe13df Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 14 Sep 2023 21:02:43 +0200 Subject: [PATCH] Memoize serverDistDir generation (#55375) Was looking into the CPU profiles for `next start` and noticed this work kept being repeated. This reuses instead. --- packages/next/src/server/next-server.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index ad7b8034b2b40..f5d3d7feb62eb 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -145,6 +145,7 @@ function getMiddlewareMatcher( } export default class NextNodeServer extends BaseServer { + private _serverDistDir: string | undefined private imageResponseCache?: ResponseCache protected renderWorkersPromises?: Promise protected renderWorkerOpts?: Parameters< @@ -1886,8 +1887,13 @@ export default class NextNodeServer extends BaseServer { return result } - protected get serverDistDir() { - return join(this.distDir, SERVER_DIRECTORY) + protected get serverDistDir(): string { + if (this._serverDistDir) { + return this._serverDistDir + } + const serverDistDir = join(this.distDir, SERVER_DIRECTORY) + this._serverDistDir = serverDistDir + return serverDistDir } protected async getFallbackErrorComponents(): Promise {