Skip to content

Commit 7bd8658

Browse files
authored
Document lazyRouteManifest option for React Router v6 and v7 (#16397)
Documents: getsentry/sentry-javascript#19086
1 parent 822ac94 commit 7bd8658

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

  • docs/platforms/javascript/guides/react/features/react-router

docs/platforms/javascript/guides/react/features/react-router/v6.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,52 @@ ReactDOM.render(
151151

152152
Now, Sentry should generate `pageload`/`navigation` transactions with parameterized transaction names (for example, `/teams/:teamid/user/:userid`), where applicable. This is only needed at the top level of your app, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized.
153153

154+
## Static Route Manifest
155+
156+
<AvailableSince version="10.39.0" />
157+
158+
When using [`patchRoutesOnNavigation`](https://reactrouter.com/6.30.3/routers/create-browser-router#optspatchroutesonnavigation) to dynamically load route definitions, the full route hierarchy isn't available to Sentry until each route is navigated to. This can cause transactions to receive incomplete or wildcard names (for example, `/users/*` instead of `/users/:userId`).
159+
160+
To ensure accurate transaction names, you can provide a static list of route patterns via the `lazyRouteManifest` option. When provided, Sentry uses this manifest as the primary source for determining transaction names without needing to wait for route modules to load.
161+
162+
Make sure to keep the `lazyRouteManifest` array in sync with your route definitions: if you add, remove, or change routes in your app, update this list accordingly. Any route that doesn't match a pattern in the manifest will fall back to the default behavior, which may result in incomplete transaction names until the route is visited. You can also include non-lazy routes in the manifest for convenience or consistency.
163+
164+
To use `lazyRouteManifest`, you need to set `enableAsyncRouteHandlers: true` in your `reactRouterV6BrowserTracingIntegration` configuration:
165+
166+
```javascript {20-27}
167+
import React from "react";
168+
import {
169+
createRoutesFromChildren,
170+
matchRoutes,
171+
useLocation,
172+
useNavigationType,
173+
} from "react-router-dom";
174+
175+
import * as Sentry from "@sentry/react";
176+
177+
Sentry.init({
178+
dsn: "___PUBLIC_DSN___",
179+
integrations: [
180+
Sentry.reactRouterV6BrowserTracingIntegration({
181+
useEffect: React.useEffect,
182+
useLocation,
183+
useNavigationType,
184+
createRoutesFromChildren,
185+
matchRoutes,
186+
enableAsyncRouteHandlers: true,
187+
lazyRouteManifest: [
188+
"/users",
189+
"/users/:userId",
190+
"/users/:userId/settings",
191+
"/dashboard",
192+
"/dashboard/analytics",
193+
],
194+
}),
195+
],
196+
tracesSampleRate: 1.0,
197+
});
198+
```
199+
154200
## Set Up a Custom Error Boundary
155201

156202
When using `react-router`, errors thrown inside route elements will only be re-thrown in **development mode** while using [`strict mode`](https://react.dev/reference/react/StrictMode).\

docs/platforms/javascript/guides/react/features/react-router/v7.mdx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ sidebar_order: 10
55
---
66

77
<Alert level="info" title="Looking for framework mode?">
8-
React Router v7 (framework mode) is currently in Beta, check out
9-
the docs [here](/platforms/javascript/guides/react-router/).
8+
React Router v7 (framework mode) is currently in Beta, check out the docs
9+
[here](/platforms/javascript/guides/react-router/).
1010
</Alert>
1111
Apply the following setup steps based on your routing method and create a
1212
[custom error boundary](#set-up-a-custom-error-boundary) to make sure Sentry
@@ -155,6 +155,52 @@ ReactDOM.render(
155155

156156
Now, Sentry should generate `pageload`/`navigation` transactions with parameterized transaction names (for example, `/teams/:teamid/user/:userid`), where applicable. This is only needed at the top level of your app, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized.
157157

158+
## Static Route Manifest
159+
160+
<AvailableSince version="10.39.0" />
161+
162+
When using [`patchRoutesOnNavigation`](https://reactrouter.com/api/data-routers/createBrowserRouter#optspatchroutesonnavigation) to dynamically load route definitions, the full route hierarchy isn't available to Sentry until each route is navigated to. This can cause transactions to receive incomplete or wildcard names (for example, `/users/*` instead of `/users/:userId`).
163+
164+
To ensure accurate transaction names, you can provide a static list of route patterns via the `lazyRouteManifest` option. When provided, Sentry uses this manifest as the primary source for determining transaction names without needing to wait for route modules to load.
165+
166+
Make sure to keep the `lazyRouteManifest` array in sync with your route definitions: if you add, remove, or change routes in your app, update this list accordingly. Any route that doesn't match a pattern in the manifest will fall back to the default behavior, which may result in incomplete transaction names until the route is visited. You can also include non-lazy routes in the manifest for convenience or consistency.
167+
168+
To use `lazyRouteManifest`, you need to set `enableAsyncRouteHandlers: true` in your `reactRouterV7BrowserTracingIntegration` configuration:
169+
170+
```javascript {20-27}
171+
import React from "react";
172+
import {
173+
createRoutesFromChildren,
174+
matchRoutes,
175+
useLocation,
176+
useNavigationType,
177+
} from "react-router";
178+
179+
import * as Sentry from "@sentry/react";
180+
181+
Sentry.init({
182+
dsn: "___PUBLIC_DSN___",
183+
integrations: [
184+
Sentry.reactRouterV7BrowserTracingIntegration({
185+
useEffect: React.useEffect,
186+
useLocation,
187+
useNavigationType,
188+
createRoutesFromChildren,
189+
matchRoutes,
190+
enableAsyncRouteHandlers: true,
191+
lazyRouteManifest: [
192+
"/users",
193+
"/users/:userId",
194+
"/users/:userId/settings",
195+
"/dashboard",
196+
"/dashboard/analytics",
197+
],
198+
}),
199+
],
200+
tracesSampleRate: 1.0,
201+
});
202+
```
203+
158204
## Set Up a Custom Error Boundary
159205

160206
When using `react-router`, errors thrown inside route elements will only be re-thrown in **development mode** while using [`strict mode`](https://react.dev/reference/react/StrictMode).\

0 commit comments

Comments
 (0)