Skip to content

Commit 27cf07f

Browse files
fix(react): Compare location against basename-prefixed route. (#9076)
Co-authored-by: Abhijeet Prasad <aprasad@sentry.io>
1 parent 3ae909b commit 27cf07f

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

packages/react/src/reactrouterv6.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function getNormalizedName(
7878
routes: RouteObject[],
7979
location: Location,
8080
branches: RouteMatch[],
81+
basename: string = '',
8182
): [string, TransactionSource] {
8283
if (!routes || routes.length === 0) {
8384
return [location.pathname, 'url'];
@@ -99,7 +100,8 @@ function getNormalizedName(
99100
if (path) {
100101
const newPath = path[0] === '/' || pathBuilder[pathBuilder.length - 1] === '/' ? path : `/${path}`;
101102
pathBuilder += newPath;
102-
if (branch.pathname === location.pathname) {
103+
104+
if (basename + branch.pathname === location.pathname) {
103105
if (
104106
// If the route defined on the element is something like
105107
// <Route path="/stores/:storeId/products/:productId" element={<div>Product</div>} />
@@ -108,9 +110,9 @@ function getNormalizedName(
108110
// We should not count wildcard operators in the url segments calculation
109111
pathBuilder.slice(-2) !== '/*'
110112
) {
111-
return [newPath, 'route'];
113+
return [basename + newPath, 'route'];
112114
}
113-
return [pathBuilder, 'route'];
115+
return [basename + pathBuilder, 'route'];
114116
}
115117
}
116118
}
@@ -131,7 +133,7 @@ function updatePageloadTransaction(
131133
: (_matchRoutes(routes, location, basename) as unknown as RouteMatch[]);
132134

133135
if (activeTransaction && branches) {
134-
activeTransaction.setName(...getNormalizedName(routes, location, branches));
136+
activeTransaction.setName(...getNormalizedName(routes, location, branches, basename));
135137
}
136138
}
137139

@@ -149,7 +151,7 @@ function handleNavigation(
149151
activeTransaction.finish();
150152
}
151153

152-
const [name, source] = getNormalizedName(routes, location, branches);
154+
const [name, source] = getNormalizedName(routes, location, branches, basename);
153155
activeTransaction = _customStartTransaction({
154156
name,
155157
op: 'navigation',

packages/react/test/reactrouterv6.4.test.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,51 @@ describe('React Router v6.4', () => {
307307
op: 'navigation',
308308
origin: 'auto.navigation.react.reactrouterv6',
309309
tags: { 'routing.instrumentation': 'react-router-v6' },
310-
metadata: { source: 'url' },
310+
metadata: { source: 'route' },
311+
});
312+
});
313+
314+
it('works with parameterized paths and `basename`', () => {
315+
const [mockStartTransaction] = createInstrumentation();
316+
const sentryCreateBrowserRouter = wrapCreateBrowserRouter(createMemoryRouter as CreateRouterFunction);
317+
318+
const router = sentryCreateBrowserRouter(
319+
[
320+
{
321+
path: '/',
322+
element: <Navigate to="/some-org-id/users/some-user-id" />,
323+
},
324+
{
325+
path: ':orgId',
326+
children: [
327+
{
328+
path: 'users',
329+
children: [
330+
{
331+
path: ':userId',
332+
element: <div>User</div>,
333+
},
334+
],
335+
},
336+
],
337+
},
338+
],
339+
{
340+
initialEntries: ['/admin'],
341+
basename: '/admin',
342+
},
343+
);
344+
345+
// @ts-expect-error router is fine
346+
render(<RouterProvider router={router} />);
347+
348+
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
349+
expect(mockStartTransaction).toHaveBeenLastCalledWith({
350+
name: '/admin/:orgId/users/:userId',
351+
op: 'navigation',
352+
origin: 'auto.navigation.react.reactrouterv6',
353+
tags: { 'routing.instrumentation': 'react-router-v6' },
354+
metadata: { source: 'route' },
311355
});
312356
});
313357
});

0 commit comments

Comments
 (0)