Skip to content

Commit 2737373

Browse files
authored
doc: instrumentation-client (#77649)
1 parent 906dfd2 commit 2737373

File tree

9 files changed

+78
-108
lines changed

9 files changed

+78
-108
lines changed

docs/01-app/03-building-your-application/06-optimizing/08-analytics.mdx

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ description: Measure and track page performance using Next.js Speed Insights
77

88
Next.js has built-in support for measuring and reporting performance metrics. You can either use the `useReportWebVitals` hook to manage reporting yourself, or alternatively, Vercel provides a [managed service](https://vercel.com/analytics?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) to automatically collect and visualize metrics for you.
99

10+
## Client Instrumentation
11+
12+
For more advanced analytics and monitoring needs, Next.js provides a `instrumentation-client.js|ts` file that runs before your application's frontend code starts executing. This is ideal for setting up global analytics, error tracking, or performance monitoring tools.
13+
14+
To use it, create an `instrumentation-client.js` or `instrumentation-client.ts` file in your application's root directory:
15+
16+
```js filename="instrumentation-client.js"
17+
// Initialize analytics before the app starts
18+
console.log('Analytics initialized')
19+
20+
// Set up global error tracking
21+
window.addEventListener('error', (event) => {
22+
// Send to your error tracking service
23+
reportError(event.error)
24+
})
25+
```
26+
1027
## Build Your Own
1128

1229
<PagesOnly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: instrumentation-client.js
3+
description: Learn how to add client-side instrumentation to track and monitor your Next.js application's frontend performance.
4+
---
5+
6+
The `instrumentation-client.js|ts` file allows you to add monitoring and analytics code that runs before your application's frontend code starts executing. This is useful for setting up performance tracking, error monitoring, or any other client-side observability tools.
7+
8+
To use it, place the file in the **root** of your application or inside a `src` folder.
9+
10+
## Usage
11+
12+
Unlike [server-side instrumentation](/docs/app/building-your-application/optimizing/instrumentation), you don't need to export any specific functions. Simply write your monitoring code directly in the file:
13+
14+
```ts filename="instrumentation-client.ts" switcher
15+
// Set up performance monitoring
16+
performance.mark('app-init')
17+
18+
// Initialize analytics
19+
console.log('Analytics initialized')
20+
21+
// Set up error tracking
22+
window.addEventListener('error', (event) => {
23+
// Send to your error tracking service
24+
reportError(event.error)
25+
})
26+
```
27+
28+
```js filename="instrumentation-client.js" switcher
29+
// Set up performance monitoring
30+
performance.mark('app-init')
31+
32+
// Initialize analytics
33+
console.log('Analytics initialized')
34+
35+
// Set up error tracking
36+
window.addEventListener('error', (event) => {
37+
// Send to your error tracking service
38+
reportError(event.error)
39+
})
40+
```

docs/01-app/04-api-reference/05-config/01-next-config-js/clientInstrumentationHook.mdx

-66
This file was deleted.

packages/next/src/build/webpack/plugins/define-env-plugin.ts

-3
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ export function getDefineEnv({
240240
'process.env.__NEXT_TELEMETRY_DISABLED': Boolean(
241241
process.env.NEXT_TELEMETRY_DISABLED
242242
),
243-
'process.env.__NEXT_EXPERIMENTAL_CLIENT_INSTRUMENTATION_HOOK': Boolean(
244-
config.experimental.clientInstrumentationHook
245-
),
246243
...(isNodeOrEdgeCompilation
247244
? {
248245
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)

packages/next/src/lib/require-instrumentation-client.ts

+21-23
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@
44
* The `private-next-instrumentation-client` module is automatically aliased to
55
* the `instrumentation-client.ts` file in the project root by webpack or turbopack.
66
*/
7-
if (process.env.__NEXT_EXPERIMENTAL_CLIENT_INSTRUMENTATION_HOOK) {
8-
if (process.env.NODE_ENV === 'development') {
9-
const measureName = 'Client Instrumentation Hook'
10-
const startTime = performance.now()
11-
require('private-next-instrumentation-client')
12-
const endTime = performance.now()
7+
if (process.env.NODE_ENV === 'development') {
8+
const measureName = 'Client Instrumentation Hook'
9+
const startTime = performance.now()
10+
require('private-next-instrumentation-client')
11+
const endTime = performance.now()
1312

14-
const duration = endTime - startTime
15-
performance.measure(measureName, {
16-
start: startTime,
17-
end: endTime,
18-
detail: 'Client instrumentation initialization',
19-
})
13+
const duration = endTime - startTime
14+
performance.measure(measureName, {
15+
start: startTime,
16+
end: endTime,
17+
detail: 'Client instrumentation initialization',
18+
})
2019

21-
// Using 16ms threshold as it represents one frame (1000ms/60fps)
22-
// This helps identify if the instrumentation hook initialization
23-
// could potentially cause frame drops during development.
24-
const THRESHOLD = 16
25-
if (duration > THRESHOLD) {
26-
console.log(
27-
`[${measureName}] Slow execution detected: ${duration.toFixed(0)}ms (Note: Code download overhead is not included in this measurement)`
28-
)
29-
}
30-
} else {
31-
require('private-next-instrumentation-client')
20+
// Using 16ms threshold as it represents one frame (1000ms/60fps)
21+
// This helps identify if the instrumentation hook initialization
22+
// could potentially cause frame drops during development.
23+
const THRESHOLD = 16
24+
if (duration > THRESHOLD) {
25+
console.log(
26+
`[${measureName}] Slow execution detected: ${duration.toFixed(0)}ms (Note: Code download overhead is not included in this measurement)`
27+
)
3228
}
29+
} else {
30+
require('private-next-instrumentation-client')
3331
}

packages/next/src/server/config-schema.ts

-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
457457
buildTimeThresholdMs: z.number().int(),
458458
})
459459
.optional(),
460-
clientInstrumentationHook: z.boolean().optional(),
461460
})
462461
.optional(),
463462
exportPathMap: z

test/e2e/instrumentation-client-hook/app-router/next.config.js

-5
This file was deleted.

test/e2e/instrumentation-client-hook/app-with-src/next.config.js

-5
This file was deleted.

test/e2e/instrumentation-client-hook/pages-router/next.config.js

-5
This file was deleted.

0 commit comments

Comments
 (0)