Skip to content

Commit 24e04e4

Browse files
committed
fix(react): Remove handleExistingNavigation
1 parent 9e70a5a commit 24e04e4

File tree

3 files changed

+14
-111
lines changed

3 files changed

+14
-111
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export {
99
createV6CompatibleWrapCreateMemoryRouter,
1010
createV6CompatibleWrapUseRoutes,
1111
handleNavigation,
12-
handleExistingNavigationSpan,
1312
createNewNavigationSpan,
1413
addResolvedRoutesToParent,
1514
processResolvedRoutes,

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

Lines changed: 14 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,7 @@ export function updateNavigationSpan(
176176
// Check if this span has already been named to avoid multiple updates
177177
// But allow updates if this is a forced update (e.g., when lazy routes are loaded)
178178
const hasBeenNamed =
179-
!forceUpdate &&
180-
(
181-
activeRootSpan as {
182-
__sentry_navigation_name_set__?: boolean;
183-
}
184-
)?.__sentry_navigation_name_set__;
179+
!forceUpdate && (activeRootSpan as { __sentry_navigation_name_set__?: boolean })?.__sentry_navigation_name_set__;
185180

186181
if (!hasBeenNamed) {
187182
// Get fresh branches for the current location with all loaded routes
@@ -199,14 +194,14 @@ export function updateNavigationSpan(
199194
if (name && !spanJson.timestamp) {
200195
activeRootSpan.updateName(name);
201196
activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
202-
203-
// Mark this span as having its name set to prevent future updates
204-
addNonEnumerableProperty(
205-
activeRootSpan as { __sentry_navigation_name_set__?: boolean },
206-
'__sentry_navigation_name_set__',
207-
true,
208-
);
209197
}
198+
199+
// Mark this span as having its name set to prevent future updates
200+
addNonEnumerableProperty(
201+
activeRootSpan as { __sentry_navigation_name_set__?: boolean },
202+
'__sentry_navigation_name_set__',
203+
true,
204+
);
210205
}
211206
}
212207

@@ -355,13 +350,7 @@ export function createV6CompatibleWrapCreateMemoryRouter<
355350
: router.state.location;
356351

357352
if (router.state.historyAction === 'POP' && activeRootSpan) {
358-
updatePageloadTransaction({
359-
activeRootSpan,
360-
location,
361-
routes,
362-
basename,
363-
allRoutes: Array.from(allRoutes),
364-
});
353+
updatePageloadTransaction({ activeRootSpan, location, routes, basename, allRoutes: Array.from(allRoutes) });
365354
}
366355

367356
router.subscribe((state: RouterState) => {
@@ -389,11 +378,7 @@ export function createReactRouterV6CompatibleTracingIntegration(
389378
options: Parameters<typeof browserTracingIntegration>[0] & ReactRouterOptions,
390379
version: V6CompatibleVersion,
391380
): Integration {
392-
const integration = browserTracingIntegration({
393-
...options,
394-
instrumentPageLoad: false,
395-
instrumentNavigation: false,
396-
});
381+
const integration = browserTracingIntegration({ ...options, instrumentPageLoad: false, instrumentNavigation: false });
397382

398383
const {
399384
useEffect,
@@ -532,13 +517,7 @@ function wrapPatchRoutesOnNavigation(
532517
if (activeRootSpan && (spanToJSON(activeRootSpan) as { op?: string }).op === 'navigation') {
533518
updateNavigationSpan(
534519
activeRootSpan,
535-
{
536-
pathname: targetPath,
537-
search: '',
538-
hash: '',
539-
state: null,
540-
key: 'default',
541-
},
520+
{ pathname: targetPath, search: '', hash: '', state: null, key: 'default' },
542521
Array.from(allRoutes),
543522
true, // forceUpdate = true since we're loading lazy routes
544523
_matchRoutes,
@@ -559,13 +538,7 @@ function wrapPatchRoutesOnNavigation(
559538
if (pathname) {
560539
updateNavigationSpan(
561540
activeRootSpan,
562-
{
563-
pathname,
564-
search: '',
565-
hash: '',
566-
state: null,
567-
key: 'default',
568-
},
541+
{ pathname, search: '', hash: '', state: null, key: 'default' },
569542
Array.from(allRoutes),
570543
false, // forceUpdate = false since this is after lazy routes are loaded
571544
_matchRoutes,
@@ -612,9 +585,7 @@ export function handleNavigation(opts: {
612585
const isAlreadyInNavigationSpan = spanJson?.op === 'navigation';
613586

614587
// Cross usage can result in multiple navigation spans being created without this check
615-
if (isAlreadyInNavigationSpan && activeSpan && spanJson) {
616-
handleExistingNavigationSpan(activeSpan, spanJson, name, source, isLazyRouteContext);
617-
} else {
588+
if (!isAlreadyInNavigationSpan) {
618589
createNewNavigationSpan(client, name, source, version, isLazyRouteContext);
619590
}
620591
}
@@ -726,13 +697,7 @@ export function createV6CompatibleWithSentryReactRouterRouting<P extends Record<
726697
});
727698
isMountRenderPass.current = false;
728699
} else {
729-
handleNavigation({
730-
location,
731-
routes,
732-
navigationType,
733-
version,
734-
allRoutes: Array.from(allRoutes),
735-
});
700+
handleNavigation({ location, routes, navigationType, version, allRoutes: Array.from(allRoutes) });
736701
}
737702
},
738703
// `props.children` is purposely not included in the dependency array, because we do not want to re-run this effect
@@ -766,43 +731,6 @@ function getActiveRootSpan(): Span | undefined {
766731
return op === 'navigation' || op === 'pageload' ? rootSpan : undefined;
767732
}
768733

769-
/**
770-
* Handles updating an existing navigation span
771-
*/
772-
export function handleExistingNavigationSpan(
773-
activeSpan: Span,
774-
spanJson: ReturnType<typeof spanToJSON>,
775-
name: string,
776-
source: TransactionSource,
777-
isLikelyLazyRoute: boolean,
778-
): void {
779-
// Check if we've already set the name for this span using a custom property
780-
const hasBeenNamed = (
781-
activeSpan as {
782-
__sentry_navigation_name_set__?: boolean;
783-
}
784-
)?.__sentry_navigation_name_set__;
785-
786-
if (!hasBeenNamed) {
787-
// This is the first time we're setting the name for this span
788-
if (!spanJson.timestamp) {
789-
activeSpan?.updateName(name);
790-
}
791-
792-
// For lazy routes, don't mark as named yet so it can be updated later
793-
if (!isLikelyLazyRoute) {
794-
addNonEnumerableProperty(
795-
activeSpan as { __sentry_navigation_name_set__?: boolean },
796-
'__sentry_navigation_name_set__',
797-
true,
798-
);
799-
}
800-
}
801-
802-
// Always set the source attribute to keep it consistent with the current route
803-
activeSpan?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
804-
}
805-
806734
/**
807735
* Creates a new navigation span
808736
*/

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
addResolvedRoutesToParent,
1111
createNewNavigationSpan,
1212
createReactRouterV6CompatibleTracingIntegration,
13-
handleExistingNavigationSpan,
1413
updateNavigationSpan,
1514
} from '../../src/reactrouter-compat-utils';
1615
import type { Location, RouteObject } from '../../src/types';
@@ -93,29 +92,6 @@ describe('reactrouter-compat-utils/instrumentation', () => {
9392
expect(mockUpdateName).not.toHaveBeenCalled();
9493
});
9594
});
96-
97-
describe('handleExistingNavigationSpan', () => {
98-
it('should update span name when not already named', () => {
99-
const spanJson = { op: 'navigation', data: {}, span_id: 'test', start_timestamp: 0, trace_id: 'test' };
100-
101-
handleExistingNavigationSpan(mockSpan, spanJson, 'Test Route', 'route', false);
102-
103-
expect(mockUpdateName).toHaveBeenCalledWith('Test Route');
104-
expect(mockSetAttribute).toHaveBeenCalledWith('sentry.source', 'route');
105-
expect(addNonEnumerableProperty).toHaveBeenCalledWith(mockSpan, '__sentry_navigation_name_set__', true);
106-
});
107-
108-
it('should not mark as named for lazy routes', () => {
109-
const spanJson = { op: 'navigation', data: {}, span_id: 'test', start_timestamp: 0, trace_id: 'test' };
110-
111-
handleExistingNavigationSpan(mockSpan, spanJson, 'Test Route', 'route', true);
112-
113-
expect(mockUpdateName).toHaveBeenCalledWith('Test Route');
114-
expect(mockSetAttribute).toHaveBeenCalledWith('sentry.source', 'route');
115-
expect(addNonEnumerableProperty).not.toHaveBeenCalled();
116-
});
117-
});
118-
11995
describe('createNewNavigationSpan', () => {
12096
it('should create new navigation span with correct attributes', () => {
12197
createNewNavigationSpan(mockClient, 'Test Route', 'route', '6', false);

0 commit comments

Comments
 (0)