Skip to content

Commit fb42933

Browse files
committed
Clean up
1 parent 57c8b22 commit fb42933

File tree

1 file changed

+32
-78
lines changed

1 file changed

+32
-78
lines changed

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

Lines changed: 32 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -149,35 +149,23 @@ function processResolvedRoutes(
149149
);
150150
} else if (spanOp === 'navigation') {
151151
// For navigation spans, update the name with the newly loaded routes
152-
updateNavigationSpanWithLazyRoutesLocal(activeRootSpan, location, Array.from(allRoutes));
152+
updateNavigationSpanWithLazyRoutes(
153+
activeRootSpan,
154+
location,
155+
Array.from(allRoutes),
156+
false,
157+
_matchRoutes,
158+
rebuildRoutePathFromAllRoutes,
159+
locationIsInsideDescendantRoute,
160+
getNormalizedName,
161+
prefixWithSlash,
162+
);
153163
}
154164
}
155165
}
156166
}
157167

158-
/**
159-
* Local wrapper for updateNavigationSpanWithLazyRoutes that provides dependencies.
160-
*/
161-
function updateNavigationSpanWithLazyRoutesLocal(
162-
activeRootSpan: Span,
163-
location: Location,
164-
allRoutes: RouteObject[],
165-
forceUpdate = false,
166-
): void {
167-
updateNavigationSpanWithLazyRoutes(
168-
activeRootSpan,
169-
location,
170-
allRoutes,
171-
forceUpdate,
172-
_matchRoutes,
173-
rebuildRoutePathFromAllRoutes,
174-
locationIsInsideDescendantRoute,
175-
getNormalizedName,
176-
prefixWithSlash,
177-
);
178-
}
179-
180-
function wrapPatchRoutesOnNavigationLocal(
168+
function wrapPatchRoutesOnNavigation(
181169
opts: Record<string, unknown> | undefined,
182170
isMemoryRouter = false,
183171
): Record<string, unknown> {
@@ -202,7 +190,7 @@ function wrapPatchRoutesOnNavigationLocal(
202190
addRoutesToAllRoutes(children);
203191
const activeRootSpan = getActiveRootSpan();
204192
if (activeRootSpan && (spanToJSON(activeRootSpan) as { op?: string }).op === 'navigation') {
205-
updateNavigationSpanWithLazyRoutesLocal(
193+
updateNavigationSpanWithLazyRoutes(
206194
activeRootSpan,
207195
{
208196
pathname: targetPath,
@@ -213,6 +201,11 @@ function wrapPatchRoutesOnNavigationLocal(
213201
},
214202
Array.from(allRoutes),
215203
true,
204+
_matchRoutes,
205+
rebuildRoutePathFromAllRoutes,
206+
locationIsInsideDescendantRoute,
207+
getNormalizedName,
208+
prefixWithSlash,
216209
);
217210
}
218211
return originalPatch(routeId, children);
@@ -229,7 +222,7 @@ function wrapPatchRoutesOnNavigationLocal(
229222
// without accessing window.location, so we'll use targetPath for both cases
230223
const pathname = targetPath || (isMemoryRouter ? getGlobalPathname() : undefined);
231224
if (pathname) {
232-
updateNavigationSpanWithLazyRoutesLocal(
225+
updateNavigationSpanWithLazyRoutes(
233226
activeRootSpan,
234227
{
235228
pathname,
@@ -239,6 +232,12 @@ function wrapPatchRoutesOnNavigationLocal(
239232
key: 'default',
240233
},
241234
Array.from(allRoutes),
235+
false,
236+
_matchRoutes,
237+
rebuildRoutePathFromAllRoutes,
238+
locationIsInsideDescendantRoute,
239+
getNormalizedName,
240+
prefixWithSlash,
242241
);
243242
}
244243
}
@@ -278,7 +277,7 @@ export function createV6CompatibleWrapCreateBrowserRouter<
278277
}
279278

280279
// Wrap patchRoutesOnNavigation to detect when lazy routes are loaded
281-
const wrappedOpts = wrapPatchRoutesOnNavigationLocal(opts);
280+
const wrappedOpts = wrapPatchRoutesOnNavigation(opts);
282281

283282
const router = createRouterFunction(routes, wrappedOpts);
284283
const basename = opts?.basename;
@@ -367,7 +366,7 @@ export function createV6CompatibleWrapCreateMemoryRouter<
367366
}
368367

369368
// Wrap patchRoutesOnNavigation to detect when lazy routes are loaded
370-
const wrappedOpts = wrapPatchRoutesOnNavigationLocal(opts, true);
369+
const wrappedOpts = wrapPatchRoutesOnNavigation(opts, true);
371370

372371
const router = createRouterFunction(routes, wrappedOpts);
373372
const basename = opts?.basename;
@@ -556,11 +555,11 @@ export function handleNavigation(opts: {
556555
}
557556

558557
if ((navigationType === 'PUSH' || navigationType === 'POP') && branches) {
559-
const [name, source, isLikelyLazyRoute] = resolveRouteName(
558+
const [name, source] = resolveRouteNameAndSource(
560559
location,
561560
routes,
562561
allRoutes || routes,
563-
branches,
562+
branches as RouteMatch[],
564563
basename,
565564
locationIsInsideDescendantRoute,
566565
rebuildRoutePathFromAllRoutes,
@@ -574,9 +573,9 @@ export function handleNavigation(opts: {
574573

575574
// Cross usage can result in multiple navigation spans being created without this check
576575
if (isAlreadyInNavigationSpan && activeSpan && spanJson) {
577-
handleExistingNavigationSpan(activeSpan, spanJson, name, source, isLikelyLazyRoute);
576+
handleExistingNavigationSpan(activeSpan, spanJson, name, source, false);
578577
} else {
579-
createNewNavigationSpan(client, name, source, version, isLikelyLazyRoute);
578+
createNewNavigationSpan(client, name, source, version, false);
580579
}
581580
}
582581
}
@@ -784,7 +783,7 @@ function getNormalizedName(
784783

785784
const fallbackTransactionName = _stripBasename
786785
? stripBasenameFromPathname(location.pathname, basename)
787-
: location.pathname || '/';
786+
: location.pathname;
788787

789788
return [fallbackTransactionName, 'url'];
790789
}
@@ -931,51 +930,6 @@ export function resolveRouteNameAndSource(
931930
return [name || location.pathname, source];
932931
}
933932

934-
/**
935-
* Resolves the route name and source for navigation tracking
936-
*/
937-
export function resolveRouteName(
938-
location: Location,
939-
routes: RouteObject[],
940-
allRoutes: RouteObject[],
941-
branches: unknown[],
942-
basename: string | undefined,
943-
locationIsInsideDescendantRoute: (location: Location, routes: RouteObject[]) => boolean,
944-
rebuildRoutePathFromAllRoutes: (allRoutes: RouteObject[], location: Location) => string,
945-
getNormalizedName: (
946-
routes: RouteObject[],
947-
location: Location,
948-
branches: RouteMatch[],
949-
basename?: string,
950-
) => [string, TransactionSource],
951-
prefixWithSlash: (path: string) => string,
952-
): [string, TransactionSource, boolean] {
953-
const [name, source] = resolveRouteNameAndSource(
954-
location,
955-
routes,
956-
allRoutes || routes,
957-
branches as RouteMatch[],
958-
basename,
959-
locationIsInsideDescendantRoute,
960-
rebuildRoutePathFromAllRoutes,
961-
getNormalizedName,
962-
prefixWithSlash,
963-
);
964-
965-
// If we couldn't find a good route name, it might be because we're navigating to a lazy route
966-
// that hasn't been loaded yet. In this case, use the pathname as a fallback
967-
const isLikelyLazyRoute = source === 'url' && (branches as unknown[]).length === 0;
968-
let finalName = name;
969-
let finalSource = source;
970-
971-
if (isLikelyLazyRoute) {
972-
finalName = location.pathname;
973-
finalSource = 'url';
974-
}
975-
976-
return [finalName, finalSource, isLikelyLazyRoute];
977-
}
978-
979933
/**
980934
* Handles updating an existing navigation span
981935
*/

0 commit comments

Comments
 (0)