@tanstack/router-core 1.136.6 → 1.136.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -537,7 +537,7 @@ export type ProcessedTree<
537
537
  /** @deprecated keep until v2 so that `router.matchRoute` can keep not caring about the actual route tree */
538
538
  singleCache: LRUCache<string, AnySegmentNode<TSingle>>
539
539
  /** a cache of route matches from the `segmentTree` */
540
- matchCache: LRUCache<string, ReturnType<typeof findMatch<TTree>>>
540
+ matchCache: LRUCache<string, RouteMatch<TTree> | null>
541
541
  /** a cache of route matches from the `masksTree` */
542
542
  flatCache: LRUCache<string, ReturnType<typeof findMatch<TFlat>>> | null
543
543
  }
@@ -603,6 +603,12 @@ export function findSingleMatch(
603
603
  return findMatch(path, tree, fuzzy)
604
604
  }
605
605
 
606
+ type RouteMatch<T extends Extract<RouteLike, { fullPath: string }>> = {
607
+ route: T
608
+ params: Record<string, string>
609
+ branch: ReadonlyArray<T>
610
+ }
611
+
606
612
  export function findRouteMatch<
607
613
  T extends Extract<RouteLike, { fullPath: string }>,
608
614
  >(
@@ -612,12 +618,17 @@ export function findRouteMatch<
612
618
  processedTree: ProcessedTree<T, any, any>,
613
619
  /** 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? */
614
620
  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
617
623
  const cached = processedTree.matchCache.get(key)
618
- if (cached) return cached
624
+ if (cached !== undefined) return cached
619
625
  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)
621
632
  processedTree.matchCache.set(key, result)
622
633
  return result
623
634
  }
@@ -676,10 +687,7 @@ export function processRouteTree<
676
687
  const processedTree: ProcessedTree<TRouteLike, any, any> = {
677
688
  segmentTree,
678
689
  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),
683
691
  flatCache: null,
684
692
  masksTree: null,
685
693
  }
@@ -780,6 +788,16 @@ function extractParams<T extends RouteLike>(
780
788
  return params
781
789
  }
782
790
 
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
+
783
801
  function buildBranch<T extends RouteLike>(node: AnySegmentNode<T>) {
784
802
  const list: Array<AnySegmentNode<T>> = Array(node.depth + 1)
785
803
  do {
package/src/router.ts CHANGED
@@ -697,7 +697,7 @@ export type ParseLocationFn<TRouteTree extends AnyRoute> = (
697
697
  ) => ParsedLocation<FullSearchSchema<TRouteTree>>
698
698
 
699
699
  export type GetMatchRoutesFn = (pathname: string) => {
700
- matchedRoutes: Array<AnyRoute>
700
+ matchedRoutes: ReadonlyArray<AnyRoute>
701
701
  routeParams: Record<string, string>
702
702
  foundRoute: AnyRoute | undefined
703
703
  }
@@ -1248,9 +1248,9 @@ export class RouterCore<
1248
1248
  next: ParsedLocation,
1249
1249
  opts?: MatchRoutesOpts,
1250
1250
  ): Array<AnyRouteMatch> {
1251
- const { foundRoute, matchedRoutes, routeParams } = this.getMatchedRoutes(
1252
- next.pathname,
1253
- )
1251
+ const matchedRoutesResult = this.getMatchedRoutes(next.pathname)
1252
+ const { foundRoute, routeParams } = matchedRoutesResult
1253
+ let { matchedRoutes } = matchedRoutesResult
1254
1254
  let isGlobalNotFound = false
1255
1255
 
1256
1256
  // Check to see if the route needs a 404 entry
@@ -1263,7 +1263,7 @@ export class RouterCore<
1263
1263
  ) {
1264
1264
  // If the user has defined an (old) 404 route, use it
1265
1265
  if (this.options.notFoundRoute) {
1266
- matchedRoutes.push(this.options.notFoundRoute)
1266
+ matchedRoutes = [...matchedRoutes, this.options.notFoundRoute]
1267
1267
  } else {
1268
1268
  // If there is no routes found during path matching
1269
1269
  isGlobalNotFound = true
@@ -2642,15 +2642,7 @@ export function getMatchedRoutes<TRouteLike extends RouteLike>({
2642
2642
  Object.assign(routeParams, match.params) // Copy params, because they're cached
2643
2643
  }
2644
2644
 
2645
- let routeCursor: TRouteLike = foundRoute || routesById[rootRouteId]!
2646
-
2647
- const matchedRoutes: Array<TRouteLike> = [routeCursor]
2648
-
2649
- while (routeCursor.parentRoute) {
2650
- routeCursor = routeCursor.parentRoute as TRouteLike
2651
- matchedRoutes.push(routeCursor)
2652
- }
2653
- matchedRoutes.reverse()
2645
+ const matchedRoutes = match?.branch || [routesById[rootRouteId]!]
2654
2646
 
2655
2647
  return { matchedRoutes, routeParams, foundRoute }
2656
2648
  }