Skip to content

fix(react): Handle nested parameterized routes in reactrouterv3 transaction normalization #16274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/react/src/reactrouterv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ function getRouteStringFromRoutes(routes: Route[]): string {
}
}

return routesWithPaths
const pathParts = routesWithPaths
.slice(index)
.filter(({ path }) => !!path)
.map(({ path }) => path)
.join('');
.map(({ path }) => path);

// Join all parts with '/', then replace multiple slashes with a single one.
let fullPath = pathParts.join('/');
fullPath = fullPath.replace(/\/+/g, '/');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check if reduce is faster than the join + replace here.

it will also likely be more bundle size efficient, and we can account for the edge case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought about this more - I think this works. We can replace both const pathParts and the fullPath calculations.

return routesWithPaths.slice(index).reduce((acc, { path }) => {
  const pathSegment = acc === '/' || acc === '' ? path : `/${path}`;
  return `${acc}${pathSegment}`;
}, '');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce is 2.51% slower: https://jsbench.me/g2mamtp9bd/1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my M1 Macbook it does 102K ops/s, I don't think it matters that much. Added a commit here: c1144e2


// Edge case: If the path started with multiple slashes and routes,
// like `//foo`, it might become `/foo`. This is generally acceptable.

return fullPath;
}
21 changes: 21 additions & 0 deletions packages/react/test/reactrouterv3.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('browserTracingReactRouterV3', () => {
<Route path=":orgid" component={() => <div>OrgId</div>} />
<Route path=":orgid/v1/:teamid" component={() => <div>Team</div>} />
</Route>
<Route path="teams">
<Route path=":teamId">
<Route path="details" component={() => <div>Team Details</div>} />
</Route>
</Route>
</Route>
);
const history = createMemoryHistory();
Expand Down Expand Up @@ -192,6 +197,22 @@ describe('browserTracingReactRouterV3', () => {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
},
});
expect(getCurrentScope().getScopeData().transactionName).toEqual('/users/:userid');

act(() => {
history.push('/teams/456/details');
});

expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2);
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
name: '/teams/:teamId/details',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v3',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
},
});
expect(getCurrentScope().getScopeData().transactionName).toEqual('/teams/:teamId/details');
});

it("updates the scope's `transactionName` on a navigation", () => {
Expand Down
Loading