@@ -773,6 +773,32 @@ export function createV6CompatibleWrapUseRoutes(origUseRoutes: UseRoutes, versio
773773 } ;
774774}
775775
776+ /**
777+ * Helper to update the current span (navigation or pageload) with lazy-loaded route information.
778+ * Reduces code duplication in patchRoutesOnNavigation wrapper.
779+ */
780+ function updateSpanWithLazyRoutes ( pathname : string , forceUpdate : boolean ) : void {
781+ const currentActiveRootSpan = getActiveRootSpan ( ) ;
782+ if ( ! currentActiveRootSpan ) {
783+ return ;
784+ }
785+
786+ const spanOp = ( spanToJSON ( currentActiveRootSpan ) as { op ?: string } ) . op ;
787+ const location = { pathname, search : '' , hash : '' , state : null , key : 'default' } ;
788+ const routesArray = Array . from ( allRoutes ) ;
789+
790+ if ( spanOp === 'navigation' ) {
791+ updateNavigationSpan ( currentActiveRootSpan , location , routesArray , forceUpdate , _matchRoutes ) ;
792+ } else if ( spanOp === 'pageload' ) {
793+ updatePageloadTransaction ( {
794+ activeRootSpan : currentActiveRootSpan ,
795+ location,
796+ routes : routesArray ,
797+ allRoutes : routesArray ,
798+ } ) ;
799+ }
800+ }
801+
776802function wrapPatchRoutesOnNavigation (
777803 opts : Record < string , unknown > | undefined ,
778804 isMemoryRouter = false ,
@@ -809,10 +835,16 @@ function wrapPatchRoutesOnNavigation(
809835 // to update the route objects in our allRoutes Set for proper route matching.
810836 if ( matches && matches . length > 0 ) {
811837 const leafMatch = matches [ matches . length - 1 ] ;
812- if ( leafMatch ?. route ) {
813- // Find the matching route in allRoutes by reference or path
838+ const leafRoute = leafMatch ?. route ;
839+ if ( leafRoute ) {
840+ // Find the matching route in allRoutes by id, reference, or path
814841 for ( const route of allRoutes ) {
815- if ( route === leafMatch . route || ( route . path && route . path === leafMatch . route . path ) ) {
842+ const idMatches = route . id !== undefined && route . id === routeId ;
843+ const referenceMatches = route === leafRoute ;
844+ const pathMatches =
845+ route . path !== undefined && leafRoute . path !== undefined && route . path === leafRoute . path ;
846+
847+ if ( idMatches || referenceMatches || pathMatches ) {
816848 // Attach children to this parent route
817849 addResolvedRoutesToParent ( children , route ) ;
818850 break ;
@@ -821,21 +853,9 @@ function wrapPatchRoutesOnNavigation(
821853 }
822854 }
823855
824- const currentActiveRootSpan = getActiveRootSpan ( ) ;
825856 // Only update if we have a valid targetPath (patchRoutesOnNavigation can be called without path)
826- if ( targetPath && currentActiveRootSpan ) {
827- const spanOp = ( spanToJSON ( currentActiveRootSpan ) as { op ?: string } ) . op ;
828- const location = { pathname : targetPath , search : '' , hash : '' , state : null , key : 'default' } ;
829- if ( spanOp === 'navigation' ) {
830- updateNavigationSpan ( currentActiveRootSpan , location , Array . from ( allRoutes ) , true , _matchRoutes ) ;
831- } else if ( spanOp === 'pageload' ) {
832- updatePageloadTransaction ( {
833- activeRootSpan : currentActiveRootSpan ,
834- location,
835- routes : Array . from ( allRoutes ) ,
836- allRoutes : Array . from ( allRoutes ) ,
837- } ) ;
838- }
857+ if ( targetPath ) {
858+ updateSpanWithLazyRoutes ( targetPath , true ) ;
839859 }
840860 return originalPatch ( routeId , children ) ;
841861 } ;
@@ -857,24 +877,9 @@ function wrapPatchRoutesOnNavigation(
857877 }
858878 }
859879
860- const currentActiveRootSpan = getActiveRootSpan ( ) ;
861- if ( currentActiveRootSpan ) {
862- const spanOp = ( spanToJSON ( currentActiveRootSpan ) as { op ?: string } ) . op ;
863- const pathname = isMemoryRouter ? targetPath : targetPath || WINDOW . location ?. pathname ;
864-
865- if ( pathname ) {
866- const location = { pathname, search : '' , hash : '' , state : null , key : 'default' } ;
867- if ( spanOp === 'navigation' ) {
868- updateNavigationSpan ( currentActiveRootSpan , location , Array . from ( allRoutes ) , false , _matchRoutes ) ;
869- } else if ( spanOp === 'pageload' ) {
870- updatePageloadTransaction ( {
871- activeRootSpan : currentActiveRootSpan ,
872- location,
873- routes : Array . from ( allRoutes ) ,
874- allRoutes : Array . from ( allRoutes ) ,
875- } ) ;
876- }
877- }
880+ const pathname = isMemoryRouter ? targetPath : targetPath || WINDOW . location ?. pathname ;
881+ if ( pathname ) {
882+ updateSpanWithLazyRoutes ( pathname , false ) ;
878883 }
879884
880885 return result ;
0 commit comments