@@ -192,6 +192,12 @@ export interface ReactRouterOptions {
192192
193193type V6CompatibleVersion = '6' | '7' ;
194194
195+ type RouterWrapperOptions = Record < string , unknown > & {
196+ basename ?: string ;
197+ initialEntries ?: ( string | { pathname : string } ) [ ] ;
198+ initialIndex ?: number ;
199+ } ;
200+
195201export function addResolvedRoutesToParent ( resolvedRoutes : RouteObject [ ] , parentRoute : RouteObject ) : void {
196202 const existingChildren = parentRoute . children || [ ] ;
197203
@@ -408,26 +414,21 @@ function setupRouterSubscription(
408414 } ) ;
409415}
410416
411- /**
412- * Creates a wrapCreateBrowserRouter function that can be used with all React Router v6 compatible versions.
413- */
414- export function createV6CompatibleWrapCreateBrowserRouter <
415- TState extends RouterState = RouterState ,
416- TRouter extends Router < TState > = Router < TState > ,
417- > (
417+ function createRouterWrapper < TState extends RouterState = RouterState , TRouter extends Router < TState > = Router < TState > > (
418418 createRouterFunction : CreateRouterFunction < TState , TRouter > ,
419419 version : V6CompatibleVersion ,
420+ isMemoryRouter : boolean ,
420421) : CreateRouterFunction < TState , TRouter > {
421422 if ( ! _useEffect || ! _useLocation || ! _useNavigationType || ! _matchRoutes ) {
422423 DEBUG_BUILD &&
423424 debug . warn (
424- `reactRouterV${ version } Instrumentation was unable to wrap the \`createRouter\` function because of one or more missing parameters.` ,
425+ `reactRouterV${ version } Instrumentation was unable to wrap the \`${ isMemoryRouter ? 'createMemoryRouter' : ' createRouter' } \` function because of one or more missing parameters.` ,
425426 ) ;
426427
427428 return createRouterFunction ;
428429 }
429430
430- return function ( routes : RouteObject [ ] , opts ?: Record < string , unknown > & { basename ?: string } ) : TRouter {
431+ return function ( routes : RouteObject [ ] , opts ?: RouterWrapperOptions ) : TRouter {
431432 addRoutesToAllRoutes ( routes ) ;
432433
433434 if ( _enableAsyncRouteHandlers ) {
@@ -436,15 +437,19 @@ export function createV6CompatibleWrapCreateBrowserRouter<
436437 }
437438 }
438439
439- const wrappedOpts = wrapPatchRoutesOnNavigation ( opts ) ;
440+ const wrappedOpts = wrapPatchRoutesOnNavigation ( opts , isMemoryRouter ) ;
440441 const router = createRouterFunction ( routes , wrappedOpts ) ;
441442 const basename = opts ?. basename ;
442443 const activeRootSpan = getActiveRootSpan ( ) ;
443444
445+ const initialLocation = isMemoryRouter
446+ ? getInitialLocationFromEntries ( opts , router . state . location )
447+ : router . state . location ;
448+
444449 if ( router . state . historyAction === 'POP' && activeRootSpan ) {
445450 updatePageloadTransaction ( {
446451 activeRootSpan,
447- location : router . state . location ,
452+ location : initialLocation ,
448453 routes,
449454 basename,
450455 allRoutes : Array . from ( allRoutes ) ,
@@ -457,6 +462,44 @@ export function createV6CompatibleWrapCreateBrowserRouter<
457462 } ;
458463}
459464
465+ function getInitialLocationFromEntries ( opts : RouterWrapperOptions | undefined , fallbackLocation : Location ) : Location {
466+ const initialEntries = opts ?. initialEntries ;
467+ const initialIndex = opts ?. initialIndex ;
468+
469+ if ( initialEntries ?. length === 1 ) {
470+ const firstEntry = initialEntries [ 0 ] ;
471+ if ( firstEntry ) {
472+ return normalizeInitialEntry ( firstEntry ) ;
473+ }
474+ }
475+
476+ if ( typeof initialIndex === 'number' && initialEntries && initialEntries [ initialIndex ] ) {
477+ const indexedEntry = initialEntries [ initialIndex ] ;
478+ if ( indexedEntry ) {
479+ return normalizeInitialEntry ( indexedEntry ) ;
480+ }
481+ }
482+
483+ return fallbackLocation ;
484+ }
485+
486+ function normalizeInitialEntry ( entry : string | { pathname : string } ) : Location {
487+ return typeof entry === 'string' ? { pathname : entry } : entry ;
488+ }
489+
490+ /**
491+ * Creates a wrapCreateBrowserRouter function that can be used with all React Router v6 compatible versions.
492+ */
493+ export function createV6CompatibleWrapCreateBrowserRouter <
494+ TState extends RouterState = RouterState ,
495+ TRouter extends Router < TState > = Router < TState > ,
496+ > (
497+ createRouterFunction : CreateRouterFunction < TState , TRouter > ,
498+ version : V6CompatibleVersion ,
499+ ) : CreateRouterFunction < TState , TRouter > {
500+ return createRouterWrapper ( createRouterFunction , version , false ) ;
501+ }
502+
460503/**
461504 * Creates a wrapCreateMemoryRouter function that can be used with all React Router v6 compatible versions.
462505 */
@@ -467,72 +510,7 @@ export function createV6CompatibleWrapCreateMemoryRouter<
467510 createRouterFunction : CreateRouterFunction < TState , TRouter > ,
468511 version : V6CompatibleVersion ,
469512) : CreateRouterFunction < TState , TRouter > {
470- if ( ! _useEffect || ! _useLocation || ! _useNavigationType || ! _matchRoutes ) {
471- DEBUG_BUILD &&
472- debug . warn (
473- `reactRouterV${ version } Instrumentation was unable to wrap the \`createMemoryRouter\` function because of one or more missing parameters.` ,
474- ) ;
475-
476- return createRouterFunction ;
477- }
478-
479- return function (
480- routes : RouteObject [ ] ,
481- opts ?: Record < string , unknown > & {
482- basename ?: string ;
483- initialEntries ?: ( string | { pathname : string } ) [ ] ;
484- initialIndex ?: number ;
485- } ,
486- ) : TRouter {
487- addRoutesToAllRoutes ( routes ) ;
488-
489- if ( _enableAsyncRouteHandlers ) {
490- for ( const route of routes ) {
491- checkRouteForAsyncHandler ( route , processResolvedRoutes ) ;
492- }
493- }
494-
495- const wrappedOpts = wrapPatchRoutesOnNavigation ( opts , true ) ;
496-
497- const router = createRouterFunction ( routes , wrappedOpts ) ;
498- const basename = opts ?. basename ;
499-
500- let initialEntry = undefined ;
501-
502- const initialEntries = opts ?. initialEntries ;
503- const initialIndex = opts ?. initialIndex ;
504-
505- const hasOnlyOneInitialEntry = initialEntries && initialEntries . length === 1 ;
506- const hasIndexedEntry = initialIndex !== undefined && initialEntries && initialEntries [ initialIndex ] ;
507-
508- initialEntry = hasOnlyOneInitialEntry
509- ? initialEntries [ 0 ]
510- : hasIndexedEntry
511- ? initialEntries [ initialIndex ]
512- : undefined ;
513-
514- const location = initialEntry
515- ? typeof initialEntry === 'string'
516- ? { pathname : initialEntry }
517- : initialEntry
518- : router . state . location ;
519-
520- const memoryActiveRootSpan = getActiveRootSpan ( ) ;
521-
522- if ( router . state . historyAction === 'POP' && memoryActiveRootSpan ) {
523- updatePageloadTransaction ( {
524- activeRootSpan : memoryActiveRootSpan ,
525- location,
526- routes,
527- basename,
528- allRoutes : Array . from ( allRoutes ) ,
529- } ) ;
530- }
531-
532- setupRouterSubscription ( router , routes , version , basename , memoryActiveRootSpan ) ;
533-
534- return router ;
535- } ;
513+ return createRouterWrapper ( createRouterFunction , version , true ) ;
536514}
537515
538516/**
0 commit comments