From e87c74d96d6ce52c4ed48cc62de8d3f43ff25387 Mon Sep 17 00:00:00 2001 From: James Wiseman <62024516+JamesRobertWiseman@users.noreply.github.com> Date: Sun, 26 Jan 2025 15:11:32 +0200 Subject: [PATCH] Add disableInstrumentation option to next.config.js Add `disableInstrumentation` configuration option to control environment-specific instrumentation. * **Configuration Changes** - Add `disableInstrumentation` to the `NextConfig` type definition in `packages/next/src/server/config-shared.ts`. - Add `disableInstrumentation` to the `defaultConfig` object in `packages/next/src/server/config-shared.ts` with a default value of `false`. - Add logic to handle the `disableInstrumentation` option in the `assignDefaults` function in `packages/next/src/server/config.ts`. * **Instrumentation Control** - Add a check for the `disableInstrumentation` option in the `runInstrumentationHookIfAvailable` function in `packages/next/src/server/next-server.ts`. - Skip the instrumentation registration if `disableInstrumentation` is `true` in `packages/next/src/server/next-server.ts`. * **Example** - Add an example of using the `disableInstrumentation` option in the `next.config.js` file in `examples/basic-app/next.config.js`. --- examples/basic-app/next.config.js | 3 +++ packages/next/src/server/config-shared.ts | 6 ++++++ packages/next/src/server/config.ts | 8 ++++++++ packages/next/src/server/next-server.ts | 3 +++ 4 files changed, 20 insertions(+) create mode 100644 examples/basic-app/next.config.js diff --git a/examples/basic-app/next.config.js b/examples/basic-app/next.config.js new file mode 100644 index 0000000000000..36a9adb84e094 --- /dev/null +++ b/examples/basic-app/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + disableInstrumentation: process.env.NODE_ENV !== 'production' +} diff --git a/packages/next/src/server/config-shared.ts b/packages/next/src/server/config-shared.ts index 44167144e9caa..9077e233353d8 100644 --- a/packages/next/src/server/config-shared.ts +++ b/packages/next/src/server/config-shared.ts @@ -1058,6 +1058,11 @@ export interface NextConfig extends Record { watchOptions?: { pollIntervalMs?: number } + + /** + * Disable instrumentation in specific environments. + */ + disableInstrumentation?: boolean | ((options: any) => boolean) } export const defaultConfig: NextConfig = { @@ -1245,6 +1250,7 @@ export const defaultConfig: NextConfig = { useCache: undefined, }, bundlePagesRouterDependencies: false, + disableInstrumentation: false, } export async function normalizeConfig(phase: string, config: any) { diff --git a/packages/next/src/server/config.ts b/packages/next/src/server/config.ts index c14083394e356..7190f6e598063 100644 --- a/packages/next/src/server/config.ts +++ b/packages/next/src/server/config.ts @@ -1024,6 +1024,14 @@ function assignDefaults( result.experimental.useCache = result.experimental.dynamicIO } + // Handle the disableInstrumentation option + if (typeof result.disableInstrumentation === 'function') { + result.disableInstrumentation = result.disableInstrumentation({ + dev: process.env.NODE_ENV !== 'production', + isServer: typeof window === 'undefined', + }) + } + return result } diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 0195abc7c0258..436bc5f0642b7 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -357,6 +357,9 @@ export default class NextNodeServer extends BaseServer< protected async runInstrumentationHookIfAvailable() { if (this.registeredInstrumentation) return this.registeredInstrumentation = true + if (this.nextConfig.disableInstrumentation) { + return + } await this.instrumentation?.register?.() }