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
feat(nextjs): Add captureRouterTransitionStart hook for capturing navigations (#15981)
We relied on Next.js internals for navigation spans and that inevitably
broke so Next.js added a `onRouterTransitionStart` hook in
`instrumentation-client.ts`
(vercel/next.js#77791) for us to use.
This PR exposes a handler called `captureRouterTransitionStart` for that
hook so that we can actually instrument navigations again.
* This mutable keeps track of what router navigation instrumentation mechanism we are using.
14
+
*
15
+
* The default one is 'router-patch' which is a way of instrumenting that worked up until Next.js 15.3.0 was released.
16
+
* For this method we took the global router instance and simply monkey patched all the router methods like push(), replace(), and so on.
17
+
* This worked because Next.js itself called the router methods for things like the <Link /> component.
18
+
* Vercel decided that it is not good to call these public API methods from within the framework so they switched to an internal system that completely bypasses our monkey patching. This happened in 15.3.0.
19
+
*
20
+
* We raised with Vercel that this breaks our SDK so together with them we came up with an API for `instrumentation-client.ts` called `onRouterTransitionStart` that is called whenever a navigation is kicked off.
21
+
*
22
+
* Now we have the problem of version compatibility.
23
+
* For older Next.js versions we cannot use the new hook so we need to always patch the router.
24
+
* For newer Next.js versions we cannot know whether the user actually registered our handler for the `onRouterTransitionStart` hook, so we need to wait until it was called at least once before switching the instrumentation mechanism.
25
+
* The problem is, that the user may still have registered a hook and then call a patched router method.
26
+
* First, the monkey patched router method will be called, starting a navigation span, then the hook will also called.
27
+
* We need to handle this case and not create two separate navigation spans but instead update the current navigation span and then switch to the new instrumentation mode.
28
+
* This is all denoted by this `navigationRoutingMode` variable.
'[@sentry/nextjs] ACTION REQUIRED: To instrument navigations, the Sentry SDK requires you to export an `onRouterTransitionStart` hook from your `instrumentation-client.(js|ts)` file. You can do so by adding `export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;` to the file.',
0 commit comments