Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions packages/react/src/reactrouterv6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function getNormalizedName(
routes: RouteObject[],
location: Location,
branches: RouteMatch[],
basename: string = '',
): [string, TransactionSource] {
if (!routes || routes.length === 0) {
return [location.pathname, 'url'];
Expand All @@ -99,7 +100,8 @@ function getNormalizedName(
if (path) {
const newPath = path[0] === '/' || pathBuilder[pathBuilder.length - 1] === '/' ? path : `/${path}`;
pathBuilder += newPath;
if (branch.pathname === location.pathname) {

if (basename + branch.pathname === location.pathname) {
if (
// If the route defined on the element is something like
// <Route path="/stores/:storeId/products/:productId" element={<div>Product</div>} />
Expand All @@ -108,9 +110,9 @@ function getNormalizedName(
// We should not count wildcard operators in the url segments calculation
pathBuilder.slice(-2) !== '/*'
) {
return [newPath, 'route'];
return [basename + newPath, 'route'];
}
return [pathBuilder, 'route'];
return [basename + pathBuilder, 'route'];
}
}
}
Expand All @@ -131,7 +133,7 @@ function updatePageloadTransaction(
: (_matchRoutes(routes, location, basename) as unknown as RouteMatch[]);

if (activeTransaction && branches) {
activeTransaction.setName(...getNormalizedName(routes, location, branches));
activeTransaction.setName(...getNormalizedName(routes, location, branches, basename));
}
}

Expand All @@ -149,7 +151,7 @@ function handleNavigation(
activeTransaction.finish();
}

const [name, source] = getNormalizedName(routes, location, branches);
const [name, source] = getNormalizedName(routes, location, branches, basename);
activeTransaction = _customStartTransaction({
name,
op: 'navigation',
Expand Down
46 changes: 45 additions & 1 deletion packages/react/test/reactrouterv6.4.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,51 @@ describe('React Router v6.4', () => {
op: 'navigation',
origin: 'auto.navigation.react.reactrouterv6',
tags: { 'routing.instrumentation': 'react-router-v6' },
metadata: { source: 'url' },
metadata: { source: 'route' },
});
});

it('works with parameterized paths and `basename`', () => {
const [mockStartTransaction] = createInstrumentation();
const sentryCreateBrowserRouter = wrapCreateBrowserRouter(createMemoryRouter as CreateRouterFunction);

const router = sentryCreateBrowserRouter(
[
{
path: '/',
element: <Navigate to="/some-org-id/users/some-user-id" />,
},
{
path: ':orgId',
children: [
{
path: 'users',
children: [
{
path: ':userId',
element: <div>User</div>,
},
],
},
],
},
],
{
initialEntries: ['/admin'],
basename: '/admin',
},
);

// @ts-expect-error router is fine
render(<RouterProvider router={router} />);

expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/admin/:orgId/users/:userId',
op: 'navigation',
origin: 'auto.navigation.react.reactrouterv6',
tags: { 'routing.instrumentation': 'react-router-v6' },
metadata: { source: 'route' },
});
});
});
Expand Down