Skip to content

Commit 8507984

Browse files
committed
Introduce isLikeLazyRouteContext
1 parent 453f5c6 commit 8507984

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

packages/react/src/reactrouter-compat-utils/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ export {
1616

1717
// Utility exports
1818
export {
19+
resolveRouteNameAndSource,
20+
getNormalizedName,
1921
initializeRouterUtils,
22+
isLikelyLazyRouteContext,
23+
locationIsInsideDescendantRoute,
2024
prefixWithSlash,
2125
rebuildRoutePathFromAllRoutes,
22-
locationIsInsideDescendantRoute,
23-
getNormalizedName,
24-
getNumberOfUrlSegments,
25-
resolveRouteNameAndSource,
2626
pathEndsWithWildcard,
2727
pathIsWildcardAndHasChildren,
28+
getNumberOfUrlSegments,
2829
} from './utils';
2930

3031
// Lazy route exports

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { checkRouteForAsyncHandler } from './lazy-routes';
4444
import {
4545
getNormalizedName,
4646
initializeRouterUtils,
47+
isLikelyLazyRouteContext,
4748
locationIsInsideDescendantRoute,
4849
prefixWithSlash,
4950
rebuildRoutePathFromAllRoutes,
@@ -538,7 +539,7 @@ function wrapPatchRoutesOnNavigation(
538539
key: 'default',
539540
},
540541
Array.from(allRoutes),
541-
true,
542+
true, // forceUpdate = true since we're loading lazy routes
542543
_matchRoutes,
543544
);
544545
}
@@ -566,7 +567,7 @@ function wrapPatchRoutesOnNavigation(
566567
key: 'default',
567568
},
568569
Array.from(allRoutes),
569-
false,
570+
false, // forceUpdate = false since this is after lazy routes are loaded
570571
_matchRoutes,
571572
);
572573
}
@@ -603,15 +604,18 @@ export function handleNavigation(opts: {
603604
basename,
604605
);
605606

607+
// Check if this might be a lazy route context
608+
const isLazyRouteContext = isLikelyLazyRouteContext(allRoutes || routes, location);
609+
606610
const activeSpan = getActiveSpan();
607611
const spanJson = activeSpan && spanToJSON(activeSpan);
608612
const isAlreadyInNavigationSpan = spanJson?.op === 'navigation';
609613

610614
// Cross usage can result in multiple navigation spans being created without this check
611615
if (isAlreadyInNavigationSpan && activeSpan && spanJson) {
612-
handleExistingNavigationSpan(activeSpan, spanJson, name, source, false);
616+
handleExistingNavigationSpan(activeSpan, spanJson, name, source, isLazyRouteContext);
613617
} else {
614-
createNewNavigationSpan(client, name, source, version, false);
618+
createNewNavigationSpan(client, name, source, version, isLazyRouteContext);
615619
}
616620
}
617621
}
@@ -783,6 +787,7 @@ export function handleExistingNavigationSpan(
783787
}
784788
}
785789

790+
// Always set the source attribute to keep it consistent with the current route
786791
activeSpan?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
787792
}
788793

packages/react/src/reactrouter-compat-utils/utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ export function initializeRouterUtils(matchRoutes: MatchRoutes, stripBasename: b
1414
_stripBasename = stripBasename;
1515
}
1616

17+
/**
18+
* Checks if the given routes or location context suggests this might be a lazy route scenario.
19+
* This helps determine if we should delay marking navigation spans as "named" to allow for updates
20+
* when lazy routes are loaded.
21+
*/
22+
export function isLikelyLazyRouteContext(routes: RouteObject[], location: Location): boolean {
23+
// Check if any route in the current match has lazy properties
24+
const hasLazyRoute = routes.some(route => {
25+
return (
26+
// React Router lazy() route
27+
route.lazy ||
28+
// Route with async handlers that might load child routes
29+
(route.handle &&
30+
typeof route.handle === 'object' &&
31+
Object.values(route.handle).some(handler => typeof handler === 'function'))
32+
);
33+
});
34+
35+
if (hasLazyRoute) {
36+
return true;
37+
}
38+
39+
// Check if current route is unmatched, which might indicate a lazy route that hasn't loaded yet
40+
const currentMatches = _matchRoutes(routes, location);
41+
if (!currentMatches || currentMatches.length === 0) {
42+
return true;
43+
}
44+
45+
return false;
46+
}
47+
1748
// Helper functions
1849
function pickPath(match: RouteMatch): string {
1950
return trimWildcard(match.route.path || '');

0 commit comments

Comments
 (0)