Skip to content

Commit 81c1bc5

Browse files
committed
Prevent duplicate children in addResolvedRoutesToParent
1 parent 06e535e commit 81c1bc5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

dev-packages/e2e-tests/test-applications/react-router-7-lazy-routes/tests/transactions.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { waitForTransaction } from '@sentry-internal/test-utils';
44

55
test('Creates a pageload transaction with parameterized route', async ({ page }) => {
66
const transactionPromise = waitForTransaction('react-router-7-lazy-routes', async transactionEvent => {
7-
console.debug('transactionEvent', transactionEvent);
8-
97
return (
108
!!transactionEvent?.transaction &&
119
transactionEvent.contexts?.trace?.op === 'pageload' &&

packages/react/src/reactrouterv6-compat-utils.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,22 @@ const allRoutes = new Set<RouteObject>();
6666

6767
/**
6868
* Adds resolved routes as children to the parent route.
69+
* Prevents duplicate routes by checking if they already exist.
6970
*/
7071
function addResolvedRoutesToParent(resolvedRoutes: RouteObject[], parentRoute: RouteObject): void {
71-
parentRoute.children = Array.isArray(parentRoute.children)
72-
? [...parentRoute.children, ...resolvedRoutes]
73-
: resolvedRoutes;
72+
const existingChildren = parentRoute.children || [];
73+
74+
const newRoutes = resolvedRoutes.filter(newRoute =>
75+
!existingChildren.some(existing =>
76+
existing === newRoute ||
77+
(newRoute.path && existing.path === newRoute.path) ||
78+
(newRoute.id && existing.id === newRoute.id)
79+
)
80+
);
81+
82+
if (newRoutes.length > 0) {
83+
parentRoute.children = [...existingChildren, ...newRoutes];
84+
}
7485
}
7586

7687
/**

0 commit comments

Comments
 (0)