React Router browser tracing - Lazy imported routes with suspense start transaction spans with wrong path #15027
Description
Is there an existing issue for this?
- I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- I have reviewed the documentation https://docs.sentry.io/
- I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Self-hosted/on-premise
Which SDK are you using?
@sentry/react
SDK Version
8.47.0
Framework Version
React 18.3.1
Link to Sentry event
No response
Reproduction Example/SDK Setup
Init sentry with React Router browser tracing integration.
Sentry.init({
dsn,
release,
environment,
attachStacktrace: true,
tracePropagationTargets,
integrations: [
Sentry.reactRouterV7BrowserTracingIntegration({
useEffect,
useLocation,
useNavigationType,
createRoutesFromChildren,
matchRoutes,
}),
],
tracesSampleRate,
});
Configure React Router with Sentry wrapper. But Lazy import routes, e.g. something like:
// user routes bundle loaded lazily
const UserRoutes = lazy(() => import("./UserRoutes"));
const Router: FC = () => {
const { user } = useContext(UserContext);
const sentryCreateBrowserRouter: typeof createBrowserRouter =
Sentry.wrapCreateBrowserRouterV7(createBrowserRouter);
const router = sentryCreateBrowserRouter(
createRoutesFromElements(
<>
<Route path="login" element={<Login />} />
<Route
path="*"
element={
user ? (
// All other routes for logged in users
<Suspense fallback={<LoadingPage />}>
<UserRoutes />
</Suspense>
) : (
// All other routes when not logged in, just navigate back to login page
<Navigate to="/login" />
)
}
/>
</>,
),
);
Steps to Reproduce
- Configured React Router with Sentry Wrappers
- Lazy load routes
- Navigate to
"/"
(This initializes Sentry's route change subscriptions using routes available on initial page load) - Navigate to lazy loaded routes, e.g.
"/profile"
Expected Result
Navigation transaction span started using path "/profile"
Actual Result
Navigation transaction span started using path "/*"
This appears to be because the routes are only extracted once on mount:
sentry-javascript/packages/react/src/reactrouterv6-compat-utils.tsx
Lines 602 to 631 in 9f74bc9
And even though additional routes are lazy loaded, and the route name is correctly passed from the navigation event, when the name is normalized, it can't find the route, so falls back to the parent catch-all "/*"
sentry-javascript/packages/react/src/reactrouterv6-compat-utils.tsx
Lines 333 to 342 in 9f74bc9
Metadata
Assignees
Type
Projects
Status
No status