instrumentation-client.js
Learn how to add client-side instrumentation to track and monitor your Next.js application's frontend performance.
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.
To use it, place the file in the root of your application or inside a src folder.
Usage
Unlike server-side instrumentation, you do not need to export any specific functions. You can write your monitoring code directly in the file:
Error handling: Implement try-catch blocks around your instrumentation code to ensure robust monitoring. This prevents individual tracking failures from affecting other instrumentation features.
Router navigation tracking
You can export onRouterTransitionStart to observe the start of App Router navigations:
Additional router transition information is experimental. Enable it to receive a third event argument:
The event includes the transition metadata and source context known when the navigation is dispatched:
onRouterTransitionStart receives:
url: string- The URL being navigated tonavigationType: 'push' | 'replace' | 'traverse'- The type of navigationevent.id- An opaque ID shared by events for this transitionevent.timestamp- A framework-captured Unix timestamp in millisecondsevent.fromRoutes- Route patterns visible before navigation. The primarychildrenroute is first, followed by parallel slots in deterministic orderevent.prefetchIntent- For link navigations, whether the clicked link requested full prefetching (full), used automatic prefetching (auto), or did not request prefetching (none). For navigations with no associated link (programmaticrouter.push()/router.replace(), or browser back/forward) this isnull, since no link prefetch intent applies
Route entries use filesystem-style patterns, so a navigation away from /blog/hello may report /blog/[slug].
Hook errors are isolated and do not affect navigation or other hooks.
Performance considerations
Keep instrumentation code lightweight.
Next.js monitors initialization time in development and will log warnings if it takes longer than 16ms, which could impact smooth page loading.
Execution timing
The instrumentation-client.js file executes at a specific point in the application lifecycle:
- After the HTML document is loaded
- Before React hydration begins
- Before user interactions are possible
This timing makes it ideal for setting up error tracking, analytics, and performance monitoring that needs to capture early application lifecycle events.
See also
next.config.js plugins (for example, wrappers like withSentry) can register their own client instrumentation module via the instrumentationClientInject option. Injected modules run before this file, in array order, and may export the same router transition start hook. Application code should continue to use this file convention directly.
Examples
Error tracking
Initialize error tracking before React starts and add navigation breadcrumbs for better debugging context.
Analytics tracking
Initialize analytics and track navigation events with detailed metadata for user behavior analysis.
Performance monitoring
Track Time to Interactive and navigation performance using the Performance Observer API and performance marks.
Polyfills
Load polyfills before application code runs. Use static imports for immediate loading and dynamic imports for conditional loading based on feature detection.
Version history
| Version | Changes |
|---|---|
v16.3.0 | Experimental router transition start event introduced |
v15.3 | instrumentation-client introduced |