Skip to content

Commit cffe480

Browse files
committed
Optimize
1 parent 930cb8e commit cffe480

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ export function createV6CompatibleWrapUseRoutes(origUseRoutes: UseRoutes, versio
200200
const normalizedLocation =
201201
typeof stableLocationParam === 'string' ? { pathname: stableLocationParam } : stableLocationParam;
202202

203-
routes.forEach(route => {
204-
allRoutes.push(...getChildRoutesRecursively(route));
205-
});
206-
207203
if (isMountRenderPass.current) {
204+
routes.forEach(route => {
205+
allRoutes.push(...getChildRoutesRecursively(route));
206+
});
207+
208208
updatePageloadTransaction(getActiveRootSpan(), normalizedLocation, routes, undefined, undefined, allRoutes);
209209
isMountRenderPass.current = false;
210210
} else {
@@ -238,16 +238,21 @@ export function handleNavigation(
238238
}
239239

240240
if ((navigationType === 'PUSH' || navigationType === 'POP') && branches) {
241-
const [name, source] = getNormalizedName(routes, location, branches, basename);
241+
let name,
242+
source: TransactionSource = 'url';
243+
const isInDescendantRoute = locationIsInsideDescendantRoute(location, allRoutes || routes);
242244

243-
let txnName = name;
245+
if (isInDescendantRoute) {
246+
name = prefixWithSlash(rebuildRoutePathFromAllRoutes(allRoutes || routes, location));
247+
source = 'route';
248+
}
244249

245-
if (locationIsInsideDescendantRoute(location, allRoutes || routes)) {
246-
txnName = prefixWithSlash(rebuildRoutePathFromAllRoutes(allRoutes || routes, location));
250+
if (!isInDescendantRoute || !name) {
251+
[name, source] = getNormalizedName(routes, location, branches, basename);
247252
}
248253

249254
startBrowserTracingNavigationSpan(client, {
250-
name: txnName,
255+
name,
251256
attributes: {
252257
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
253258
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
@@ -307,10 +312,6 @@ function pathIsWildcardAndHasChildren(path: string, branch: RouteMatch<string>):
307312
return (pathEndsWithWildcard(path) && branch.route.children && branch.route.children.length > 0) || false;
308313
}
309314

310-
// function pathIsWildcardWithNoChildren(path: string, branch: RouteMatch<string>): boolean {
311-
// return (pathEndsWithWildcard(path) && (!branch.route.children || branch.route.children.length === 0)) || false;
312-
// }
313-
314315
function routeIsDescendant(route: RouteObject): boolean {
315316
return !!(!route.children && route.element && route.path && route.path.endsWith('/*'));
316317
}
@@ -464,18 +465,23 @@ function updatePageloadTransaction(
464465
: (_matchRoutes(routes, location, basename) as unknown as RouteMatch[]);
465466

466467
if (branches) {
467-
const [name, source] = getNormalizedName(routes, location, branches, basename);
468+
let name,
469+
source: TransactionSource = 'url';
470+
const isInDescendantRoute = locationIsInsideDescendantRoute(location, allRoutes || routes);
468471

469-
let txnName = name;
472+
if (isInDescendantRoute) {
473+
name = prefixWithSlash(rebuildRoutePathFromAllRoutes(allRoutes || routes, location));
474+
source = 'route';
475+
}
470476

471-
if (locationIsInsideDescendantRoute(location, allRoutes || routes)) {
472-
txnName = prefixWithSlash(rebuildRoutePathFromAllRoutes(allRoutes || routes, location));
477+
if (!isInDescendantRoute || !name) {
478+
[name, source] = getNormalizedName(routes, location, branches, basename);
473479
}
474480

475-
getCurrentScope().setTransactionName(txnName);
481+
getCurrentScope().setTransactionName(name);
476482

477483
if (activeRootSpan) {
478-
activeRootSpan.updateName(txnName);
484+
activeRootSpan.updateName(name);
479485
activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
480486
}
481487
}
@@ -507,11 +513,11 @@ export function createV6CompatibleWithSentryReactRouterRouting<P extends Record<
507513
() => {
508514
const routes = _createRoutesFromChildren(props.children) as RouteObject[];
509515

510-
routes.forEach(route => {
511-
allRoutes.push(...getChildRoutesRecursively(route));
512-
});
513-
514516
if (isMountRenderPass.current) {
517+
routes.forEach(route => {
518+
allRoutes.push(...getChildRoutesRecursively(route));
519+
});
520+
515521
updatePageloadTransaction(getActiveRootSpan(), location, routes, undefined, undefined, allRoutes);
516522
isMountRenderPass.current = false;
517523
} else {

0 commit comments

Comments
 (0)