@tanstack/router-core 1.131.38 → 1.131.40

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.
@@ -0,0 +1,233 @@
1
+ import invariant from 'tiny-invariant'
2
+ import {
3
+ SEGMENT_TYPE_OPTIONAL_PARAM,
4
+ SEGMENT_TYPE_PARAM,
5
+ SEGMENT_TYPE_PATHNAME,
6
+ parsePathname,
7
+ trimPathLeft,
8
+ trimPathRight,
9
+ } from './path'
10
+ import type { Segment } from './path'
11
+ import type { RouteLike } from './route'
12
+
13
+ const SLASH_SCORE = 0.75
14
+ const STATIC_SEGMENT_SCORE = 1
15
+ const REQUIRED_PARAM_BASE_SCORE = 0.5
16
+ const OPTIONAL_PARAM_BASE_SCORE = 0.4
17
+ const WILDCARD_PARAM_BASE_SCORE = 0.25
18
+ const STATIC_AFTER_DYNAMIC_BONUS_SCORE = 0.2
19
+ const BOTH_PRESENCE_BASE_SCORE = 0.05
20
+ const PREFIX_PRESENCE_BASE_SCORE = 0.02
21
+ const SUFFIX_PRESENCE_BASE_SCORE = 0.01
22
+ const PREFIX_LENGTH_SCORE_MULTIPLIER = 0.0002
23
+ const SUFFIX_LENGTH_SCORE_MULTIPLIER = 0.0001
24
+
25
+ function handleParam(segment: Segment, baseScore: number) {
26
+ if (segment.prefixSegment && segment.suffixSegment) {
27
+ return (
28
+ baseScore +
29
+ BOTH_PRESENCE_BASE_SCORE +
30
+ PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length +
31
+ SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length
32
+ )
33
+ }
34
+
35
+ if (segment.prefixSegment) {
36
+ return (
37
+ baseScore +
38
+ PREFIX_PRESENCE_BASE_SCORE +
39
+ PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length
40
+ )
41
+ }
42
+
43
+ if (segment.suffixSegment) {
44
+ return (
45
+ baseScore +
46
+ SUFFIX_PRESENCE_BASE_SCORE +
47
+ SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length
48
+ )
49
+ }
50
+
51
+ return baseScore
52
+ }
53
+
54
+ function sortRoutes<TRouteLike extends RouteLike>(
55
+ routes: ReadonlyArray<TRouteLike>,
56
+ ): Array<TRouteLike> {
57
+ const scoredRoutes: Array<{
58
+ child: TRouteLike
59
+ trimmed: string
60
+ parsed: ReadonlyArray<Segment>
61
+ index: number
62
+ scores: Array<number>
63
+ hasStaticAfter: boolean
64
+ optionalParamCount: number
65
+ }> = []
66
+
67
+ routes.forEach((d, i) => {
68
+ if (d.isRoot || !d.path) {
69
+ return
70
+ }
71
+
72
+ const trimmed = trimPathLeft(d.fullPath)
73
+ let parsed = parsePathname(trimmed)
74
+
75
+ // Removes the leading slash if it is not the only remaining segment
76
+ let skip = 0
77
+ while (parsed.length > skip + 1 && parsed[skip]?.value === '/') {
78
+ skip++
79
+ }
80
+ if (skip > 0) parsed = parsed.slice(skip)
81
+
82
+ let optionalParamCount = 0
83
+ let hasStaticAfter = false
84
+ const scores = parsed.map((segment, index) => {
85
+ if (segment.value === '/') {
86
+ return SLASH_SCORE
87
+ }
88
+
89
+ if (segment.type === SEGMENT_TYPE_PATHNAME) {
90
+ return STATIC_SEGMENT_SCORE
91
+ }
92
+
93
+ let baseScore: number | undefined = undefined
94
+ if (segment.type === SEGMENT_TYPE_PARAM) {
95
+ baseScore = REQUIRED_PARAM_BASE_SCORE
96
+ } else if (segment.type === SEGMENT_TYPE_OPTIONAL_PARAM) {
97
+ baseScore = OPTIONAL_PARAM_BASE_SCORE
98
+ optionalParamCount++
99
+ } else {
100
+ baseScore = WILDCARD_PARAM_BASE_SCORE
101
+ }
102
+
103
+ // if there is any static segment (that is not an index) after a required / optional param,
104
+ // we will boost this param so it ranks higher than a required/optional param without a static segment after it
105
+ // JUST FOR SORTING, NOT FOR MATCHING
106
+ for (let i = index + 1; i < parsed.length; i++) {
107
+ const nextSegment = parsed[i]!
108
+ if (
109
+ nextSegment.type === SEGMENT_TYPE_PATHNAME &&
110
+ nextSegment.value !== '/'
111
+ ) {
112
+ hasStaticAfter = true
113
+ return handleParam(
114
+ segment,
115
+ baseScore + STATIC_AFTER_DYNAMIC_BONUS_SCORE,
116
+ )
117
+ }
118
+ }
119
+
120
+ return handleParam(segment, baseScore)
121
+ })
122
+
123
+ scoredRoutes.push({
124
+ child: d,
125
+ trimmed,
126
+ parsed,
127
+ index: i,
128
+ scores,
129
+ optionalParamCount,
130
+ hasStaticAfter,
131
+ })
132
+ })
133
+
134
+ const flatRoutes = scoredRoutes
135
+ .sort((a, b) => {
136
+ const minLength = Math.min(a.scores.length, b.scores.length)
137
+
138
+ // Sort by segment-by-segment score comparison ONLY for the common prefix
139
+ for (let i = 0; i < minLength; i++) {
140
+ if (a.scores[i] !== b.scores[i]) {
141
+ return b.scores[i]! - a.scores[i]!
142
+ }
143
+ }
144
+
145
+ // If all common segments have equal scores, then consider length and specificity
146
+ if (a.scores.length !== b.scores.length) {
147
+ // If different number of optional parameters, fewer optional parameters wins (more specific)
148
+ // only if both or none of the routes has static segments after the params
149
+ if (a.optionalParamCount !== b.optionalParamCount) {
150
+ if (a.hasStaticAfter === b.hasStaticAfter) {
151
+ return a.optionalParamCount - b.optionalParamCount
152
+ } else if (a.hasStaticAfter && !b.hasStaticAfter) {
153
+ return -1
154
+ } else if (!a.hasStaticAfter && b.hasStaticAfter) {
155
+ return 1
156
+ }
157
+ }
158
+
159
+ // If same number of optional parameters, longer path wins (for static segments)
160
+ return b.scores.length - a.scores.length
161
+ }
162
+
163
+ // Sort by min available parsed value for alphabetical ordering
164
+ for (let i = 0; i < minLength; i++) {
165
+ if (a.parsed[i]!.value !== b.parsed[i]!.value) {
166
+ return a.parsed[i]!.value > b.parsed[i]!.value ? 1 : -1
167
+ }
168
+ }
169
+
170
+ // Sort by original index
171
+ return a.index - b.index
172
+ })
173
+ .map((d, i) => {
174
+ d.child.rank = i
175
+ return d.child
176
+ })
177
+
178
+ return flatRoutes
179
+ }
180
+
181
+ export type ProcessRouteTreeResult<TRouteLike extends RouteLike> = {
182
+ routesById: Record<string, TRouteLike>
183
+ routesByPath: Record<string, TRouteLike>
184
+ flatRoutes: Array<TRouteLike>
185
+ }
186
+
187
+ export function processRouteTree<TRouteLike extends RouteLike>({
188
+ routeTree,
189
+ initRoute,
190
+ }: {
191
+ routeTree: TRouteLike
192
+ initRoute?: (route: TRouteLike, index: number) => void
193
+ }): ProcessRouteTreeResult<TRouteLike> {
194
+ const routesById = {} as Record<string, TRouteLike>
195
+ const routesByPath = {} as Record<string, TRouteLike>
196
+
197
+ const recurseRoutes = (childRoutes: Array<TRouteLike>) => {
198
+ childRoutes.forEach((childRoute, i) => {
199
+ initRoute?.(childRoute, i)
200
+
201
+ const existingRoute = routesById[childRoute.id]
202
+
203
+ invariant(
204
+ !existingRoute,
205
+ `Duplicate routes found with id: ${String(childRoute.id)}`,
206
+ )
207
+
208
+ routesById[childRoute.id] = childRoute
209
+
210
+ if (!childRoute.isRoot && childRoute.path) {
211
+ const trimmedFullPath = trimPathRight(childRoute.fullPath)
212
+ if (
213
+ !routesByPath[trimmedFullPath] ||
214
+ childRoute.fullPath.endsWith('/')
215
+ ) {
216
+ routesByPath[trimmedFullPath] = childRoute
217
+ }
218
+ }
219
+
220
+ const children = childRoute.children as Array<TRouteLike>
221
+
222
+ if (children?.length) {
223
+ recurseRoutes(children)
224
+ }
225
+ })
226
+ }
227
+
228
+ recurseRoutes([routeTree])
229
+
230
+ const flatRoutes = sortRoutes(Object.values(routesById))
231
+
232
+ return { routesById, routesByPath, flatRoutes }
233
+ }
package/src/route.ts CHANGED
@@ -1749,3 +1749,16 @@ export class BaseRootRoute<
1749
1749
  }
1750
1750
 
1751
1751
  //
1752
+
1753
+ export interface RouteLike {
1754
+ id: string
1755
+ isRoot?: boolean
1756
+ path?: string
1757
+ fullPath: string
1758
+ rank?: number
1759
+ parentRoute?: RouteLike
1760
+ children?: Array<RouteLike>
1761
+ options?: {
1762
+ caseSensitive?: boolean
1763
+ }
1764
+ }
package/src/router.ts CHANGED
@@ -4,7 +4,6 @@ import {
4
4
  createMemoryHistory,
5
5
  parseHref,
6
6
  } from '@tanstack/history'
7
- import invariant from 'tiny-invariant'
8
7
  import {
9
8
  createControlledPromise,
10
9
  deepEqual,
@@ -13,19 +12,14 @@ import {
13
12
  last,
14
13
  replaceEqualDeep,
15
14
  } from './utils'
15
+ import { processRouteTree } from './process-route-tree'
16
16
  import {
17
- SEGMENT_TYPE_OPTIONAL_PARAM,
18
- SEGMENT_TYPE_PARAM,
19
- SEGMENT_TYPE_PATHNAME,
20
- SEGMENT_TYPE_WILDCARD,
21
17
  cleanPath,
22
18
  interpolatePath,
23
19
  joinPaths,
24
20
  matchPathname,
25
- parsePathname,
26
21
  resolvePath,
27
22
  trimPath,
28
- trimPathLeft,
29
23
  trimPathRight,
30
24
  } from './path'
31
25
  import { isNotFound } from './not-found'
@@ -35,7 +29,7 @@ import { rootRouteId } from './root'
35
29
  import { isRedirect, redirect } from './redirect'
36
30
  import { createLRUCache } from './lru-cache'
37
31
  import { loadMatches, loadRouteChunk, routeNeedsPreload } from './load-matches'
38
- import type { ParsePathnameCache, Segment } from './path'
32
+ import type { ParsePathnameCache } from './path'
39
33
  import type { SearchParser, SearchSerializer } from './searchParams'
40
34
  import type { AnyRedirect, ResolvedRedirect } from './redirect'
41
35
  import type {
@@ -59,6 +53,7 @@ import type {
59
53
  AnyRouteWithContext,
60
54
  MakeRemountDepsOptionsUnion,
61
55
  RouteContextOptions,
56
+ RouteLike,
62
57
  RouteMask,
63
58
  SearchMiddleware,
64
59
  } from './route'
@@ -2383,229 +2378,6 @@ function validateSearch(validateSearch: AnyValidator, input: unknown): unknown {
2383
2378
  return {}
2384
2379
  }
2385
2380
 
2386
- interface RouteLike {
2387
- id: string
2388
- isRoot?: boolean
2389
- path?: string
2390
- fullPath: string
2391
- rank?: number
2392
- parentRoute?: RouteLike
2393
- children?: Array<RouteLike>
2394
- options?: {
2395
- caseSensitive?: boolean
2396
- }
2397
- }
2398
-
2399
- export type ProcessRouteTreeResult<TRouteLike extends RouteLike> = {
2400
- routesById: Record<string, TRouteLike>
2401
- routesByPath: Record<string, TRouteLike>
2402
- flatRoutes: Array<TRouteLike>
2403
- }
2404
-
2405
- const REQUIRED_PARAM_BASE_SCORE = 0.5
2406
- const OPTIONAL_PARAM_BASE_SCORE = 0.4
2407
- const WILDCARD_PARAM_BASE_SCORE = 0.25
2408
- const BOTH_PRESENCE_BASE_SCORE = 0.05
2409
- const PREFIX_PRESENCE_BASE_SCORE = 0.02
2410
- const SUFFIX_PRESENCE_BASE_SCORE = 0.01
2411
- const PREFIX_LENGTH_SCORE_MULTIPLIER = 0.0002
2412
- const SUFFIX_LENGTH_SCORE_MULTIPLIER = 0.0001
2413
-
2414
- function handleParam(segment: Segment, baseScore: number) {
2415
- if (segment.prefixSegment && segment.suffixSegment) {
2416
- return (
2417
- baseScore +
2418
- BOTH_PRESENCE_BASE_SCORE +
2419
- PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length +
2420
- SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length
2421
- )
2422
- }
2423
-
2424
- if (segment.prefixSegment) {
2425
- return (
2426
- baseScore +
2427
- PREFIX_PRESENCE_BASE_SCORE +
2428
- PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length
2429
- )
2430
- }
2431
-
2432
- if (segment.suffixSegment) {
2433
- return (
2434
- baseScore +
2435
- SUFFIX_PRESENCE_BASE_SCORE +
2436
- SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length
2437
- )
2438
- }
2439
-
2440
- return baseScore
2441
- }
2442
-
2443
- export function processRouteTree<TRouteLike extends RouteLike>({
2444
- routeTree,
2445
- initRoute,
2446
- }: {
2447
- routeTree: TRouteLike
2448
- initRoute?: (route: TRouteLike, index: number) => void
2449
- }): ProcessRouteTreeResult<TRouteLike> {
2450
- const routesById = {} as Record<string, TRouteLike>
2451
- const routesByPath = {} as Record<string, TRouteLike>
2452
-
2453
- const recurseRoutes = (childRoutes: Array<TRouteLike>) => {
2454
- childRoutes.forEach((childRoute, i) => {
2455
- initRoute?.(childRoute, i)
2456
-
2457
- const existingRoute = routesById[childRoute.id]
2458
-
2459
- invariant(
2460
- !existingRoute,
2461
- `Duplicate routes found with id: ${String(childRoute.id)}`,
2462
- )
2463
-
2464
- routesById[childRoute.id] = childRoute
2465
-
2466
- if (!childRoute.isRoot && childRoute.path) {
2467
- const trimmedFullPath = trimPathRight(childRoute.fullPath)
2468
- if (
2469
- !routesByPath[trimmedFullPath] ||
2470
- childRoute.fullPath.endsWith('/')
2471
- ) {
2472
- routesByPath[trimmedFullPath] = childRoute
2473
- }
2474
- }
2475
-
2476
- const children = childRoute.children as Array<TRouteLike>
2477
-
2478
- if (children?.length) {
2479
- recurseRoutes(children)
2480
- }
2481
- })
2482
- }
2483
-
2484
- recurseRoutes([routeTree])
2485
-
2486
- const scoredRoutes: Array<{
2487
- child: TRouteLike
2488
- trimmed: string
2489
- parsed: ReadonlyArray<Segment>
2490
- index: number
2491
- scores: Array<number>
2492
- hasStaticAfter: boolean
2493
- optionalParamCount: number
2494
- }> = []
2495
-
2496
- const routes: Array<TRouteLike> = Object.values(routesById)
2497
-
2498
- routes.forEach((d, i) => {
2499
- if (d.isRoot || !d.path) {
2500
- return
2501
- }
2502
-
2503
- const trimmed = trimPathLeft(d.fullPath)
2504
- let parsed = parsePathname(trimmed)
2505
-
2506
- // Removes the leading slash if it is not the only remaining segment
2507
- let skip = 0
2508
- while (parsed.length > skip + 1 && parsed[skip]?.value === '/') {
2509
- skip++
2510
- }
2511
- if (skip > 0) parsed = parsed.slice(skip)
2512
-
2513
- let optionalParamCount = 0
2514
- let hasStaticAfter = false
2515
- const scores = parsed.map((segment, index) => {
2516
- if (segment.value === '/') {
2517
- return 0.75
2518
- }
2519
-
2520
- let baseScore: number | undefined = undefined
2521
- if (segment.type === SEGMENT_TYPE_PARAM) {
2522
- baseScore = REQUIRED_PARAM_BASE_SCORE
2523
- } else if (segment.type === SEGMENT_TYPE_OPTIONAL_PARAM) {
2524
- baseScore = OPTIONAL_PARAM_BASE_SCORE
2525
- optionalParamCount++
2526
- } else if (segment.type === SEGMENT_TYPE_WILDCARD) {
2527
- baseScore = WILDCARD_PARAM_BASE_SCORE
2528
- }
2529
-
2530
- if (baseScore) {
2531
- // if there is any static segment (that is not an index) after a required / optional param,
2532
- // we will boost this param so it ranks higher than a required/optional param without a static segment after it
2533
- // JUST FOR SORTING, NOT FOR MATCHING
2534
- for (let i = index + 1; i < parsed.length; i++) {
2535
- const nextSegment = parsed[i]!
2536
- if (
2537
- nextSegment.type === SEGMENT_TYPE_PATHNAME &&
2538
- nextSegment.value !== '/'
2539
- ) {
2540
- hasStaticAfter = true
2541
- return handleParam(segment, baseScore + 0.2)
2542
- }
2543
- }
2544
-
2545
- return handleParam(segment, baseScore)
2546
- }
2547
-
2548
- return 1
2549
- })
2550
-
2551
- scoredRoutes.push({
2552
- child: d,
2553
- trimmed,
2554
- parsed,
2555
- index: i,
2556
- scores,
2557
- optionalParamCount,
2558
- hasStaticAfter,
2559
- })
2560
- })
2561
-
2562
- const flatRoutes = scoredRoutes
2563
- .sort((a, b) => {
2564
- const minLength = Math.min(a.scores.length, b.scores.length)
2565
-
2566
- // Sort by segment-by-segment score comparison ONLY for the common prefix
2567
- for (let i = 0; i < minLength; i++) {
2568
- if (a.scores[i] !== b.scores[i]) {
2569
- return b.scores[i]! - a.scores[i]!
2570
- }
2571
- }
2572
-
2573
- // If all common segments have equal scores, then consider length and specificity
2574
- if (a.scores.length !== b.scores.length) {
2575
- // If different number of optional parameters, fewer optional parameters wins (more specific)
2576
- // only if both or none of the routes has static segments after the params
2577
- if (a.optionalParamCount !== b.optionalParamCount) {
2578
- if (a.hasStaticAfter === b.hasStaticAfter) {
2579
- return a.optionalParamCount - b.optionalParamCount
2580
- } else if (a.hasStaticAfter && !b.hasStaticAfter) {
2581
- return -1
2582
- } else if (!a.hasStaticAfter && b.hasStaticAfter) {
2583
- return 1
2584
- }
2585
- }
2586
-
2587
- // If same number of optional parameters, longer path wins (for static segments)
2588
- return b.scores.length - a.scores.length
2589
- }
2590
-
2591
- // Sort by min available parsed value for alphabetical ordering
2592
- for (let i = 0; i < minLength; i++) {
2593
- if (a.parsed[i]!.value !== b.parsed[i]!.value) {
2594
- return a.parsed[i]!.value > b.parsed[i]!.value ? 1 : -1
2595
- }
2596
- }
2597
-
2598
- // Sort by original index
2599
- return a.index - b.index
2600
- })
2601
- .map((d, i) => {
2602
- d.child.rank = i
2603
- return d.child
2604
- })
2605
-
2606
- return { routesById, routesByPath, flatRoutes }
2607
- }
2608
-
2609
2381
  export function getMatchedRoutes<TRouteLike extends RouteLike>({
2610
2382
  pathname,
2611
2383
  routePathname,