Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 77a9a2c

Browse files
committedMar 31, 2025·
roll out instrumentation-client
1 parent 0bac300 commit 77a9a2c

File tree

8 files changed

+57
-85
lines changed

8 files changed

+57
-85
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/server/config-schema.ts

-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
455455
buildTimeThresholdMs: z.number().int(),
456456
})
457457
.optional(),
458-
clientInstrumentationHook: z.boolean().optional(),
459458
})
460459
.optional(),
461460
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)
Please sign in to comment.