Skip to content

Commit 11d1a6a

Browse files
committed
Address review comments from bots
1 parent 121bb5e commit 11d1a6a

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

packages/react/src/reactrouter-compat-utils/instrumentation.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ const CLIENTS_WITH_INSTRUMENT_NAVIGATION = new WeakSet<Client>();
5454

5555
/**
5656
* Tracks last navigation per client to prevent duplicate spans in cross-usage scenarios.
57-
* Uses 100ms window to deduplicate when multiple wrappers handle the same navigation.
57+
* Entry persists until next different navigation, handling delayed wrapper execution.
5858
*/
59-
const LAST_NAVIGATION_PER_CLIENT = new WeakMap<Client, { key: string; timestamp: number }>();
59+
const LAST_NAVIGATION_PER_CLIENT = new WeakMap<Client, string>();
6060

6161
export function addResolvedRoutesToParent(resolvedRoutes: RouteObject[], parentRoute: RouteObject): void {
6262
const existingChildren = parentRoute.children || [];
@@ -630,9 +630,8 @@ function tryUpdateSpanName(
630630
}
631631

632632
function isDuplicateNavigation(client: Client, navigationKey: string): boolean {
633-
const lastNavigation = LAST_NAVIGATION_PER_CLIENT.get(client);
634-
const now = Date.now();
635-
return !!(lastNavigation && lastNavigation.key === navigationKey && now - lastNavigation.timestamp < 100);
633+
const lastKey = LAST_NAVIGATION_PER_CLIENT.get(client);
634+
return lastKey === navigationKey;
636635
}
637636

638637
function createNavigationSpan(opts: {
@@ -648,11 +647,6 @@ function createNavigationSpan(opts: {
648647
}): Span | undefined {
649648
const { client, name, source, version, location, routes, basename, allRoutes, navigationKey } = opts;
650649

651-
LAST_NAVIGATION_PER_CLIENT.set(client, {
652-
key: navigationKey,
653-
timestamp: Date.now(),
654-
});
655-
656650
const navigationSpan = startBrowserTracingNavigationSpan(client, {
657651
name,
658652
attributes: {
@@ -663,11 +657,17 @@ function createNavigationSpan(opts: {
663657
});
664658

665659
if (navigationSpan) {
660+
LAST_NAVIGATION_PER_CLIENT.set(client, navigationKey);
666661
patchNavigationSpanEnd(navigationSpan, location, routes, basename, allRoutes);
667662

668-
client.on('spanEnd', endedSpan => {
663+
const unsubscribe = client.on('spanEnd', endedSpan => {
669664
if (endedSpan === navigationSpan) {
670-
LAST_NAVIGATION_PER_CLIENT.delete(client);
665+
// Clear key only if it's still our key (handles overlapping navigations)
666+
const lastKey = LAST_NAVIGATION_PER_CLIENT.get(client);
667+
if (lastKey === navigationKey) {
668+
LAST_NAVIGATION_PER_CLIENT.delete(client);
669+
}
670+
unsubscribe(); // Prevent memory leak
671671
}
672672
});
673673
}

0 commit comments

Comments
 (0)