Skip to content

Commit

Permalink
Add disableInstrumentation option to next.config.js
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
JamesRobertWiseman committed Jan 26, 2025
1 parent 2c1f68e commit e87c74d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/basic-app/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
disableInstrumentation: process.env.NODE_ENV !== 'production'
}
6 changes: 6 additions & 0 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,11 @@ export interface NextConfig extends Record<string, any> {
watchOptions?: {
pollIntervalMs?: number
}

/**
* Disable instrumentation in specific environments.
*/
disableInstrumentation?: boolean | ((options: any) => boolean)
}

export const defaultConfig: NextConfig = {
Expand Down Expand Up @@ -1245,6 +1250,7 @@ export const defaultConfig: NextConfig = {
useCache: undefined,
},
bundlePagesRouterDependencies: false,
disableInstrumentation: false,
}

export async function normalizeConfig(phase: string, config: any) {
Expand Down
8 changes: 8 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?.()
}

Expand Down

0 comments on commit e87c74d

Please sign in to comment.