|
8 | 8 | startBrowserTracingPageLoadSpan, |
9 | 9 | WINDOW, |
10 | 10 | } from '@sentry/browser'; |
11 | | -import type { Client, Integration, Span, TransactionSource } from '@sentry/core'; |
| 11 | +import type { Client, Integration, Span } from '@sentry/core'; |
12 | 12 | import { |
13 | 13 | addNonEnumerableProperty, |
14 | 14 | debug, |
@@ -41,14 +41,7 @@ import type { |
41 | 41 | UseRoutes, |
42 | 42 | } from '../types'; |
43 | 43 | 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'; |
52 | 45 |
|
53 | 46 | let _useEffect: UseEffect; |
54 | 47 | let _useLocation: UseLocation; |
@@ -668,14 +661,19 @@ export function handleNavigation(opts: { |
668 | 661 |
|
669 | 662 | // Cross usage can result in multiple navigation spans being created without this check |
670 | 663 | if (!isAlreadyInNavigationSpan) { |
671 | | - startBrowserTracingNavigationSpan(client, { |
| 664 | + const navigationSpan = startBrowserTracingNavigationSpan(client, { |
672 | 665 | name, |
673 | 666 | attributes: { |
674 | 667 | [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source, |
675 | 668 | [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
676 | 669 | [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.navigation.react.reactrouter_v${version}`, |
677 | 670 | }, |
678 | 671 | }); |
| 672 | + |
| 673 | + // Patch navigation span to handle early cancellation (e.g., document.hidden) |
| 674 | + if (navigationSpan) { |
| 675 | + patchNavigationSpanEnd(navigationSpan, location, routes, basename, allRoutes); |
| 676 | + } |
679 | 677 | } |
680 | 678 | } |
681 | 679 | } |
@@ -729,29 +727,104 @@ function updatePageloadTransaction({ |
729 | 727 | : (_matchRoutes(allRoutes || routes, location, basename) as unknown as RouteMatch[]); |
730 | 728 |
|
731 | 729 | if (branches) { |
732 | | - let name, |
733 | | - source: TransactionSource = 'url'; |
734 | | - |
735 | | - const isInDescendantRoute = locationIsInsideDescendantRoute(location, allRoutes || routes); |
736 | | - |
737 | | - if (isInDescendantRoute) { |
738 | | - name = prefixWithSlash(rebuildRoutePathFromAllRoutes(allRoutes || routes, location)); |
739 | | - source = 'route'; |
740 | | - } |
741 | | - |
742 | | - if (!isInDescendantRoute || !name) { |
743 | | - [name, source] = getNormalizedName(routes, location, branches, basename); |
744 | | - } |
| 730 | + const [name, source] = resolveRouteNameAndSource(location, routes, allRoutes || routes, branches, basename); |
745 | 731 |
|
746 | 732 | getCurrentScope().setTransactionName(name || '/'); |
747 | 733 |
|
748 | 734 | if (activeRootSpan) { |
749 | 735 | activeRootSpan.updateName(name); |
750 | 736 | activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source); |
| 737 | + |
| 738 | + // Patch span.end() to ensure we update the name one last time before the span is sent |
| 739 | + patchPageloadSpanEnd(activeRootSpan, location, routes, basename, allRoutes); |
751 | 740 | } |
752 | 741 | } |
753 | 742 | } |
754 | 743 |
|
| 744 | +/** |
| 745 | + * Patches the span.end() method to update the transaction name one last time before the span is sent. |
| 746 | + * This handles cases where the span is cancelled early (e.g., document.hidden) before lazy routes have finished loading. |
| 747 | + */ |
| 748 | +function patchSpanEnd( |
| 749 | + span: Span, |
| 750 | + location: Location, |
| 751 | + routes: RouteObject[], |
| 752 | + basename: string | undefined, |
| 753 | + _allRoutes: RouteObject[] | undefined, |
| 754 | + spanType: 'pageload' | 'navigation', |
| 755 | +): void { |
| 756 | + const patchedPropertyName = `__sentry_${spanType}_end_patched__` as const; |
| 757 | + const hasEndBeenPatched = (span as unknown as Record<string, boolean | undefined>)?.[patchedPropertyName]; |
| 758 | + |
| 759 | + if (hasEndBeenPatched || !span.end) { |
| 760 | + return; |
| 761 | + } |
| 762 | + |
| 763 | + const originalEnd = span.end.bind(span); |
| 764 | + |
| 765 | + span.end = function patchedEnd(...args) { |
| 766 | + try { |
| 767 | + // Only update if the span source is not already 'route' (i.e., it hasn't been parameterized yet) |
| 768 | + const spanJson = spanToJSON(span); |
| 769 | + const currentSource = spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]; |
| 770 | + if (currentSource !== 'route') { |
| 771 | + // Last chance to update the transaction name with the latest route info |
| 772 | + // Use the live global allRoutes Set to include any lazy routes loaded after patching |
| 773 | + const currentAllRoutes = Array.from(allRoutes); |
| 774 | + const branches = _matchRoutes( |
| 775 | + currentAllRoutes.length > 0 ? currentAllRoutes : routes, |
| 776 | + location, |
| 777 | + basename, |
| 778 | + ) as unknown as RouteMatch[]; |
| 779 | + |
| 780 | + if (branches) { |
| 781 | + const [name, source] = resolveRouteNameAndSource( |
| 782 | + location, |
| 783 | + routes, |
| 784 | + currentAllRoutes.length > 0 ? currentAllRoutes : routes, |
| 785 | + branches, |
| 786 | + basename, |
| 787 | + ); |
| 788 | + |
| 789 | + // Only update if we have a valid name |
| 790 | + if (name && (spanType === 'pageload' || !spanJson.timestamp)) { |
| 791 | + span.updateName(name); |
| 792 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source); |
| 793 | + } |
| 794 | + } |
| 795 | + } |
| 796 | + } catch (error) { |
| 797 | + // Silently catch errors to ensure span.end() is always called |
| 798 | + DEBUG_BUILD && debug.warn(`Error updating span details before ending: ${error}`); |
| 799 | + } |
| 800 | + |
| 801 | + return originalEnd(...args); |
| 802 | + }; |
| 803 | + |
| 804 | + // Mark this span as having its end() method patched to prevent duplicate patching |
| 805 | + addNonEnumerableProperty(span as unknown as Record<string, boolean>, patchedPropertyName, true); |
| 806 | +} |
| 807 | + |
| 808 | +function patchPageloadSpanEnd( |
| 809 | + span: Span, |
| 810 | + location: Location, |
| 811 | + routes: RouteObject[], |
| 812 | + basename: string | undefined, |
| 813 | + _allRoutes: RouteObject[] | undefined, |
| 814 | +): void { |
| 815 | + patchSpanEnd(span, location, routes, basename, _allRoutes, 'pageload'); |
| 816 | +} |
| 817 | + |
| 818 | +function patchNavigationSpanEnd( |
| 819 | + span: Span, |
| 820 | + location: Location, |
| 821 | + routes: RouteObject[], |
| 822 | + basename: string | undefined, |
| 823 | + _allRoutes: RouteObject[] | undefined, |
| 824 | +): void { |
| 825 | + patchSpanEnd(span, location, routes, basename, _allRoutes, 'navigation'); |
| 826 | +} |
| 827 | + |
755 | 828 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
756 | 829 | export function createV6CompatibleWithSentryReactRouterRouting<P extends Record<string, any>, R extends React.FC<P>>( |
757 | 830 | Routes: R, |
|
0 commit comments