Skip to content

Commit f55018f

Browse files
authored
fix(react): Handle nested parameterized routes in reactrouterv3 transaction normalization (#16274)
We noticed that routes like `/teams/:teamId/details` were normalized to `/teams:teamIddetails` which is missing `/` and is incorrect. This PR updates the tests for the React Router v3 integration to ensure correct transaction name normalization for nested parameterized routes. Specifically, it modifies the `normalizes transaction name` test case to include navigation to a route like `/teams/:teamId/details` and verifies that the resulting transaction name is correctly normalized. This addresses an issue where similar nested route patterns might not be handled correctly.
1 parent 6d2b971 commit f55018f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

packages/react/src/reactrouterv3.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ function getRouteStringFromRoutes(routes: Route[]): string {
150150
}
151151
}
152152

153-
return routesWithPaths
154-
.slice(index)
155-
.filter(({ path }) => !!path)
156-
.map(({ path }) => path)
157-
.join('');
153+
return routesWithPaths.slice(index).reduce((acc, { path }) => {
154+
const pathSegment = acc === '/' || acc === '' ? path : `/${path}`;
155+
return `${acc}${pathSegment}`;
156+
}, '');
158157
}

packages/react/test/reactrouterv3.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ describe('browserTracingReactRouterV3', () => {
6464
<Route path=":orgid" component={() => <div>OrgId</div>} />
6565
<Route path=":orgid/v1/:teamid" component={() => <div>Team</div>} />
6666
</Route>
67+
<Route path="teams">
68+
<Route path=":teamId">
69+
<Route path="details" component={() => <div>Team Details</div>} />
70+
</Route>
71+
</Route>
6772
</Route>
6873
);
6974
const history = createMemoryHistory();
@@ -192,6 +197,22 @@ describe('browserTracingReactRouterV3', () => {
192197
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
193198
},
194199
});
200+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/users/:userid');
201+
202+
act(() => {
203+
history.push('/teams/456/details');
204+
});
205+
206+
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2);
207+
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
208+
name: '/teams/:teamId/details',
209+
attributes: {
210+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
211+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v3',
212+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
213+
},
214+
});
215+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/teams/:teamId/details');
195216
});
196217

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

0 commit comments

Comments
 (0)