You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/03-file-conventions/instrumentation-client.mdx
+180-2Lines changed: 180 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: instrumentation-client.js
3
3
description: Learn how to add client-side instrumentation to track and monitor your Next.js application's frontend performance.
4
4
---
5
5
6
-
The `instrumentation-client.js|ts` file allows you to add monitoring and analytics codethat 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.
6
+
The `instrumentation-client.js|ts` file allows you to add monitoring, analytics code, and other side-effects that run before your application becomes interactive. This is useful for setting up performance tracking, error monitoring, polyfills, or any other client-side observability tools.
7
7
8
8
To use it, place the file in the **root** of your application or inside a `src` folder.
**Error handling:** Implement try-catch blocks around your instrumentation code to ensure robust monitoring. This prevents individual tracking failures from affecting other instrumentation features.
43
+
44
+
## Router navigation tracking
45
+
46
+
You can export an `onRouterTransitionStart` function to receive notifications when navigation begins:
console.log(`Navigation started: ${navigationType} to ${url}`)
65
+
performance.mark(`nav-start-${Date.now()}`)
66
+
}
67
+
```
68
+
69
+
The `onRouterTransitionStart` function receives two parameters:
70
+
71
+
-`url: string` - The URL being navigated to
72
+
-`navigationType: 'push' | 'replace' | 'traverse'` - The type of navigation
73
+
74
+
## Performance considerations
75
+
76
+
Keep instrumentation code lightweight.
77
+
78
+
Next.js monitors initialization time in development and will log warnings if it takes longer than 16ms, which could impact smooth page loading.
79
+
80
+
## Execution timing
81
+
82
+
The `instrumentation-client.js` file executes at a specific point in the application lifecycle:
83
+
84
+
1.**After** the HTML document is loaded
85
+
2.**Before** React hydration begins
86
+
3.**Before** user interactions are possible
87
+
88
+
This timing makes it ideal for setting up error tracking, analytics, and performance monitoring that needs to capture early application lifecycle events.
89
+
90
+
## Examples
91
+
92
+
### Error tracking
93
+
94
+
Initialize error tracking before React starts and add navigation breadcrumbs for better debugging context.
if (entry instanceofPerformanceNavigationTiming) {
184
+
console.log('Time to Interactive:', entry.loadEventEnd- startTime)
185
+
}
186
+
}
187
+
})
188
+
189
+
observer.observe({ entryTypes: ['navigation'] })
190
+
191
+
exportfunctiononRouterTransitionStart(url) {
192
+
performance.mark(`nav-start-${url}`)
193
+
}
194
+
```
195
+
196
+
### Polyfills
197
+
198
+
Load polyfills before application code runs. Use static imports for immediate loading and dynamic imports for conditional loading based on feature detection.
Copy file name to clipboardExpand all lines: docs/03-architecture/supported-browsers.mdx
+50Lines changed: 50 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,57 @@ In addition, to reduce bundle size, Next.js will only load these polyfills for b
43
43
44
44
If your own code or any external npm dependencies require features not supported by your target browsers (such as IE 11), you need to add polyfills yourself.
45
45
46
+
<PagesOnly>
46
47
In this case, you should add a top-level import for the **specific polyfill** you need in your [Custom `<App>`](/docs/pages/building-your-application/routing/custom-app) or the individual component.
48
+
</PagesOnly>
49
+
50
+
<AppOnly>
51
+
52
+
To include polyfills, you can import them into the [`instrumentation-client.js` file](/docs/app/api-reference/file-conventions/instrumentation-client).
53
+
54
+
```ts filename="instrumentation-client.ts"
55
+
import'./polyfills'
56
+
```
57
+
58
+
</AppOnly>
59
+
60
+
The best approach is to isolate unsupported features to specific UI sections and conditionally load the polyfill if needed.
0 commit comments