@tanstack/react-router 1.48.1 → 1.49.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.
Files changed (51) hide show
  1. package/dist/cjs/Matches.cjs +3 -1
  2. package/dist/cjs/Matches.cjs.map +1 -1
  3. package/dist/cjs/Matches.d.cts +5 -4
  4. package/dist/cjs/Transitioner.cjs +2 -2
  5. package/dist/cjs/Transitioner.cjs.map +1 -1
  6. package/dist/cjs/fileRoute.cjs.map +1 -1
  7. package/dist/cjs/fileRoute.d.cts +4 -4
  8. package/dist/cjs/index.cjs +2 -0
  9. package/dist/cjs/index.cjs.map +1 -1
  10. package/dist/cjs/index.d.cts +3 -1
  11. package/dist/cjs/route.cjs +1 -1
  12. package/dist/cjs/route.cjs.map +1 -1
  13. package/dist/cjs/route.d.cts +73 -59
  14. package/dist/cjs/router.cjs +90 -57
  15. package/dist/cjs/router.cjs.map +1 -1
  16. package/dist/cjs/router.d.cts +22 -9
  17. package/dist/cjs/transformer.cjs +39 -0
  18. package/dist/cjs/transformer.cjs.map +1 -0
  19. package/dist/cjs/transformer.d.cts +5 -0
  20. package/dist/cjs/utils.cjs.map +1 -1
  21. package/dist/cjs/utils.d.cts +1 -0
  22. package/dist/esm/Matches.d.ts +5 -4
  23. package/dist/esm/Matches.js +3 -1
  24. package/dist/esm/Matches.js.map +1 -1
  25. package/dist/esm/Transitioner.js +2 -2
  26. package/dist/esm/Transitioner.js.map +1 -1
  27. package/dist/esm/fileRoute.d.ts +4 -4
  28. package/dist/esm/fileRoute.js.map +1 -1
  29. package/dist/esm/index.d.ts +3 -1
  30. package/dist/esm/index.js +2 -0
  31. package/dist/esm/index.js.map +1 -1
  32. package/dist/esm/route.d.ts +73 -59
  33. package/dist/esm/route.js +1 -1
  34. package/dist/esm/route.js.map +1 -1
  35. package/dist/esm/router.d.ts +22 -9
  36. package/dist/esm/router.js +91 -58
  37. package/dist/esm/router.js.map +1 -1
  38. package/dist/esm/transformer.d.ts +5 -0
  39. package/dist/esm/transformer.js +39 -0
  40. package/dist/esm/transformer.js.map +1 -0
  41. package/dist/esm/utils.d.ts +1 -0
  42. package/dist/esm/utils.js.map +1 -1
  43. package/package.json +2 -2
  44. package/src/Matches.tsx +11 -7
  45. package/src/Transitioner.tsx +2 -2
  46. package/src/fileRoute.ts +26 -21
  47. package/src/index.tsx +5 -1
  48. package/src/route.ts +356 -186
  49. package/src/router.ts +144 -75
  50. package/src/transformer.ts +49 -0
  51. package/src/utils.ts +5 -0
package/src/route.ts CHANGED
@@ -16,7 +16,7 @@ import type { NavigateOptions, ParsePathParams, ToMaskOptions } from './link'
16
16
  import type { ParsedLocation } from './location'
17
17
  import type { RouteById, RouteIds, RoutePaths } from './routeInfo'
18
18
  import type { AnyRouter, RegisteredRouter, Router } from './router'
19
- import type { Assign, Expand, NoInfer, PickRequired } from './utils'
19
+ import type { Assign, Constrain, Expand, NoInfer, PickRequired } from './utils'
20
20
  import type { BuildLocationFn, NavigateFn } from './RouterProvider'
21
21
  import type { NotFoundError } from './not-found'
22
22
  import type { LazyRoute } from './fileRoute'
@@ -57,25 +57,23 @@ export type RouteOptions<
57
57
  TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
58
58
  TParams = AnyPathParams,
59
59
  TAllParams = TParams,
60
- TRouteContextReturn = RouteContext,
61
- TRouteContext = RouteContext,
62
- TParentAllContext = AnyContext,
63
- TAllContext = AnyContext,
64
60
  TLoaderDeps extends Record<string, any> = {},
65
61
  TLoaderDataReturn = {},
66
62
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
63
+ TRouterContext = {},
64
+ TRouteContextFn = AnyContext,
65
+ TBeforeLoadFn = AnyContext,
67
66
  > = BaseRouteOptions<
68
67
  TParentRoute,
69
68
  TCustomId,
70
69
  TPath,
71
70
  TSearchValidator,
72
71
  TParams,
73
- TAllParams,
74
- TRouteContextReturn,
75
- TParentAllContext,
76
- TAllContext,
77
72
  TLoaderDeps,
78
- TLoaderDataReturn
73
+ TLoaderDataReturn,
74
+ TRouterContext,
75
+ TRouteContextFn,
76
+ TBeforeLoadFn
79
77
  > &
80
78
  UpdatableRouteOptions<
81
79
  NoInfer<TParentRoute>,
@@ -83,9 +81,10 @@ export type RouteOptions<
83
81
  NoInfer<TAllParams>,
84
82
  NoInfer<TSearchValidator>,
85
83
  NoInfer<TLoaderData>,
86
- NoInfer<TAllContext>,
87
- NoInfer<TRouteContext>,
88
- NoInfer<TLoaderDeps>
84
+ NoInfer<TLoaderDeps>,
85
+ NoInfer<TRouterContext>,
86
+ NoInfer<TRouteContextFn>,
87
+ NoInfer<TBeforeLoadFn>
89
88
  >
90
89
 
91
90
  export type ParseParamsFn<TPath extends string, TParams> = (
@@ -115,52 +114,103 @@ export type ParamsOptions<TPath extends string, TParams> = {
115
114
  stringifyParams?: StringifyParamsFn<TPath, TParams>
116
115
  }
117
116
 
118
- export interface FullSearchSchemaOption<TFullSearchSchema> {
119
- search: TFullSearchSchema
117
+ export interface FullSearchSchemaOption<
118
+ in out TParentRoute extends AnyRoute,
119
+ in out TSearchValidator extends AnySearchValidator,
120
+ > {
121
+ search: Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
120
122
  }
121
123
 
124
+ export type RouteContextFn<
125
+ in out TParentRoute extends AnyRoute,
126
+ in out TSearchValidator extends AnySearchValidator,
127
+ in out TParams,
128
+ in out TRouterContext,
129
+ > = (
130
+ ctx: RouteContextOptions<
131
+ TParentRoute,
132
+ TSearchValidator,
133
+ TParams,
134
+ TRouterContext
135
+ >,
136
+ ) => any
137
+
138
+ export type BeforeLoadFn<
139
+ in out TParentRoute extends AnyRoute,
140
+ in out TSearchValidator extends AnySearchValidator,
141
+ in out TParams,
142
+ in out TRouterContext,
143
+ in out TRouteContextFn,
144
+ > = (
145
+ ctx: BeforeLoadContextOptions<
146
+ TParentRoute,
147
+ TSearchValidator,
148
+ TParams,
149
+ TRouterContext,
150
+ TRouteContextFn
151
+ >,
152
+ ) => any
153
+
122
154
  export type FileBaseRouteOptions<
123
155
  TParentRoute extends AnyRoute = AnyRoute,
124
156
  TPath extends string = string,
125
157
  TSearchValidator extends AnySearchValidator = undefined,
126
158
  TParams = {},
127
- TAllParams = {},
128
- TRouteContextReturn = RouteContext,
129
- TParentAllContext = AnyContext,
130
- TAllContext = AnyContext,
131
159
  TLoaderDeps extends Record<string, any> = {},
132
160
  TLoaderDataReturn = {},
133
- > = {
161
+ TRouterContext = {},
162
+ TRouteContextFn = AnyContext,
163
+ TBeforeLoadFn = AnyContext,
164
+ > = ParamsOptions<TPath, TParams> & {
134
165
  validateSearch?: TSearchValidator
135
166
  shouldReload?:
136
167
  | boolean
137
168
  | ((
138
169
  match: LoaderFnContext<
139
- TAllParams,
140
- ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
141
- TAllContext
170
+ TParentRoute,
171
+ TParams,
172
+ TLoaderDeps,
173
+ TRouterContext,
174
+ TRouteContextFn,
175
+ TBeforeLoadFn
142
176
  >,
143
177
  ) => any)
178
+
179
+ context?: Constrain<
180
+ TRouteContextFn,
181
+ RouteContextFn<TParentRoute, TSearchValidator, TParams, TRouterContext>
182
+ >
183
+
144
184
  // This async function is called before a route is loaded.
145
185
  // If an error is thrown here, the route's loader will not be called.
146
186
  // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
147
187
  // If thrown during a preload event, the error will be logged to the console.
148
- beforeLoad?: (
149
- ctx: BeforeLoadContext<
150
- ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
151
- TAllParams,
152
- TParentAllContext
153
- >,
154
- ) => Promise<TRouteContextReturn> | TRouteContextReturn | void
188
+ beforeLoad?: Constrain<
189
+ TBeforeLoadFn,
190
+ BeforeLoadFn<
191
+ TParentRoute,
192
+ TSearchValidator,
193
+ TParams,
194
+ TRouterContext,
195
+ TRouteContextFn
196
+ >
197
+ >
198
+
155
199
  loaderDeps?: (
156
- opts: FullSearchSchemaOption<
157
- ResolveFullSearchSchema<TParentRoute, TSearchValidator>
158
- >,
200
+ opts: FullSearchSchemaOption<TParentRoute, TSearchValidator>,
159
201
  ) => TLoaderDeps
202
+
160
203
  loader?: (
161
- ctx: LoaderFnContext<TAllParams, TLoaderDeps, TAllContext>,
204
+ ctx: LoaderFnContext<
205
+ TParentRoute,
206
+ TParams,
207
+ TLoaderDeps,
208
+ TRouterContext,
209
+ TRouteContextFn,
210
+ TBeforeLoadFn
211
+ >,
162
212
  ) => TLoaderDataReturn | Promise<TLoaderDataReturn>
163
- } & ParamsOptions<TPath, TParams>
213
+ }
164
214
 
165
215
  export type BaseRouteOptions<
166
216
  TParentRoute extends AnyRoute = AnyRoute,
@@ -168,37 +218,34 @@ export type BaseRouteOptions<
168
218
  TPath extends string = string,
169
219
  TSearchValidator extends AnySearchValidator = undefined,
170
220
  TParams = {},
171
- TAllParams = {},
172
- TRouteContextReturn = RouteContext,
173
- TParentAllContext = AnyContext,
174
- TAllContext = AnyContext,
175
221
  TLoaderDeps extends Record<string, any> = {},
176
222
  TLoaderDataReturn = {},
223
+ TRouterContext = {},
224
+ TRouteContextFn = AnyContext,
225
+ TBeforeLoadFn = AnyContext,
177
226
  > = RoutePathOptions<TCustomId, TPath> &
178
227
  FileBaseRouteOptions<
179
228
  TParentRoute,
180
229
  TPath,
181
230
  TSearchValidator,
182
231
  TParams,
183
- TAllParams,
184
- TRouteContextReturn,
185
- TParentAllContext,
186
- TAllContext,
187
232
  TLoaderDeps,
188
- TLoaderDataReturn
233
+ TLoaderDataReturn,
234
+ TRouterContext,
235
+ TRouteContextFn,
236
+ TBeforeLoadFn
189
237
  > & {
190
238
  getParentRoute: () => TParentRoute
191
239
  }
192
240
 
193
- export interface BeforeLoadContext<
194
- TFullSearchSchema,
195
- TAllParams,
196
- TParentAllContext,
197
- > extends FullSearchSchemaOption<TFullSearchSchema> {
241
+ export interface ContextOptions<
242
+ in out TParentRoute extends AnyRoute,
243
+ in out TSearchValidator extends AnySearchValidator,
244
+ in out TParams,
245
+ > extends FullSearchSchemaOption<TParentRoute, TSearchValidator> {
198
246
  abortController: AbortController
199
247
  preload: boolean
200
- params: Expand<TAllParams>
201
- context: TParentAllContext
248
+ params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>
202
249
  location: ParsedLocation
203
250
  /**
204
251
  * @deprecated Use `throw redirect({ to: '/somewhere' })` instead
@@ -208,15 +255,37 @@ export interface BeforeLoadContext<
208
255
  cause: 'preload' | 'enter' | 'stay'
209
256
  }
210
257
 
258
+ export interface RouteContextOptions<
259
+ in out TParentRoute extends AnyRoute,
260
+ in out TSearchValidator extends AnySearchValidator,
261
+ in out TParams,
262
+ in out TRouterContext,
263
+ > extends ContextOptions<TParentRoute, TSearchValidator, TParams> {
264
+ context: Expand<RouteContextParameter<TParentRoute, TRouterContext>>
265
+ }
266
+
267
+ export interface BeforeLoadContextOptions<
268
+ in out TParentRoute extends AnyRoute,
269
+ in out TSearchValidator extends AnySearchValidator,
270
+ in out TParams,
271
+ in out TRouterContext,
272
+ in out TRouteContextFn,
273
+ > extends ContextOptions<TParentRoute, TSearchValidator, TParams> {
274
+ context: Expand<
275
+ BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>
276
+ >
277
+ }
278
+
211
279
  export type UpdatableRouteOptions<
212
280
  TParentRoute extends AnyRoute,
213
281
  TRouteId,
214
282
  TAllParams,
215
283
  TSearchValidator extends AnySearchValidator,
216
284
  TLoaderData,
217
- TAllContext,
218
- TRouteContext,
219
285
  TLoaderDeps,
286
+ TRouterContext,
287
+ TRouteContextFn,
288
+ TBeforeLoadFn,
220
289
  > = {
221
290
  // test?: (args: TAllContext) => void
222
291
  // If true, this route will be matched as case-sensitive
@@ -232,6 +301,7 @@ export type UpdatableRouteOptions<
232
301
  pendingMinMs?: number
233
302
  staleTime?: number
234
303
  gcTime?: number
304
+ preload?: boolean
235
305
  preloadStaleTime?: number
236
306
  preloadGcTime?: number
237
307
  // Filter functions that can manipulate search params *before* they are passed to links and navigate
@@ -254,8 +324,12 @@ export type UpdatableRouteOptions<
254
324
  TAllParams,
255
325
  ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
256
326
  TLoaderData,
257
- TAllContext,
258
- TRouteContext,
327
+ ResolveAllContext<
328
+ TParentRoute,
329
+ TRouterContext,
330
+ TRouteContextFn,
331
+ TBeforeLoadFn
332
+ >,
259
333
  TLoaderDeps
260
334
  >,
261
335
  ) => void
@@ -265,8 +339,12 @@ export type UpdatableRouteOptions<
265
339
  TAllParams,
266
340
  ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
267
341
  TLoaderData,
268
- TAllContext,
269
- TRouteContext,
342
+ ResolveAllContext<
343
+ TParentRoute,
344
+ TRouterContext,
345
+ TRouteContextFn,
346
+ TBeforeLoadFn
347
+ >,
270
348
  TLoaderDeps
271
349
  >,
272
350
  ) => void
@@ -276,8 +354,12 @@ export type UpdatableRouteOptions<
276
354
  TAllParams,
277
355
  ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
278
356
  TLoaderData,
279
- TAllContext,
280
- TRouteContext,
357
+ ResolveAllContext<
358
+ TParentRoute,
359
+ TRouterContext,
360
+ TRouteContextFn,
361
+ TBeforeLoadFn
362
+ >,
281
363
  TLoaderDeps
282
364
  >,
283
365
  ) => void
@@ -288,8 +370,12 @@ export type UpdatableRouteOptions<
288
370
  TAllParams,
289
371
  ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
290
372
  TLoaderData,
291
- TAllContext,
292
- TRouteContext,
373
+ ResolveAllContext<
374
+ TParentRoute,
375
+ TRouterContext,
376
+ TRouteContextFn,
377
+ TBeforeLoadFn
378
+ >,
293
379
  TLoaderDeps
294
380
  >
295
381
  >
@@ -298,8 +384,12 @@ export type UpdatableRouteOptions<
298
384
  TAllParams,
299
385
  ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
300
386
  TLoaderData,
301
- TAllContext,
302
- TRouteContext,
387
+ ResolveAllContext<
388
+ TParentRoute,
389
+ TRouterContext,
390
+ TRouteContextFn,
391
+ TBeforeLoadFn
392
+ >,
303
393
  TLoaderDeps
304
394
  >
305
395
  params: TAllParams
@@ -372,24 +462,44 @@ export type DefaultSearchValidator = SearchValidator<
372
462
  >
373
463
 
374
464
  export type RouteLoaderFn<
375
- in out TAllParams = {},
376
- in out TLoaderDeps extends Record<string, any> = {},
377
- in out TAllContext = AnyContext,
465
+ in out TParentRoute extends AnyRoute = AnyRoute,
466
+ in out TParams = {},
467
+ in out TLoaderDeps = {},
468
+ in out TRouterContext = {},
469
+ in out TRouteContextFn = AnyContext,
470
+ in out TBeforeLoadFn = AnyContext,
378
471
  TLoaderData = undefined,
379
472
  > = (
380
- match: LoaderFnContext<TAllParams, TLoaderDeps, TAllContext>,
473
+ match: LoaderFnContext<
474
+ TParentRoute,
475
+ TParams,
476
+ TLoaderDeps,
477
+ TRouterContext,
478
+ TRouteContextFn,
479
+ TBeforeLoadFn
480
+ >,
381
481
  ) => TLoaderData | Promise<TLoaderData>
382
482
 
383
483
  export interface LoaderFnContext<
384
- in out TAllParams = {},
484
+ in out TParentRoute extends AnyRoute = AnyRoute,
485
+ in out TParams = {},
385
486
  in out TLoaderDeps = {},
386
- in out TAllContext = AnyContext,
487
+ in out TRouterContext = {},
488
+ in out TRouteContextFn = AnyContext,
489
+ in out TBeforeLoadFn = AnyContext,
387
490
  > {
388
491
  abortController: AbortController
389
492
  preload: boolean
390
- params: Expand<TAllParams>
493
+ params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>
391
494
  deps: TLoaderDeps
392
- context: TAllContext
495
+ context: Expand<
496
+ ResolveAllContext<
497
+ TParentRoute,
498
+ TRouterContext,
499
+ TRouteContextFn,
500
+ TBeforeLoadFn
501
+ >
502
+ >
393
503
  location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps
394
504
  /**
395
505
  * @deprecated Use `throw redirect({ to: '/somewhere' })` instead
@@ -434,13 +544,15 @@ export type InferAllParams<TRoute> = TRoute extends {
434
544
  ? TAllParams
435
545
  : {}
436
546
 
437
- export type InferAllContext<TRoute> = TRoute extends {
438
- types: {
439
- allContext: infer TAllContext
440
- }
441
- }
442
- ? TAllContext
443
- : {}
547
+ export type InferAllContext<TRoute> = unknown extends TRoute
548
+ ? TRoute
549
+ : TRoute extends {
550
+ types: {
551
+ allContext: infer TAllContext
552
+ }
553
+ }
554
+ ? TAllContext
555
+ : {}
444
556
 
445
557
  export type ResolveSearchSchemaFnInput<
446
558
  TSearchValidator extends AnySearchValidator,
@@ -490,16 +602,61 @@ export type ResolveFullSearchSchemaInput<
490
602
  ResolveSearchSchemaInput<TSearchValidator>
491
603
  >
492
604
 
493
- export type ResolveRouteContext<TRouteContextReturn> = [
494
- TRouteContextReturn,
495
- ] extends [never]
496
- ? RouteContext
497
- : TRouteContextReturn
605
+ export type LooseReturnType<T> = T extends (
606
+ ...args: Array<any>
607
+ ) => infer TReturn
608
+ ? TReturn
609
+ : never
610
+
611
+ export type LooseAsyncReturnType<T> = T extends (
612
+ ...args: Array<any>
613
+ ) => infer TReturn
614
+ ? TReturn extends Promise<infer TReturn>
615
+ ? TReturn
616
+ : TReturn
617
+ : never
618
+
619
+ export type ContextReturnType<TContextFn> = unknown extends TContextFn
620
+ ? TContextFn
621
+ : LooseReturnType<TContextFn> extends never
622
+ ? AnyContext
623
+ : LooseReturnType<TContextFn>
624
+
625
+ export type ContextAsyncReturnType<TContextFn> = unknown extends TContextFn
626
+ ? TContextFn
627
+ : LooseAsyncReturnType<TContextFn> extends never
628
+ ? AnyContext
629
+ : LooseAsyncReturnType<TContextFn>
630
+
631
+ export type RouteContextParameter<
632
+ TParentRoute extends AnyRoute,
633
+ TRouterContext,
634
+ > = unknown extends TParentRoute
635
+ ? TRouterContext
636
+ : Assign<TRouterContext, InferAllContext<TParentRoute>>
637
+
638
+ export type ResolveRouteContext<TRouteContextFn, TBeforeLoadFn> = Assign<
639
+ ContextReturnType<TRouteContextFn>,
640
+ ContextAsyncReturnType<TBeforeLoadFn>
641
+ >
642
+ export type BeforeLoadContextParameter<
643
+ TParentRoute extends AnyRoute,
644
+ TRouterContext,
645
+ TRouteContextFn,
646
+ > = Assign<
647
+ RouteContextParameter<TParentRoute, TRouterContext>,
648
+ ContextReturnType<TRouteContextFn>
649
+ >
498
650
 
499
651
  export type ResolveAllContext<
500
652
  TParentRoute extends AnyRoute,
501
- TRouteContext,
502
- > = Assign<InferAllContext<TParentRoute>, TRouteContext>
653
+ TRouterContext,
654
+ TRouteContextFn,
655
+ TBeforeLoadFn,
656
+ > = Assign<
657
+ BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>,
658
+ ContextAsyncReturnType<TBeforeLoadFn>
659
+ >
503
660
 
504
661
  export type ResolveLoaderData<TLoaderDataReturn> = [TLoaderDataReturn] extends [
505
662
  never,
@@ -526,23 +683,9 @@ export interface AnyRoute
526
683
  any
527
684
  > {}
528
685
 
529
- export type AnyRouteWithContext<TContext> = Route<
530
- any,
531
- any,
532
- any,
533
- any,
534
- any,
535
- any,
536
- any,
537
- any,
538
- any,
539
- any,
540
- any,
541
- any,
542
- TContext,
543
- any,
544
- any
545
- >
686
+ export type AnyRouteWithContext<TContext> = AnyRoute & {
687
+ types: { allContext: TContext }
688
+ }
546
689
 
547
690
  export type ResolveAllParamsFromParent<
548
691
  TParentRoute extends AnyRoute,
@@ -676,9 +819,9 @@ export class Route<
676
819
  in out TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
677
820
  in out TParams = Record<ParsePathParams<TPath>, string>,
678
821
  in out TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
679
- TRouteContextReturn = RouteContext,
680
- in out TRouteContext = ResolveRouteContext<TRouteContextReturn>,
681
- in out TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
822
+ in out TRouterContext = AnyContext,
823
+ in out TRouteContextFn = AnyContext,
824
+ in out TBeforeLoadFn = AnyContext,
682
825
  in out TLoaderDeps extends Record<string, any> = {},
683
826
  TLoaderDataReturn = {},
684
827
  in out TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -692,13 +835,12 @@ export class Route<
692
835
  TSearchValidator,
693
836
  TParams,
694
837
  TAllParams,
695
- TRouteContextReturn,
696
- TRouteContext,
697
- InferAllContext<TParentRoute>,
698
- TAllContext,
699
838
  TLoaderDeps,
700
839
  TLoaderDataReturn,
701
- TLoaderData
840
+ TLoaderData,
841
+ TRouterContext,
842
+ TRouteContextFn,
843
+ TBeforeLoadFn
702
844
  >
703
845
 
704
846
  // Set up in this.init()
@@ -728,13 +870,12 @@ export class Route<
728
870
  TSearchValidator,
729
871
  TParams,
730
872
  TAllParams,
731
- TRouteContextReturn,
732
- TRouteContext,
733
- InferAllContext<TParentRoute>,
734
- TAllContext,
735
873
  TLoaderDeps,
736
874
  TLoaderDataReturn,
737
- TLoaderData
875
+ TLoaderData,
876
+ TRouterContext,
877
+ TRouteContextFn,
878
+ TBeforeLoadFn
738
879
  >,
739
880
  ) {
740
881
  this.options = (options as any) || {}
@@ -764,8 +905,16 @@ export class Route<
764
905
  >
765
906
  params: TParams
766
907
  allParams: TAllParams
767
- routeContext: TRouteContext
768
- allContext: TAllContext
908
+ routerContext: TRouterContext
909
+ routeContext: ResolveRouteContext<TRouteContextFn, TBeforeLoadFn>
910
+ routeContextFn: TRouteContextFn
911
+ beforeLoadFn: TBeforeLoadFn
912
+ allContext: ResolveAllContext<
913
+ TParentRoute,
914
+ TRouterContext,
915
+ TRouteContextFn,
916
+ TBeforeLoadFn
917
+ >
769
918
  children: TChildren
770
919
  loaderData: TLoaderData
771
920
  loaderDeps: TLoaderDeps
@@ -782,13 +931,12 @@ export class Route<
782
931
  TSearchValidator,
783
932
  TParams,
784
933
  TAllParams,
785
- TRouteContextReturn,
786
- TRouteContext,
787
- InferAllContext<TParentRoute>,
788
- TAllContext,
789
934
  TLoaderDeps,
790
935
  TLoaderDataReturn,
791
- TLoaderData
936
+ TLoaderData,
937
+ TRouterContext,
938
+ TRouteContextFn,
939
+ TBeforeLoadFn
792
940
  > &
793
941
  RoutePathOptionsIntersection<TCustomId, TPath>)
794
942
  | undefined
@@ -796,7 +944,7 @@ export class Route<
796
944
  const isRoot = !options?.path && !options?.id
797
945
 
798
946
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
799
- this.parentRoute = this.options?.getParentRoute?.()
947
+ this.parentRoute = this.options.getParentRoute?.()
800
948
 
801
949
  if (isRoot) {
802
950
  this.path = rootRouteId as TPath
@@ -857,9 +1005,9 @@ export class Route<
857
1005
  TSearchValidator,
858
1006
  TParams,
859
1007
  TAllParams,
860
- TRouteContextReturn,
861
- TRouteContext,
862
- TAllContext,
1008
+ TRouterContext,
1009
+ TRouteContextFn,
1010
+ TBeforeLoadFn,
863
1011
  TLoaderDeps,
864
1012
  TLoaderDataReturn,
865
1013
  TLoaderData,
@@ -872,7 +1020,15 @@ export class Route<
872
1020
  }
873
1021
 
874
1022
  updateLoader = <TNewLoaderData = unknown>(options: {
875
- loader: RouteLoaderFn<TAllParams, TLoaderDeps, TAllContext, TNewLoaderData>
1023
+ loader: RouteLoaderFn<
1024
+ TParentRoute,
1025
+ TParams,
1026
+ TLoaderDeps,
1027
+ TRouterContext,
1028
+ TRouteContextFn,
1029
+ TBeforeLoadFn,
1030
+ TNewLoaderData
1031
+ >
876
1032
  }) => {
877
1033
  Object.assign(this.options, options)
878
1034
  return this as unknown as Route<
@@ -884,9 +1040,9 @@ export class Route<
884
1040
  TSearchValidator,
885
1041
  TParams,
886
1042
  TAllParams,
887
- TRouteContextReturn,
888
- TRouteContext,
889
- TAllContext,
1043
+ TRouterContext,
1044
+ TRouteContextFn,
1045
+ TBeforeLoadFn,
890
1046
  TLoaderDeps,
891
1047
  TNewLoaderData,
892
1048
  TChildren
@@ -900,9 +1056,10 @@ export class Route<
900
1056
  TAllParams,
901
1057
  TSearchValidator,
902
1058
  TLoaderData,
903
- TAllContext,
904
- TRouteContext,
905
- TLoaderDeps
1059
+ TLoaderDeps,
1060
+ TRouterContext,
1061
+ TRouteContextFn,
1062
+ TBeforeLoadFn
906
1063
  >,
907
1064
  ): this => {
908
1065
  Object.assign(this.options, options)
@@ -925,8 +1082,26 @@ export class Route<
925
1082
  return useMatch({ ...opts, from: this.id })
926
1083
  }
927
1084
 
928
- useRouteContext = <TSelected = Expand<TAllContext>>(opts?: {
929
- select?: (search: Expand<TAllContext>) => TSelected
1085
+ useRouteContext = <
1086
+ TSelected = Expand<
1087
+ ResolveAllContext<
1088
+ TParentRoute,
1089
+ TRouterContext,
1090
+ TRouteContextFn,
1091
+ TBeforeLoadFn
1092
+ >
1093
+ >,
1094
+ >(opts?: {
1095
+ select?: (
1096
+ search: Expand<
1097
+ ResolveAllContext<
1098
+ TParentRoute,
1099
+ TRouterContext,
1100
+ TRouteContextFn,
1101
+ TBeforeLoadFn
1102
+ >
1103
+ >,
1104
+ ) => TSelected
930
1105
  }): TSelected => {
931
1106
  return useMatch({
932
1107
  ...opts,
@@ -984,9 +1159,8 @@ export function createRoute<
984
1159
  TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
985
1160
  TParams = Record<ParsePathParams<TPath>, string>,
986
1161
  TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
987
- TRouteContextReturn = RouteContext,
988
- TRouteContext = ResolveRouteContext<TRouteContextReturn>,
989
- TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
1162
+ TRouteContextFn = AnyContext,
1163
+ TBeforeLoadFn = AnyContext,
990
1164
  TLoaderDeps extends Record<string, any> = {},
991
1165
  TLoaderDataReturn = {},
992
1166
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -999,13 +1173,12 @@ export function createRoute<
999
1173
  TSearchValidator,
1000
1174
  TParams,
1001
1175
  TAllParams,
1002
- TRouteContextReturn,
1003
- TRouteContext,
1004
- InferAllContext<TParentRoute>,
1005
- TAllContext,
1006
1176
  TLoaderDeps,
1007
1177
  TLoaderDataReturn,
1008
- TLoaderData
1178
+ TLoaderData,
1179
+ AnyContext,
1180
+ TRouteContextFn,
1181
+ TBeforeLoadFn
1009
1182
  >,
1010
1183
  ) {
1011
1184
  return new Route<
@@ -1017,9 +1190,9 @@ export function createRoute<
1017
1190
  TSearchValidator,
1018
1191
  TParams,
1019
1192
  TAllParams,
1020
- TRouteContextReturn,
1021
- TRouteContext,
1022
- TAllContext,
1193
+ AnyContext,
1194
+ TRouteContextFn,
1195
+ TBeforeLoadFn,
1023
1196
  TLoaderDeps,
1024
1197
  TLoaderDataReturn,
1025
1198
  TLoaderData,
@@ -1031,9 +1204,9 @@ export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any, any>
1031
1204
 
1032
1205
  export type RootRouteOptions<
1033
1206
  TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
1034
- TRouteContextReturn = RouteContext,
1035
- TRouteContext = ResolveRouteContext<TRouteContextReturn>,
1036
1207
  TRouterContext = {},
1208
+ TRouteContextFn = AnyContext,
1209
+ TBeforeLoadFn = AnyContext,
1037
1210
  TLoaderDeps extends Record<string, any> = {},
1038
1211
  TLoaderDataReturn = {},
1039
1212
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -1045,13 +1218,12 @@ export type RootRouteOptions<
1045
1218
  TSearchValidator,
1046
1219
  {}, // TParams
1047
1220
  {}, // TAllParams
1048
- TRouteContextReturn, // TRouteContextReturn
1049
- TRouteContext, // TRouteContext
1050
- TRouterContext, // TParentAllContext
1051
- Assign<TRouterContext, TRouteContext>, // TAllContext
1052
1221
  TLoaderDeps,
1053
1222
  TLoaderDataReturn, // TLoaderDataReturn,
1054
- TLoaderData // TLoaderData,
1223
+ TLoaderData, // TLoaderData,
1224
+ TRouterContext,
1225
+ TRouteContextFn,
1226
+ TBeforeLoadFn
1055
1227
  >,
1056
1228
  | 'path'
1057
1229
  | 'id'
@@ -1064,19 +1236,18 @@ export type RootRouteOptions<
1064
1236
 
1065
1237
  export function createRootRouteWithContext<TRouterContext extends {}>() {
1066
1238
  return <
1239
+ TRouteContextFn = AnyContext,
1240
+ TBeforeLoadFn = AnyContext,
1067
1241
  TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
1068
- TRouteContextReturn extends RouteContext = RouteContext,
1069
- TRouteContext extends
1070
- RouteContext = ResolveRouteContext<TRouteContextReturn>,
1071
1242
  TLoaderDeps extends Record<string, any> = {},
1072
1243
  TLoaderDataReturn = {},
1073
1244
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
1074
1245
  >(
1075
1246
  options?: RootRouteOptions<
1076
1247
  TSearchValidator,
1077
- TRouteContextReturn,
1078
- TRouteContext,
1079
1248
  TRouterContext,
1249
+ TRouteContextFn,
1250
+ TBeforeLoadFn,
1080
1251
  TLoaderDeps,
1081
1252
  TLoaderDataReturn,
1082
1253
  TLoaderData
@@ -1084,9 +1255,9 @@ export function createRootRouteWithContext<TRouterContext extends {}>() {
1084
1255
  ) => {
1085
1256
  return createRootRoute<
1086
1257
  TSearchValidator,
1087
- TRouteContextReturn,
1088
- TRouteContext,
1089
1258
  TRouterContext,
1259
+ TRouteContextFn,
1260
+ TBeforeLoadFn,
1090
1261
  TLoaderDeps,
1091
1262
  TLoaderData
1092
1263
  >(options as any)
@@ -1100,9 +1271,9 @@ export const rootRouteWithContext = createRootRouteWithContext
1100
1271
 
1101
1272
  export class RootRoute<
1102
1273
  in out TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
1103
- TRouteContextReturn = RouteContext,
1104
- in out TRouteContext = ResolveRouteContext<TRouteContextReturn>,
1105
1274
  in out TRouterContext = {},
1275
+ in out TRouteContextFn = AnyContext,
1276
+ in out TBeforeLoadFn = AnyContext,
1106
1277
  TLoaderDeps extends Record<string, any> = {},
1107
1278
  TLoaderDataReturn = {},
1108
1279
  in out TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -1116,9 +1287,9 @@ export class RootRoute<
1116
1287
  TSearchValidator, // TSearchValidator
1117
1288
  {}, // TParams
1118
1289
  {}, // TAllParams
1119
- TRouteContextReturn, // TRouteContextReturn
1120
- TRouteContext, // TRouteContext
1121
- Assign<TRouterContext, TRouteContext>, // TAllContext
1290
+ TRouterContext,
1291
+ TRouteContextFn,
1292
+ TBeforeLoadFn,
1122
1293
  TLoaderDeps,
1123
1294
  TLoaderDataReturn,
1124
1295
  TLoaderData,
@@ -1130,9 +1301,9 @@ export class RootRoute<
1130
1301
  constructor(
1131
1302
  options?: RootRouteOptions<
1132
1303
  TSearchValidator,
1133
- TRouteContextReturn,
1134
- TRouteContext,
1135
1304
  TRouterContext,
1305
+ TRouteContextFn,
1306
+ TBeforeLoadFn,
1136
1307
  TLoaderDeps,
1137
1308
  TLoaderDataReturn,
1138
1309
  TLoaderData
@@ -1149,9 +1320,9 @@ export class RootRoute<
1149
1320
  children: TNewChildren,
1150
1321
  ): RootRoute<
1151
1322
  TSearchValidator,
1152
- TRouteContextReturn,
1153
- TRouteContext,
1154
1323
  TRouterContext,
1324
+ TRouteContextFn,
1325
+ TBeforeLoadFn,
1155
1326
  TLoaderDeps,
1156
1327
  TLoaderDataReturn,
1157
1328
  TLoaderData,
@@ -1163,18 +1334,18 @@ export class RootRoute<
1163
1334
 
1164
1335
  export function createRootRoute<
1165
1336
  TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
1166
- TRouteContextReturn = RouteContext,
1167
- TRouteContext = ResolveRouteContext<TRouteContextReturn>,
1168
1337
  TRouterContext = {},
1338
+ TRouteContextFn = AnyContext,
1339
+ TBeforeLoadFn = AnyContext,
1169
1340
  TLoaderDeps extends Record<string, any> = {},
1170
1341
  TLoaderDataReturn = {},
1171
1342
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
1172
1343
  >(
1173
1344
  options?: RootRouteOptions<
1174
1345
  TSearchValidator,
1175
- TRouteContextReturn,
1176
- TRouteContext,
1177
1346
  TRouterContext,
1347
+ TRouteContextFn,
1348
+ TBeforeLoadFn,
1178
1349
  TLoaderDeps,
1179
1350
  TLoaderDataReturn,
1180
1351
  TLoaderData
@@ -1182,9 +1353,9 @@ export function createRootRoute<
1182
1353
  ) {
1183
1354
  return new RootRoute<
1184
1355
  TSearchValidator,
1185
- TRouteContextReturn,
1186
- TRouteContext,
1187
1356
  TRouterContext,
1357
+ TRouteContextFn,
1358
+ TBeforeLoadFn,
1188
1359
  TLoaderDeps,
1189
1360
  TLoaderDataReturn,
1190
1361
  TLoaderData
@@ -1289,10 +1460,10 @@ export type NotFoundRouteComponent = SyncRouteComponent<NotFoundRouteProps>
1289
1460
 
1290
1461
  export class NotFoundRoute<
1291
1462
  TParentRoute extends AnyRootRoute,
1463
+ TRouterContext = AnyContext,
1464
+ TRouteContextFn = AnyContext,
1465
+ TBeforeLoadFn = AnyContext,
1292
1466
  TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
1293
- TRouteContextReturn = AnyContext,
1294
- TRouteContext = RouteContext,
1295
- TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
1296
1467
  TLoaderDeps extends Record<string, any> = {},
1297
1468
  TLoaderDataReturn = {},
1298
1469
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -1306,9 +1477,9 @@ export class NotFoundRoute<
1306
1477
  TSearchValidator,
1307
1478
  {},
1308
1479
  {},
1309
- TRouteContextReturn,
1310
- TRouteContext,
1311
- TAllContext,
1480
+ TRouterContext,
1481
+ TRouteContextFn,
1482
+ TBeforeLoadFn,
1312
1483
  TLoaderDeps,
1313
1484
  TLoaderDataReturn,
1314
1485
  TLoaderData,
@@ -1323,13 +1494,12 @@ export class NotFoundRoute<
1323
1494
  TSearchValidator,
1324
1495
  {},
1325
1496
  {},
1326
- TRouteContextReturn,
1327
- TRouteContext,
1328
- InferAllContext<TParentRoute>,
1329
- TAllContext,
1330
1497
  TLoaderDeps,
1331
1498
  TLoaderDataReturn,
1332
- TLoaderData
1499
+ TLoaderData,
1500
+ TRouterContext,
1501
+ TRouteContextFn,
1502
+ TBeforeLoadFn
1333
1503
  >,
1334
1504
  | 'caseSensitive'
1335
1505
  | 'parseParams'