@tanstack/react-router 1.90.0 → 1.91.0

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.
package/src/router.ts CHANGED
@@ -2553,37 +2553,7 @@ export class Router<
2553
2553
 
2554
2554
  // Actually run the loader and handle the result
2555
2555
  try {
2556
- if (route._lazyPromise === undefined) {
2557
- if (route.lazyFn) {
2558
- route._lazyPromise = route
2559
- .lazyFn()
2560
- .then((lazyRoute) => {
2561
- // explicitly don't copy over the lazy route's id
2562
- const { id: _id, ...options } =
2563
- lazyRoute.options
2564
- Object.assign(route.options, options)
2565
- })
2566
- } else {
2567
- route._lazyPromise = Promise.resolve()
2568
- }
2569
- }
2570
-
2571
- // If for some reason lazy resolves more lazy components...
2572
- // We'll wait for that before pre attempt to preload any
2573
- // components themselves.
2574
- if (route._componentsPromise === undefined) {
2575
- route._componentsPromise = route._lazyPromise.then(
2576
- () =>
2577
- Promise.all(
2578
- componentTypes.map(async (type) => {
2579
- const component = route.options[type]
2580
- if ((component as any)?.preload) {
2581
- await (component as any).preload()
2582
- }
2583
- }),
2584
- ),
2585
- )
2586
- }
2556
+ this.loadRouteChunk(route)
2587
2557
 
2588
2558
  updateMatch(matchId, (prev) => ({
2589
2559
  ...prev,
@@ -2821,6 +2791,37 @@ export class Router<
2821
2791
  this.clearCache({ filter })
2822
2792
  }
2823
2793
 
2794
+ loadRouteChunk = (route: AnyRoute) => {
2795
+ if (route._lazyPromise === undefined) {
2796
+ if (route.lazyFn) {
2797
+ route._lazyPromise = route.lazyFn().then((lazyRoute) => {
2798
+ // explicitly don't copy over the lazy route's id
2799
+ const { id: _id, ...options } = lazyRoute.options
2800
+ Object.assign(route.options, options)
2801
+ })
2802
+ } else {
2803
+ route._lazyPromise = Promise.resolve()
2804
+ }
2805
+ }
2806
+
2807
+ // If for some reason lazy resolves more lazy components...
2808
+ // We'll wait for that before pre attempt to preload any
2809
+ // components themselves.
2810
+ if (route._componentsPromise === undefined) {
2811
+ route._componentsPromise = route._lazyPromise.then(() =>
2812
+ Promise.all(
2813
+ componentTypes.map(async (type) => {
2814
+ const component = route.options[type]
2815
+ if ((component as any)?.preload) {
2816
+ await (component as any).preload()
2817
+ }
2818
+ }),
2819
+ ),
2820
+ )
2821
+ }
2822
+ return route._componentsPromise
2823
+ }
2824
+
2824
2825
  preloadRoute = async <
2825
2826
  TFrom extends RoutePaths<TRouteTree> | string = string,
2826
2827
  TTo extends string | undefined = undefined,
@@ -0,0 +1,168 @@
1
+ import type {
2
+ FromPathOption,
3
+ NavigateOptions,
4
+ PathParamOptions,
5
+ SearchParamOptions,
6
+ ToPathOption,
7
+ } from './link'
8
+ import type { RouteIds } from './routeInfo'
9
+ import type { AnyRouter, RegisteredRouter } from './router'
10
+ import type { UseParamsOptions, UseParamsResult } from './useParams'
11
+ import type { UseSearchOptions, UseSearchResult } from './useSearch'
12
+ import type { Constrain, ConstrainLiteral } from './utils'
13
+
14
+ export type ValidateFromPath<
15
+ TFrom,
16
+ TRouter extends AnyRouter = RegisteredRouter,
17
+ > = FromPathOption<TRouter, TFrom>
18
+
19
+ export type ValidateToPath<
20
+ TTo extends string | undefined,
21
+ TFrom extends string = string,
22
+ TRouter extends AnyRouter = RegisteredRouter,
23
+ > = ToPathOption<TRouter, TFrom, TTo>
24
+
25
+ export type ValidateSearch<
26
+ TTo extends string | undefined,
27
+ TFrom extends string = string,
28
+ TRouter extends AnyRouter = RegisteredRouter,
29
+ > = SearchParamOptions<TRouter, TFrom, TTo>
30
+
31
+ export type ValidateParams<
32
+ TTo extends string | undefined,
33
+ TFrom extends string = string,
34
+ TRouter extends AnyRouter = RegisteredRouter,
35
+ > = PathParamOptions<TRouter, TFrom, TTo>
36
+
37
+ /**
38
+ * @internal
39
+ */
40
+ export type InferFrom<TOptions> = TOptions extends {
41
+ from: infer TFrom extends string
42
+ }
43
+ ? TFrom
44
+ : string
45
+
46
+ /**
47
+ * @internal
48
+ */
49
+ export type InferTo<TOptions> = TOptions extends {
50
+ to: infer TTo extends string
51
+ }
52
+ ? TTo
53
+ : undefined
54
+
55
+ /**
56
+ * @internal
57
+ */
58
+ export type InferMaskTo<TOptions> = TOptions extends {
59
+ mask: { to: infer TTo extends string }
60
+ }
61
+ ? TTo
62
+ : string
63
+
64
+ export type InferMaskFrom<TOptions> = TOptions extends {
65
+ mask: { from: infer TFrom extends string }
66
+ }
67
+ ? TFrom
68
+ : string
69
+
70
+ export type ValidateNavigateOptions<
71
+ TOptions,
72
+ TRouter extends AnyRouter = RegisteredRouter,
73
+ > = Constrain<
74
+ TOptions,
75
+ NavigateOptions<
76
+ TRouter,
77
+ InferFrom<TOptions>,
78
+ InferTo<TOptions>,
79
+ InferMaskFrom<TOptions>,
80
+ InferMaskTo<TOptions>
81
+ >
82
+ >
83
+
84
+ export type ValidateNavigateOptionsArray<TOptions extends ReadonlyArray<any>> =
85
+ { [K in keyof TOptions]: ValidateNavigateOptions<TOptions[K]> }
86
+
87
+ export type ValidateId<
88
+ TId extends string,
89
+ TRouter extends AnyRouter = RegisteredRouter,
90
+ > = ConstrainLiteral<TId, RouteIds<TRouter['routeTree']>>
91
+
92
+ /**
93
+ * @internal
94
+ */
95
+ export type InferStrict<TOptions> = TOptions extends {
96
+ strict: infer TStrict extends boolean
97
+ }
98
+ ? TStrict
99
+ : true
100
+
101
+ /**
102
+ * @internal
103
+ */
104
+ export type InferSelected<TOptions> = TOptions extends {
105
+ select: (...args: Array<any>) => infer TSelected
106
+ }
107
+ ? TSelected
108
+ : unknown
109
+
110
+ /**
111
+ * @internal
112
+ */
113
+ export type InferStructuralSharing<TOptions> = TOptions extends {
114
+ structuralSharing: infer TStructuralSharing
115
+ }
116
+ ? TStructuralSharing
117
+ : unknown
118
+
119
+ export type ValidateUseSearchOptions<
120
+ TOptions,
121
+ TRouter extends AnyRouter = RegisteredRouter,
122
+ > = Constrain<
123
+ TOptions,
124
+ UseSearchOptions<
125
+ TRouter,
126
+ InferFrom<TOptions>,
127
+ InferStrict<TOptions>,
128
+ InferSelected<TOptions>,
129
+ InferStructuralSharing<TOptions>
130
+ >
131
+ >
132
+
133
+ export type ValidateUseSearchResult<
134
+ TOptions,
135
+ TRouter extends AnyRouter = RegisteredRouter,
136
+ > = UseSearchResult<
137
+ TRouter,
138
+ InferFrom<TOptions>,
139
+ InferStrict<TOptions>,
140
+ InferSelected<TOptions>
141
+ >
142
+
143
+ export type ValidateUseParamsOptions<
144
+ TOptions,
145
+ TRouter extends AnyRouter = RegisteredRouter,
146
+ > = Constrain<
147
+ TOptions,
148
+ UseParamsOptions<
149
+ TRouter,
150
+ InferFrom<TOptions>,
151
+ InferStrict<TOptions>,
152
+ InferSelected<TOptions>,
153
+ InferSelected<TOptions>
154
+ >
155
+ >
156
+
157
+ export type ValidateUseParamsResult<
158
+ TOptions,
159
+ TRouter extends AnyRouter = RegisteredRouter,
160
+ > = Constrain<
161
+ TOptions,
162
+ UseParamsResult<
163
+ TRouter,
164
+ InferFrom<TOptions>,
165
+ InferStrict<TOptions>,
166
+ InferSelected<TOptions>
167
+ >
168
+ >
package/src/utils.ts CHANGED
@@ -111,6 +111,14 @@ export type PartialMergeAll<TUnion> =
111
111
  | ExtractPrimitives<TUnion>
112
112
  | PartialMergeAllObject<TUnion>
113
113
 
114
+ export type Constrain<T, TConstraint, TDefault = TConstraint> =
115
+ | (T extends TConstraint ? T : never)
116
+ | TDefault
117
+
118
+ export type ConstrainLiteral<T, TConstraint, TDefault = TConstraint> =
119
+ | (T & TConstraint)
120
+ | TDefault
121
+
114
122
  /**
115
123
  * To be added to router types
116
124
  */
@@ -139,10 +147,6 @@ export type MergeAll<TUnion> =
139
147
  | MergeAllObjects<TUnion>
140
148
  | ExtractPrimitives<TUnion>
141
149
 
142
- export type Constrain<T, TConstraint, TDefault = TConstraint> =
143
- | (T extends TConstraint ? T : never)
144
- | TDefault
145
-
146
150
  export type ValidateJSON<T> = ((...args: Array<any>) => any) extends T
147
151
  ? unknown extends T
148
152
  ? never
@@ -359,7 +363,7 @@ export type StrictOrFrom<
359
363
  strict: TStrict
360
364
  }
361
365
  : {
362
- from: StringLiteral<Constrain<TFrom, RouteIds<TRouter['routeTree']>>>
366
+ from: ConstrainLiteral<TFrom, RouteIds<TRouter['routeTree']>>
363
367
  strict?: TStrict
364
368
  }
365
369