@@ -279,7 +279,10 @@ export function createV6CompatibleWrapCreateBrowserRouter<
279279 state . historyAction === 'PUSH' || ( state . historyAction === 'POP' && isInitialPageloadComplete ) ;
280280
281281 if ( shouldHandleNavigation ) {
282- const navigationHandler = ( ) : void => {
282+ // Only handle navigation when it's complete (state is idle).
283+ // During 'loading' or 'submitting', state.location may still have the old pathname,
284+ // which would cause us to create a span for the wrong route.
285+ if ( state . navigation . state === 'idle' ) {
283286 handleNavigation ( {
284287 location : state . location ,
285288 routes,
@@ -288,13 +291,6 @@ export function createV6CompatibleWrapCreateBrowserRouter<
288291 basename,
289292 allRoutes : Array . from ( allRoutes ) ,
290293 } ) ;
291- } ;
292-
293- // Wait for the next render if loading an unsettled route
294- if ( state . navigation . state !== 'idle' ) {
295- requestAnimationFrame ( navigationHandler ) ;
296- } else {
297- navigationHandler ( ) ;
298294 }
299295 }
300296 } ) ;
@@ -632,7 +628,8 @@ export function handleNavigation(opts: {
632628 allRoutes ?: RouteObject [ ] ;
633629} ) : void {
634630 const { location, routes, navigationType, version, matches, basename, allRoutes } = opts ;
635- const branches = Array . isArray ( matches ) ? matches : _matchRoutes ( routes , location , basename ) ;
631+ // Use allRoutes for matching to include lazy-loaded routes
632+ const branches = Array . isArray ( matches ) ? matches : _matchRoutes ( allRoutes || routes , location , basename ) ;
636633
637634 const client = getClient ( ) ;
638635 if ( ! client || ! CLIENTS_WITH_INSTRUMENT_NAVIGATION . has ( client ) ) {
@@ -649,7 +646,7 @@ export function handleNavigation(opts: {
649646 if ( ( navigationType === 'PUSH' || navigationType === 'POP' ) && branches ) {
650647 const [ name , source ] = resolveRouteNameAndSource (
651648 location ,
652- routes ,
649+ allRoutes || routes ,
653650 allRoutes || routes ,
654651 branches as RouteMatch [ ] ,
655652 basename ,
@@ -659,8 +656,11 @@ export function handleNavigation(opts: {
659656 const spanJson = activeSpan && spanToJSON ( activeSpan ) ;
660657 const isAlreadyInNavigationSpan = spanJson ?. op === 'navigation' ;
661658
662- // Cross usage can result in multiple navigation spans being created without this check
663- if ( ! isAlreadyInNavigationSpan ) {
659+ // Only skip creating a new span if we're already in a navigation span AND the route name matches.
660+ // This handles cross-usage (multiple wrappers for same navigation) while allowing consecutive navigations.
661+ const isSpanForSameRoute = isAlreadyInNavigationSpan && spanJson ?. description === name ;
662+
663+ if ( ! isSpanForSameRoute ) {
664664 const navigationSpan = startBrowserTracingNavigationSpan ( client , {
665665 name,
666666 attributes : {
@@ -727,7 +727,7 @@ function updatePageloadTransaction({
727727 : ( _matchRoutes ( allRoutes || routes , location , basename ) as unknown as RouteMatch [ ] ) ;
728728
729729 if ( branches ) {
730- const [ name , source ] = resolveRouteNameAndSource ( location , routes , allRoutes || routes , branches , basename ) ;
730+ const [ name , source ] = resolveRouteNameAndSource ( location , allRoutes || routes , allRoutes || routes , branches , basename ) ;
731731
732732 getCurrentScope ( ) . setTransactionName ( name || '/' ) ;
733733
@@ -780,7 +780,7 @@ function patchSpanEnd(
780780 if ( branches ) {
781781 const [ name , source ] = resolveRouteNameAndSource (
782782 location ,
783- routes ,
783+ currentAllRoutes . length > 0 ? currentAllRoutes : routes ,
784784 currentAllRoutes . length > 0 ? currentAllRoutes : routes ,
785785 branches ,
786786 basename ,
0 commit comments