66 browserTracingIntegration ,
77 startBrowserTracingNavigationSpan ,
88 startBrowserTracingPageLoadSpan ,
9+ WINDOW ,
910} from '@sentry/browser' ;
1011import type { Client , Integration , Span , TransactionSource } from '@sentry/core' ;
1112import {
@@ -15,7 +16,6 @@ import {
1516 getClient ,
1617 getCurrentScope ,
1718 getRootSpan ,
18- GLOBAL_OBJ ,
1919 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
2020 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
2121 SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
@@ -25,6 +25,9 @@ import * as React from 'react';
2525import { DEBUG_BUILD } from './debug-build' ;
2626import { hoistNonReactStatics } from './hoist-non-react-statics' ;
2727import { checkRouteForAsyncHandler , updateNavigationSpanWithLazyRoutes } from './lazy-route-utils' ;
28+ import {
29+ initializeRouterUtils ,
30+ } from './reactrouterv6-utils' ;
2831import type {
2932 Action ,
3033 AgnosticDataRouteMatch ,
@@ -53,12 +56,12 @@ let _enableAsyncRouteHandlers: boolean = false;
5356const CLIENTS_WITH_INSTRUMENT_NAVIGATION = new WeakSet < Client > ( ) ;
5457
5558/**
56- * Gets the current location from the global object (window in browser environments) .
57- * Returns undefined if global location is not available.
59+ * Gets the current location from the window object in browser environments.
60+ * Returns undefined if window is not available.
5861 */
5962function getGlobalLocation ( ) : Location | undefined {
60- if ( typeof GLOBAL_OBJ !== 'undefined' ) {
61- const globalLocation = ( GLOBAL_OBJ as typeof GLOBAL_OBJ & Window ) . location ;
63+ if ( typeof WINDOW !== 'undefined' ) {
64+ const globalLocation = WINDOW . location ;
6265 if ( globalLocation ) {
6366 return { pathname : globalLocation . pathname } ;
6467 }
@@ -67,12 +70,12 @@ function getGlobalLocation(): Location | undefined {
6770}
6871
6972/**
70- * Gets the pathname from the global object (window in browser environments) .
71- * Returns undefined if global location is not available.
73+ * Gets the pathname from the window object in browser environments.
74+ * Returns undefined if window is not available.
7275 */
7376function getGlobalPathname ( ) : string | undefined {
74- if ( typeof GLOBAL_OBJ !== 'undefined' ) {
75- return ( GLOBAL_OBJ as typeof GLOBAL_OBJ & Window ) . location ?. pathname ;
77+ if ( typeof WINDOW !== 'undefined' ) {
78+ return WINDOW . location ?. pathname ;
7679 }
7780 return undefined ;
7881}
@@ -149,35 +152,19 @@ function processResolvedRoutes(
149152 ) ;
150153 } else if ( spanOp === 'navigation' ) {
151154 // For navigation spans, update the name with the newly loaded routes
152- updateNavigationSpanWithLazyRoutesLocal ( activeRootSpan , location , Array . from ( allRoutes ) ) ;
155+ updateNavigationSpanWithLazyRoutes (
156+ activeRootSpan ,
157+ location ,
158+ Array . from ( allRoutes ) ,
159+ false ,
160+ _matchRoutes ,
161+ ) ;
153162 }
154163 }
155164 }
156165}
157166
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 (
167+ function wrapPatchRoutesOnNavigation (
181168 opts : Record < string , unknown > | undefined ,
182169 isMemoryRouter = false ,
183170) : Record < string , unknown > {
@@ -202,7 +189,7 @@ function wrapPatchRoutesOnNavigationLocal(
202189 addRoutesToAllRoutes ( children ) ;
203190 const activeRootSpan = getActiveRootSpan ( ) ;
204191 if ( activeRootSpan && ( spanToJSON ( activeRootSpan ) as { op ?: string } ) . op === 'navigation' ) {
205- updateNavigationSpanWithLazyRoutesLocal (
192+ updateNavigationSpanWithLazyRoutes (
206193 activeRootSpan ,
207194 {
208195 pathname : targetPath ,
@@ -213,6 +200,7 @@ function wrapPatchRoutesOnNavigationLocal(
213200 } ,
214201 Array . from ( allRoutes ) ,
215202 true ,
203+ _matchRoutes ,
216204 ) ;
217205 }
218206 return originalPatch ( routeId , children ) ;
@@ -229,7 +217,7 @@ function wrapPatchRoutesOnNavigationLocal(
229217 // without accessing window.location, so we'll use targetPath for both cases
230218 const pathname = targetPath || ( isMemoryRouter ? getGlobalPathname ( ) : undefined ) ;
231219 if ( pathname ) {
232- updateNavigationSpanWithLazyRoutesLocal (
220+ updateNavigationSpanWithLazyRoutes (
233221 activeRootSpan ,
234222 {
235223 pathname,
@@ -239,6 +227,8 @@ function wrapPatchRoutesOnNavigationLocal(
239227 key : 'default' ,
240228 } ,
241229 Array . from ( allRoutes ) ,
230+ false ,
231+ _matchRoutes ,
242232 ) ;
243233 }
244234 }
@@ -278,7 +268,7 @@ export function createV6CompatibleWrapCreateBrowserRouter<
278268 }
279269
280270 // Wrap patchRoutesOnNavigation to detect when lazy routes are loaded
281- const wrappedOpts = wrapPatchRoutesOnNavigationLocal ( opts ) ;
271+ const wrappedOpts = wrapPatchRoutesOnNavigation ( opts ) ;
282272
283273 const router = createRouterFunction ( routes , wrappedOpts ) ;
284274 const basename = opts ?. basename ;
@@ -367,7 +357,7 @@ export function createV6CompatibleWrapCreateMemoryRouter<
367357 }
368358
369359 // Wrap patchRoutesOnNavigation to detect when lazy routes are loaded
370- const wrappedOpts = wrapPatchRoutesOnNavigationLocal ( opts , true ) ;
360+ const wrappedOpts = wrapPatchRoutesOnNavigation ( opts , true ) ;
371361
372362 const router = createRouterFunction ( routes , wrappedOpts ) ;
373363 const basename = opts ?. basename ;
@@ -452,6 +442,9 @@ export function createReactRouterV6CompatibleTracingIntegration(
452442 _createRoutesFromChildren = createRoutesFromChildren ;
453443 _stripBasename = stripBasename || false ;
454444 _enableAsyncRouteHandlers = enableAsyncRouteHandlers ;
445+
446+ // Initialize the router utils with the required dependencies
447+ initializeRouterUtils ( matchRoutes , stripBasename || false ) ;
455448 } ,
456449 afterAllSetup ( client ) {
457450 integration . afterAllSetup ( client ) ;
@@ -556,16 +549,12 @@ export function handleNavigation(opts: {
556549 }
557550
558551 if ( ( navigationType === 'PUSH' || navigationType === 'POP' ) && branches ) {
559- const [ name , source , isLikelyLazyRoute ] = resolveRouteName (
552+ const [ name , source ] = resolveRouteNameAndSource (
560553 location ,
561554 routes ,
562555 allRoutes || routes ,
563- branches ,
556+ branches as RouteMatch [ ] ,
564557 basename ,
565- locationIsInsideDescendantRoute ,
566- rebuildRoutePathFromAllRoutes ,
567- getNormalizedName ,
568- prefixWithSlash ,
569558 ) ;
570559
571560 const activeSpan = getActiveSpan ( ) ;
@@ -574,9 +563,9 @@ export function handleNavigation(opts: {
574563
575564 // Cross usage can result in multiple navigation spans being created without this check
576565 if ( isAlreadyInNavigationSpan && activeSpan && spanJson ) {
577- handleExistingNavigationSpan ( activeSpan , spanJson , name , source , isLikelyLazyRoute ) ;
566+ handleExistingNavigationSpan ( activeSpan , spanJson , name , source , false ) ;
578567 } else {
579- createNewNavigationSpan ( client , name , source , version , isLikelyLazyRoute ) ;
568+ createNewNavigationSpan ( client , name , source , version , false ) ;
580569 }
581570 }
582571}
@@ -784,7 +773,7 @@ function getNormalizedName(
784773
785774 const fallbackTransactionName = _stripBasename
786775 ? stripBasenameFromPathname ( location . pathname , basename )
787- : location . pathname || '/' ;
776+ : location . pathname ;
788777
789778 return [ fallbackTransactionName , 'url' ] ;
790779}
@@ -904,15 +893,6 @@ export function resolveRouteNameAndSource(
904893 allRoutes : RouteObject [ ] ,
905894 branches : RouteMatch [ ] ,
906895 basename : string = '' ,
907- locationIsInsideDescendantRoute : ( location : Location , routes : RouteObject [ ] ) => boolean ,
908- rebuildRoutePathFromAllRoutes : ( allRoutes : RouteObject [ ] , location : Location ) => string ,
909- getNormalizedName : (
910- routes : RouteObject [ ] ,
911- location : Location ,
912- branches : RouteMatch [ ] ,
913- basename ?: string ,
914- ) => [ string , TransactionSource ] ,
915- prefixWithSlash : ( path : string ) => string ,
916896) : [ string , TransactionSource ] {
917897 let name : string | undefined ;
918898 let source : TransactionSource = 'url' ;
@@ -931,51 +911,6 @@ export function resolveRouteNameAndSource(
931911 return [ name || location . pathname , source ] ;
932912}
933913
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-
979914/**
980915 * Handles updating an existing navigation span
981916 */
0 commit comments