From 2eceef36cc424e89032c522dfc4d26a9baf70519 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Mon, 2 Oct 2023 20:25:51 +0200 Subject: [PATCH] fix instrumentation with bundled server --- packages/next/src/server/next-server.ts | 29 +++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 9f8c3c6ebc914..b2136df1a2718 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -101,6 +101,12 @@ import { loadManifest } from './load-manifest' export * from './base-server' +declare const __non_webpack_require__: NodeRequire + +const dynamicRequire = process.env.NEXT_MINIMAL + ? __non_webpack_require__ + : require + function writeStdoutLine(text: string) { process.stdout.write(' ' + text + '\n') } @@ -244,12 +250,15 @@ export default class NextNodeServer extends BaseServer { this.nextConfig.experimental.instrumentationHook ) { try { - const instrumentationHook = await require(resolve( - this.serverOptions.dir || '.', - this.serverOptions.conf.distDir!, - 'server', - INSTRUMENTATION_HOOK_FILENAME - )) + const instrumentationHook = await dynamicRequire( + resolve( + this.serverOptions.dir || '.', + this.serverOptions.conf.distDir!, + 'server', + INSTRUMENTATION_HOOK_FILENAME + ) + ) + await instrumentationHook.register?.() } catch (err: any) { if (err.code !== 'MODULE_NOT_FOUND') { @@ -289,9 +298,11 @@ export default class NextNodeServer extends BaseServer { const { incrementalCacheHandlerPath } = this.nextConfig.experimental if (incrementalCacheHandlerPath) { - CacheHandler = require(isAbsolute(incrementalCacheHandlerPath) - ? incrementalCacheHandlerPath - : join(this.distDir, incrementalCacheHandlerPath)) + CacheHandler = dynamicRequire( + isAbsolute(incrementalCacheHandlerPath) + ? incrementalCacheHandlerPath + : join(this.distDir, incrementalCacheHandlerPath) + ) CacheHandler = CacheHandler.default || CacheHandler }