Skip to content

Commit e7270bc

Browse files
committed
Deduplicate transaction name extractors
1 parent 657256e commit e7270bc

File tree

1 file changed

+4
-41
lines changed

1 file changed

+4
-41
lines changed

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

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
startBrowserTracingPageLoadSpan,
99
WINDOW,
1010
} from '@sentry/browser';
11-
import type { Client, Integration, Span, TransactionSource } from '@sentry/core';
11+
import type { Client, Integration, Span } from '@sentry/core';
1212
import {
1313
addNonEnumerableProperty,
1414
debug,
@@ -41,14 +41,7 @@ import type {
4141
UseRoutes,
4242
} from '../types';
4343
import { checkRouteForAsyncHandler } from './lazy-routes';
44-
import {
45-
getNormalizedName,
46-
initializeRouterUtils,
47-
locationIsInsideDescendantRoute,
48-
prefixWithSlash,
49-
rebuildRoutePathFromAllRoutes,
50-
resolveRouteNameAndSource,
51-
} from './utils';
44+
import { initializeRouterUtils, resolveRouteNameAndSource } from './utils';
5245

5346
let _useEffect: UseEffect;
5447
let _useLocation: UseLocation;
@@ -732,7 +725,7 @@ function updatePageloadTransaction({
732725
: (_matchRoutes(allRoutes || routes, location, basename) as unknown as RouteMatch[]);
733726

734727
if (branches) {
735-
const [name, source] = getTransactionNameAndSource(location, routes, branches, basename, allRoutes);
728+
const [name, source] = resolveRouteNameAndSource(location, routes, allRoutes || routes, branches, basename);
736729

737730
getCurrentScope().setTransactionName(name || '/');
738731

@@ -746,33 +739,6 @@ function updatePageloadTransaction({
746739
}
747740
}
748741

749-
/**
750-
* Extracts the transaction name and source from the route information.
751-
*/
752-
function getTransactionNameAndSource(
753-
location: Location,
754-
routes: RouteObject[],
755-
branches: RouteMatch[],
756-
basename: string | undefined,
757-
allRoutes: RouteObject[] | undefined,
758-
): [string, TransactionSource] {
759-
let name: string | undefined;
760-
let source: TransactionSource = 'url';
761-
762-
const isInDescendantRoute = locationIsInsideDescendantRoute(location, allRoutes || routes);
763-
764-
if (isInDescendantRoute) {
765-
name = prefixWithSlash(rebuildRoutePathFromAllRoutes(allRoutes || routes, location));
766-
source = 'route';
767-
}
768-
769-
if (!isInDescendantRoute || !name) {
770-
[name, source] = getNormalizedName(routes, location, branches, basename);
771-
}
772-
773-
return [name || '/', source];
774-
}
775-
776742
/**
777743
* Patches the span.end() method to update the transaction name one last time before the span is sent.
778744
* This handles cases where the span is cancelled early (e.g., document.hidden) before lazy routes have finished loading.
@@ -805,10 +771,7 @@ function patchSpanEnd(
805771
const branches = _matchRoutes(currentAllRoutes || routes, location, basename) as unknown as RouteMatch[];
806772

807773
if (branches) {
808-
const [name, source] =
809-
spanType === 'pageload'
810-
? getTransactionNameAndSource(location, routes, branches, basename, currentAllRoutes)
811-
: resolveRouteNameAndSource(location, routes, currentAllRoutes, branches, basename);
774+
const [name, source] = resolveRouteNameAndSource(location, routes, currentAllRoutes, branches, basename);
812775

813776
// Only update if we have a valid name
814777
if (name && (spanType === 'pageload' || !spanJson.timestamp)) {

0 commit comments

Comments
 (0)