@tanstack/react-router 1.9.0 → 1.10.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/route.ts CHANGED
@@ -9,15 +9,9 @@ import { RouteById, RouteIds, RoutePaths } from './routeInfo'
9
9
  import { AnyRouter, RegisteredRouter } from './router'
10
10
  import { useParams } from './useParams'
11
11
  import { useSearch } from './useSearch'
12
- import {
13
- Assign,
14
- Expand,
15
- IsAny,
16
- NoInfer,
17
- PickRequired,
18
- UnionToIntersection,
19
- } from './utils'
12
+ import { Assign, Expand, IsAny, NoInfer, UnionToIntersection } from './utils'
20
13
  import { BuildLocationFn, NavigateFn } from './RouterProvider'
14
+ import { LazyRoute } from '.'
21
15
 
22
16
  export const rootRouteId = '__root__' as const
23
17
  export type RootRouteId = typeof rootRouteId
@@ -81,7 +75,7 @@ export type RouteOptions<
81
75
  TLoaderDeps,
82
76
  TLoaderData
83
77
  > &
84
- UpdatableRouteOptions<NoInfer<TFullSearchSchema>>
78
+ UpdatableRouteOptions<NoInfer<TFullSearchSchema>, NoInfer<TLoaderData>>
85
79
 
86
80
  export type ParamsFallback<
87
81
  TPath extends string,
@@ -178,6 +172,7 @@ type BeforeLoadFn<
178
172
 
179
173
  export type UpdatableRouteOptions<
180
174
  TFullSearchSchema extends Record<string, any>,
175
+ TLoaderData extends any,
181
176
  > = {
182
177
  // test?: (args: TAllContext) => void
183
178
  // If true, this route will be matched as case-sensitive
@@ -188,6 +183,7 @@ export type UpdatableRouteOptions<
188
183
  component?: RouteComponent
189
184
  errorComponent?: false | null | ErrorRouteComponent
190
185
  pendingComponent?: RouteComponent
186
+ lazy?: () => Promise<LazyRoute<any>>
191
187
  pendingMs?: number
192
188
  pendingMinMs?: number
193
189
  staleTime?: number
@@ -206,8 +202,30 @@ export type UpdatableRouteOptions<
206
202
  onEnter?: (match: AnyRouteMatch) => void
207
203
  onStay?: (match: AnyRouteMatch) => void
208
204
  onLeave?: (match: AnyRouteMatch) => void
205
+ meta?: (ctx: { loaderData: TLoaderData }) => JSX.IntrinsicElements['meta'][]
206
+ links?: () => JSX.IntrinsicElements['link'][]
207
+ scripts?: () => JSX.IntrinsicElements['script'][]
209
208
  }
210
209
 
210
+ export type MetaDescriptor =
211
+ | { charSet: 'utf-8' }
212
+ | { title: string }
213
+ | { name: string; content: string }
214
+ | { property: string; content: string }
215
+ | { httpEquiv: string; content: string }
216
+ | { 'script:ld+json': LdJsonObject }
217
+ | { tagName: 'meta' | 'link'; [name: string]: string }
218
+ | { [name: string]: unknown }
219
+
220
+ type LdJsonObject = { [Key in string]: LdJsonValue } & {
221
+ [Key in string]?: LdJsonValue | undefined
222
+ }
223
+ type LdJsonArray = LdJsonValue[] | readonly LdJsonValue[]
224
+ type LdJsonPrimitive = string | number | boolean | null
225
+ type LdJsonValue = LdJsonPrimitive | LdJsonObject | LdJsonArray
226
+
227
+ export type RouteLinkEntry = {}
228
+
211
229
  export type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn<
212
230
  TPath,
213
231
  TParams
@@ -455,6 +473,32 @@ export type RouteConstraints = {
455
473
  // }
456
474
  // }
457
475
 
476
+ export function getRouteApi<
477
+ TId extends RouteIds<RegisteredRouter['routeTree']>,
478
+ TRoute extends AnyRoute = RouteById<RegisteredRouter['routeTree'], TId>,
479
+ TFullSearchSchema extends Record<
480
+ string,
481
+ any
482
+ > = TRoute['types']['fullSearchSchema'],
483
+ TAllParams extends AnyPathParams = TRoute['types']['allParams'],
484
+ TAllContext extends Record<string, any> = TRoute['types']['allContext'],
485
+ TLoaderDeps extends Record<string, any> = TRoute['types']['loaderDeps'],
486
+ TLoaderData extends any = TRoute['types']['loaderData'],
487
+ >(id: TId) {
488
+ return new RouteApi<
489
+ TId,
490
+ TRoute,
491
+ TFullSearchSchema,
492
+ TAllParams,
493
+ TAllContext,
494
+ TLoaderDeps,
495
+ TLoaderData
496
+ >({ id })
497
+ }
498
+
499
+ /**
500
+ * @deprecated Use the `getRouteApi` function instead.
501
+ */
458
502
  export class RouteApi<
459
503
  TId extends RouteIds<RegisteredRouter['routeTree']>,
460
504
  TRoute extends AnyRoute = RouteById<RegisteredRouter['routeTree'], TId>,
@@ -513,6 +557,9 @@ export class RouteApi<
513
557
  }
514
558
  }
515
559
 
560
+ /**
561
+ * @deprecated Use the `createRoute` function instead.
562
+ */
516
563
  export class Route<
517
564
  TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
518
565
  TPath extends RouteConstraints['TPath'] = '/',
@@ -792,7 +839,7 @@ export class Route<
792
839
  >
793
840
  }
794
841
 
795
- update = (options: UpdatableRouteOptions<TFullSearchSchema>) => {
842
+ update = (options: UpdatableRouteOptions<TFullSearchSchema, TLoaderData>) => {
796
843
  Object.assign(this.options, options)
797
844
  return this
798
845
  }
@@ -838,6 +885,103 @@ export class Route<
838
885
  }
839
886
  }
840
887
 
888
+ export function createRoute<
889
+ TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
890
+ TPath extends RouteConstraints['TPath'] = '/',
891
+ TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
892
+ TParentRoute,
893
+ TPath
894
+ >,
895
+ TCustomId extends RouteConstraints['TCustomId'] = string,
896
+ TId extends RouteConstraints['TId'] = ResolveId<
897
+ TParentRoute,
898
+ TCustomId,
899
+ TPath
900
+ >,
901
+ TSearchSchemaInput extends RouteConstraints['TSearchSchema'] = {},
902
+ TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
903
+ TSearchSchemaUsed extends Record<
904
+ string,
905
+ any
906
+ > = TSearchSchemaInput extends SearchSchemaInput
907
+ ? Omit<TSearchSchemaInput, keyof SearchSchemaInput>
908
+ : TSearchSchema,
909
+ TFullSearchSchemaInput extends Record<
910
+ string,
911
+ any
912
+ > = ResolveFullSearchSchemaInput<TParentRoute, TSearchSchemaUsed>,
913
+ TFullSearchSchema extends
914
+ RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
915
+ TParentRoute,
916
+ TSearchSchema
917
+ >,
918
+ TParams extends RouteConstraints['TParams'] = Expand<
919
+ Record<ParsePathParams<TPath>, string>
920
+ >,
921
+ TAllParams extends RouteConstraints['TAllParams'] = ResolveAllParams<
922
+ TParentRoute,
923
+ TParams
924
+ >,
925
+ TRouteContextReturn extends RouteConstraints['TRouteContext'] = RouteContext,
926
+ TRouteContext extends RouteConstraints['TRouteContext'] = [
927
+ TRouteContextReturn,
928
+ ] extends [never]
929
+ ? RouteContext
930
+ : TRouteContextReturn,
931
+ TAllContext extends Expand<
932
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
933
+ > = Expand<
934
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
935
+ >,
936
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
937
+ TLoaderDeps extends Record<string, any> = {},
938
+ TLoaderData extends any = unknown,
939
+ TChildren extends RouteConstraints['TChildren'] = unknown,
940
+ TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
941
+ >(
942
+ options: RouteOptions<
943
+ TParentRoute,
944
+ TCustomId,
945
+ TPath,
946
+ TSearchSchemaInput,
947
+ TSearchSchema,
948
+ TSearchSchemaUsed,
949
+ TFullSearchSchemaInput,
950
+ TFullSearchSchema,
951
+ TParams,
952
+ TAllParams,
953
+ TRouteContextReturn,
954
+ TRouteContext,
955
+ TRouterContext,
956
+ TAllContext,
957
+ TLoaderDeps,
958
+ TLoaderData
959
+ >,
960
+ ) {
961
+ return new Route<
962
+ TParentRoute,
963
+ TPath,
964
+ TFullPath,
965
+ TCustomId,
966
+ TId,
967
+ TSearchSchemaInput,
968
+ TSearchSchema,
969
+ TSearchSchemaUsed,
970
+ TFullSearchSchemaInput,
971
+ TFullSearchSchema,
972
+ TParams,
973
+ TAllParams,
974
+ TRouteContextReturn,
975
+ TRouteContext,
976
+ TAllContext,
977
+ TRouterContext,
978
+ TLoaderDeps,
979
+ TLoaderData,
980
+ TChildren,
981
+ TRouteTree
982
+ >(options)
983
+ }
984
+
841
985
  export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any, any>
842
986
 
843
987
  export function rootRouteWithContext<TRouterContext extends {}>() {
@@ -878,15 +1022,15 @@ export function rootRouteWithContext<TRouterContext extends {}>() {
878
1022
  | 'parseParams'
879
1023
  | 'stringifyParams'
880
1024
  >,
881
- ): RootRoute<
882
- TSearchSchemaInput,
883
- TSearchSchema,
884
- TSearchSchemaUsed,
885
- TRouteContextReturn,
886
- TRouteContext,
887
- TRouterContext
888
- > => {
889
- return new RootRoute(options) as any
1025
+ ) => {
1026
+ return createRootRoute<
1027
+ TSearchSchemaInput,
1028
+ TSearchSchema,
1029
+ TSearchSchemaUsed,
1030
+ TRouteContextReturn,
1031
+ TRouteContext,
1032
+ TRouterContext
1033
+ >(options as any)
890
1034
  }
891
1035
  }
892
1036
 
@@ -894,6 +1038,9 @@ export type RootSearchSchema = {
894
1038
  __TRootSearchSchema__: '__TRootSearchSchema__'
895
1039
  }
896
1040
 
1041
+ /**
1042
+ * @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead.
1043
+ */
897
1044
  export class RootRoute<
898
1045
  TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
899
1046
  TSearchSchema extends Record<string, any> = RootSearchSchema,
@@ -959,6 +1106,57 @@ export class RootRoute<
959
1106
  }
960
1107
  }
961
1108
 
1109
+ export function createRootRoute<
1110
+ TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
1111
+ TSearchSchema extends Record<string, any> = RootSearchSchema,
1112
+ TSearchSchemaUsed extends Record<string, any> = RootSearchSchema,
1113
+ TRouteContextReturn extends RouteContext = RouteContext,
1114
+ TRouteContext extends RouteContext = [TRouteContextReturn] extends [never]
1115
+ ? RouteContext
1116
+ : TRouteContextReturn,
1117
+ TRouterContext extends {} = {},
1118
+ TLoaderDeps extends Record<string, any> = {},
1119
+ TLoaderData extends any = unknown,
1120
+ >(
1121
+ options?: Omit<
1122
+ RouteOptions<
1123
+ AnyRoute, // TParentRoute
1124
+ RootRouteId, // TCustomId
1125
+ '', // TPath
1126
+ TSearchSchemaInput, // TSearchSchemaInput
1127
+ TSearchSchema, // TSearchSchema
1128
+ TSearchSchemaUsed,
1129
+ TSearchSchemaUsed, // TFullSearchSchemaInput
1130
+ TSearchSchema, // TFullSearchSchema
1131
+ {}, // TParams
1132
+ {}, // TAllParams
1133
+ TRouteContextReturn, // TRouteContextReturn
1134
+ TRouteContext, // TRouteContext
1135
+ TRouterContext,
1136
+ Assign<TRouterContext, TRouteContext>, // TAllContext
1137
+ TLoaderDeps,
1138
+ TLoaderData
1139
+ >,
1140
+ | 'path'
1141
+ | 'id'
1142
+ | 'getParentRoute'
1143
+ | 'caseSensitive'
1144
+ | 'parseParams'
1145
+ | 'stringifyParams'
1146
+ >,
1147
+ ) {
1148
+ return new RootRoute<
1149
+ TSearchSchemaInput,
1150
+ TSearchSchema,
1151
+ TSearchSchemaUsed,
1152
+ TRouteContextReturn,
1153
+ TRouteContext,
1154
+ TRouterContext,
1155
+ TLoaderDeps,
1156
+ TLoaderData
1157
+ >(options)
1158
+ }
1159
+
962
1160
  export type ResolveFullPath<
963
1161
  TParentRoute extends AnyRoute,
964
1162
  TPath extends string,
@@ -1019,10 +1217,18 @@ export function createRouteMask<
1019
1217
  return opts as any
1020
1218
  }
1021
1219
 
1220
+ /**
1221
+ * @deprecated Use `ErrorComponentProps` instead.
1222
+ */
1022
1223
  export type ErrorRouteProps = {
1023
1224
  error: unknown
1024
1225
  info: { componentStack: string }
1025
1226
  }
1227
+
1228
+ export type ErrorComponentProps = {
1229
+ error: unknown
1230
+ info: { componentStack: string }
1231
+ }
1026
1232
  //
1027
1233
 
1028
1234
  export type ReactNode = any
@@ -1038,7 +1244,7 @@ export type AsyncRouteComponent<TProps> = SyncRouteComponent<TProps> & {
1038
1244
  export type RouteComponent<TProps = any> = SyncRouteComponent<TProps> &
1039
1245
  AsyncRouteComponent<TProps>
1040
1246
 
1041
- export type ErrorRouteComponent = RouteComponent<ErrorRouteProps>
1247
+ export type ErrorRouteComponent = RouteComponent<ErrorComponentProps>
1042
1248
 
1043
1249
  export class NotFoundRoute<
1044
1250
  TParentRoute extends AnyRootRoute,
package/src/router.ts CHANGED
@@ -176,7 +176,7 @@ export interface DehydratedRouterState {
176
176
 
177
177
  export type DehydratedRouteMatch = Pick<
178
178
  RouteMatch,
179
- 'id' | 'status' | 'updatedAt'
179
+ 'id' | 'status' | 'updatedAt' | 'loaderData'
180
180
  >
181
181
 
182
182
  export interface DehydratedRouter {
@@ -224,6 +224,19 @@ export type RouterListener<TRouterEvent extends RouterEvent> = {
224
224
  fn: ListenerFn<TRouterEvent>
225
225
  }
226
226
 
227
+ export function createRouter<
228
+ TRouteTree extends AnyRoute = AnyRoute,
229
+ TDehydrated extends Record<string, any> = Record<string, any>,
230
+ TSerializedError extends Record<string, any> = Record<string, any>,
231
+ >(
232
+ options: RouterConstructorOptions<TRouteTree, TDehydrated, TSerializedError>,
233
+ ) {
234
+ return new Router<TRouteTree, TDehydrated, TSerializedError>(options)
235
+ }
236
+
237
+ /**
238
+ * @deprecated Use the `createRouter` function instead
239
+ */
227
240
  export class Router<
228
241
  TRouteTree extends AnyRoute = AnyRoute,
229
242
  TDehydrated extends Record<string, any> = Record<string, any>,
@@ -691,6 +704,7 @@ export class Router<
691
704
  // Create a fresh route match
692
705
  const hasLoaders = !!(
693
706
  route.options.loader ||
707
+ route.options.lazy ||
694
708
  componentTypes.some((d) => (route.options[d] as any)?.preload)
695
709
  )
696
710
 
@@ -1142,6 +1156,9 @@ export class Router<
1142
1156
  ...beforeLoadContext,
1143
1157
  }
1144
1158
 
1159
+ const links = route.options.links?.()
1160
+ const scripts = route.options.scripts?.()
1161
+
1145
1162
  matches[index] = match = {
1146
1163
  ...match,
1147
1164
  routeContext: replaceEqualDeep(
@@ -1151,6 +1168,8 @@ export class Router<
1151
1168
  context: replaceEqualDeep(match.context, context),
1152
1169
  abortController,
1153
1170
  pendingPromise,
1171
+ links,
1172
+ scripts,
1154
1173
  }
1155
1174
  } catch (err) {
1156
1175
  handleErrorAndRedirect(err, 'BEFORE_LOAD')
@@ -1234,21 +1253,33 @@ export class Router<
1234
1253
  fetchCount: match.fetchCount + 1,
1235
1254
  }
1236
1255
 
1237
- const componentsPromise = Promise.all(
1238
- componentTypes.map(async (type) => {
1239
- const component = route.options[type]
1240
-
1241
- if ((component as any)?.preload) {
1242
- await (component as any).preload()
1243
- }
1244
- }),
1256
+ const lazyPromise =
1257
+ route.options.lazy?.().then((lazyRoute) => {
1258
+ Object.assign(route.options, lazyRoute.options)
1259
+ }) || Promise.resolve()
1260
+
1261
+ // If for some reason lazy resolves more lazy components...
1262
+ // We'll wait for that before pre attempt to preload any
1263
+ // components themselves.
1264
+ const componentsPromise = lazyPromise.then(() =>
1265
+ Promise.all(
1266
+ componentTypes.map(async (type) => {
1267
+ const component = route.options[type]
1268
+
1269
+ if ((component as any)?.preload) {
1270
+ await (component as any).preload()
1271
+ }
1272
+ }),
1273
+ ),
1245
1274
  )
1246
1275
 
1276
+ // Kick off the loader!
1247
1277
  const loaderPromise = route.options.loader?.(loaderContext)
1248
1278
 
1249
1279
  loadPromise = Promise.all([
1250
1280
  componentsPromise,
1251
1281
  loaderPromise,
1282
+ lazyPromise,
1252
1283
  ]).then((d) => d[1])
1253
1284
  }
1254
1285
 
@@ -1273,6 +1304,10 @@ export class Router<
1273
1304
 
1274
1305
  if ((latestPromise = checkLatest())) return await latestPromise
1275
1306
 
1307
+ const meta = route.options.meta?.({
1308
+ loaderData,
1309
+ })
1310
+
1276
1311
  matches[index] = match = {
1277
1312
  ...match,
1278
1313
  error: undefined,
@@ -1281,6 +1316,7 @@ export class Router<
1281
1316
  updatedAt: Date.now(),
1282
1317
  loaderData,
1283
1318
  loadPromise: undefined,
1319
+ meta,
1284
1320
  }
1285
1321
  } catch (error) {
1286
1322
  if ((latestPromise = checkLatest())) return await latestPromise
@@ -1704,9 +1740,16 @@ export class Router<
1704
1740
  )
1705
1741
 
1706
1742
  if (dehydratedMatch) {
1743
+ const route = this.looseRoutesById[match.routeId]!
1744
+
1707
1745
  return {
1708
1746
  ...match,
1709
1747
  ...dehydratedMatch,
1748
+ meta: route.options.meta?.({
1749
+ loaderData: dehydratedMatch.loaderData,
1750
+ }),
1751
+ links: route.options.links?.(),
1752
+ scripts: route.options.scripts?.(),
1710
1753
  }
1711
1754
  }
1712
1755
  return match
@@ -1716,6 +1759,7 @@ export class Router<
1716
1759
  return {
1717
1760
  ...s,
1718
1761
  matches: matches as any,
1762
+ lastUpdated: Date.now(),
1719
1763
  }
1720
1764
  })
1721
1765
  }
@@ -1758,7 +1802,7 @@ export function getInitialRouterState(
1758
1802
  matches: [],
1759
1803
  pendingMatches: [],
1760
1804
  cachedMatches: [],
1761
- lastUpdated: Date.now(),
1805
+ lastUpdated: 0,
1762
1806
  }
1763
1807
  }
1764
1808