@@ -537,7 +537,7 @@ export type ProcessedTree<
537537 /** @deprecated keep until v2 so that `router.matchRoute` can keep not caring about the actual route tree */
538538 singleCache : LRUCache < string , AnySegmentNode < TSingle > >
539539 /** a cache of route matches from the `segmentTree` */
540- matchCache : LRUCache < string , ReturnType < typeof findMatch < TTree > > >
540+ matchCache : LRUCache < string , RouteMatch < TTree > | null >
541541 /** a cache of route matches from the `masksTree` */
542542 flatCache : LRUCache < string , ReturnType < typeof findMatch < TFlat > > > | null
543543}
@@ -603,6 +603,12 @@ export function findSingleMatch(
603603 return findMatch ( path , tree , fuzzy )
604604}
605605
606+ type RouteMatch < T extends Extract < RouteLike , { fullPath : string } > > = {
607+ route : T
608+ params : Record < string , string >
609+ branch : ReadonlyArray < T >
610+ }
611+
606612export function findRouteMatch <
607613 T extends Extract < RouteLike , { fullPath : string } > ,
608614> (
@@ -612,12 +618,17 @@ export function findRouteMatch<
612618 processedTree : ProcessedTree < T , any , any > ,
613619 /** If `true`, allows fuzzy matching (partial matches), i.e. which node in the tree would have been an exact match if the `path` had been shorter? */
614620 fuzzy = false ,
615- ) {
616- const key = fuzzy ? `fuzzy \0${ path } ` : path
621+ ) : RouteMatch < T > | null {
622+ const key = fuzzy ? path : `nofuzz \0${ path } ` // the main use for `findRouteMatch` is fuzzy:true, so we optimize for that case
617623 const cached = processedTree . matchCache . get ( key )
618- if ( cached ) return cached
624+ if ( cached !== undefined ) return cached
619625 path ||= '/'
620- const result = findMatch ( path , processedTree . segmentTree , fuzzy )
626+ const result = findMatch (
627+ path ,
628+ processedTree . segmentTree ,
629+ fuzzy ,
630+ ) as RouteMatch < T > | null
631+ if ( result ) result . branch = buildRouteBranch ( result . route )
621632 processedTree . matchCache . set ( key , result )
622633 return result
623634}
@@ -676,10 +687,7 @@ export function processRouteTree<
676687 const processedTree : ProcessedTree < TRouteLike , any , any > = {
677688 segmentTree,
678689 singleCache : createLRUCache < string , AnySegmentNode < any > > ( 1000 ) ,
679- matchCache : createLRUCache <
680- string ,
681- ReturnType < typeof findMatch < TRouteLike > >
682- > ( 1000 ) ,
690+ matchCache : createLRUCache < string , RouteMatch < TRouteLike > | null > ( 1000 ) ,
683691 flatCache : null ,
684692 masksTree : null ,
685693 }
@@ -780,6 +788,16 @@ function extractParams<T extends RouteLike>(
780788 return params
781789}
782790
791+ function buildRouteBranch < T extends RouteLike > ( route : T ) {
792+ const list = [ route ]
793+ while ( route . parentRoute ) {
794+ route = route . parentRoute as T
795+ list . push ( route )
796+ }
797+ list . reverse ( )
798+ return list
799+ }
800+
783801function buildBranch < T extends RouteLike > ( node : AnySegmentNode < T > ) {
784802 const list : Array < AnySegmentNode < T > > = Array ( node . depth + 1 )
785803 do {
0 commit comments