@tanstack/router-core 0.0.1-beta.176 → 0.0.1-beta.178
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/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js +27 -43
- package/build/cjs/router.js.map +1 -1
- package/build/esm/index.js +27 -43
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +124 -124
- package/build/types/fileRoute.d.ts +0 -1
- package/build/types/route.d.ts +11 -10
- package/build/types/router.d.ts +1 -2
- package/build/umd/index.development.js +27 -43
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/route.ts +36 -48
- package/src/router.ts +33 -50
package/package.json
CHANGED
package/src/route.ts
CHANGED
|
@@ -254,7 +254,6 @@ export type BaseRouteOptions<
|
|
|
254
254
|
TRouteContext extends RouteContext = RouteContext,
|
|
255
255
|
TAllContext extends AnyContext = AnyContext,
|
|
256
256
|
> = RoutePathOptions<TCustomId, TPath> & {
|
|
257
|
-
layoutLimit?: string
|
|
258
257
|
getParentRoute: () => TParentRoute
|
|
259
258
|
validateSearch?: SearchSchemaValidator<TSearchSchema>
|
|
260
259
|
loader?: LoaderFn<
|
|
@@ -265,7 +264,34 @@ export type BaseRouteOptions<
|
|
|
265
264
|
NoInfer<TRouteContext>,
|
|
266
265
|
TAllContext
|
|
267
266
|
>
|
|
268
|
-
} & (
|
|
267
|
+
} & (keyof PickRequired<RouteContext> extends never
|
|
268
|
+
? // This async function is called before a route is loaded.
|
|
269
|
+
// If an error is thrown here, the route's loader will not be called.
|
|
270
|
+
// If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
|
|
271
|
+
// If thrown during a preload event, the error will be logged to the console.
|
|
272
|
+
{
|
|
273
|
+
beforeLoad?: BeforeLoadFn<
|
|
274
|
+
TParentRoute,
|
|
275
|
+
TAllParams,
|
|
276
|
+
TSearchSchema,
|
|
277
|
+
TFullSearchSchema,
|
|
278
|
+
TParentContext,
|
|
279
|
+
TAllParentContext,
|
|
280
|
+
TRouteContext
|
|
281
|
+
>
|
|
282
|
+
}
|
|
283
|
+
: {
|
|
284
|
+
beforeLoad: BeforeLoadFn<
|
|
285
|
+
TParentRoute,
|
|
286
|
+
TAllParams,
|
|
287
|
+
TSearchSchema,
|
|
288
|
+
TFullSearchSchema,
|
|
289
|
+
TParentContext,
|
|
290
|
+
TAllParentContext,
|
|
291
|
+
TRouteContext
|
|
292
|
+
>
|
|
293
|
+
}) &
|
|
294
|
+
([TLoader] extends [never]
|
|
269
295
|
? {
|
|
270
296
|
loader: 'Loaders must return a type other than never. If you are throwing a redirect() and not returning anything, return a redirect() instead.'
|
|
271
297
|
}
|
|
@@ -286,32 +312,12 @@ export type BaseRouteOptions<
|
|
|
286
312
|
stringifyParams?: never
|
|
287
313
|
parseParams?: never
|
|
288
314
|
}
|
|
289
|
-
)
|
|
290
|
-
(keyof PickRequired<RouteContext> extends never
|
|
291
|
-
? {
|
|
292
|
-
getContext?: GetContextFn<
|
|
293
|
-
TParentRoute,
|
|
294
|
-
TAllParams,
|
|
295
|
-
TFullSearchSchema,
|
|
296
|
-
TParentContext,
|
|
297
|
-
TAllParentContext,
|
|
298
|
-
TRouteContext
|
|
299
|
-
>
|
|
300
|
-
}
|
|
301
|
-
: {
|
|
302
|
-
getContext: GetContextFn<
|
|
303
|
-
TParentRoute,
|
|
304
|
-
TAllParams,
|
|
305
|
-
TFullSearchSchema,
|
|
306
|
-
TParentContext,
|
|
307
|
-
TAllParentContext,
|
|
308
|
-
TRouteContext
|
|
309
|
-
>
|
|
310
|
-
})
|
|
315
|
+
)
|
|
311
316
|
|
|
312
|
-
type
|
|
317
|
+
type BeforeLoadFn<
|
|
313
318
|
TParentRoute,
|
|
314
319
|
TAllParams,
|
|
320
|
+
TSearchSchema,
|
|
315
321
|
TFullSearchSchema,
|
|
316
322
|
TParentContext,
|
|
317
323
|
TAllParentContext,
|
|
@@ -319,7 +325,10 @@ type GetContextFn<
|
|
|
319
325
|
> = (
|
|
320
326
|
opts: {
|
|
321
327
|
params: TAllParams
|
|
328
|
+
routeSearch: TSearchSchema
|
|
322
329
|
search: TFullSearchSchema
|
|
330
|
+
abortController: AbortController
|
|
331
|
+
preload: boolean
|
|
323
332
|
} & (TParentRoute extends undefined
|
|
324
333
|
? {
|
|
325
334
|
context?: TAllParentContext
|
|
@@ -329,7 +338,7 @@ type GetContextFn<
|
|
|
329
338
|
context: TAllParentContext
|
|
330
339
|
parentContext: TParentContext
|
|
331
340
|
}),
|
|
332
|
-
) => TRouteContext
|
|
341
|
+
) => Promise<TRouteContext> | TRouteContext | void
|
|
333
342
|
|
|
334
343
|
export type UpdatableRouteOptions<
|
|
335
344
|
TLoader,
|
|
@@ -378,19 +387,6 @@ export type UpdatableRouteOptions<
|
|
|
378
387
|
maxAge?: number
|
|
379
388
|
// If set, a match of this route that becomes inactive (or unused) will be garbage collected after this many milliseconds
|
|
380
389
|
gcMaxAge?: number
|
|
381
|
-
// This async function is called before a route is loaded.
|
|
382
|
-
// If an error is thrown here, the route's loader will not be called.
|
|
383
|
-
// If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
|
|
384
|
-
// If thrown during a preload event, the error will be logged to the console.
|
|
385
|
-
beforeLoad?: (
|
|
386
|
-
opts: LoaderContext<
|
|
387
|
-
TSearchSchema,
|
|
388
|
-
TFullSearchSchema,
|
|
389
|
-
TAllParams,
|
|
390
|
-
NoInfer<TRouteContext>,
|
|
391
|
-
TAllContext
|
|
392
|
-
>,
|
|
393
|
-
) => Promise<void> | void
|
|
394
390
|
onError?: (err: any) => void
|
|
395
391
|
// These functions are called as route matches are loaded, stick around and leave the active
|
|
396
392
|
// matches
|
|
@@ -641,15 +637,7 @@ export class Route<
|
|
|
641
637
|
TAllParentContext,
|
|
642
638
|
TRouteContext,
|
|
643
639
|
TAllContext
|
|
644
|
-
>
|
|
645
|
-
UpdatableRouteOptions<
|
|
646
|
-
TLoader,
|
|
647
|
-
TSearchSchema,
|
|
648
|
-
TFullSearchSchema,
|
|
649
|
-
TAllParams,
|
|
650
|
-
TRouteContext,
|
|
651
|
-
TAllContext
|
|
652
|
-
>
|
|
640
|
+
>
|
|
653
641
|
|
|
654
642
|
// Set up in this.init()
|
|
655
643
|
parentRoute!: TParentRoute
|
package/src/router.ts
CHANGED
|
@@ -445,7 +445,7 @@ export class Router<
|
|
|
445
445
|
|
|
446
446
|
#onFocus = () => {
|
|
447
447
|
if (this.options.refetchOnWindowFocus ?? true) {
|
|
448
|
-
this.
|
|
448
|
+
this.invalidate()
|
|
449
449
|
}
|
|
450
450
|
}
|
|
451
451
|
|
|
@@ -914,52 +914,24 @@ export class Router<
|
|
|
914
914
|
}
|
|
915
915
|
})()
|
|
916
916
|
|
|
917
|
-
Object.assign(match,
|
|
918
|
-
...searchInfo,
|
|
919
|
-
})
|
|
920
|
-
|
|
921
|
-
const contextInfo = (() => {
|
|
922
|
-
try {
|
|
923
|
-
const routeContext =
|
|
924
|
-
route.options.getContext?.({
|
|
925
|
-
parentContext: parentMatch?.routeContext ?? {},
|
|
926
|
-
context: parentMatch?.context ?? this?.options.context ?? {},
|
|
927
|
-
params: match.params,
|
|
928
|
-
search: match.search,
|
|
929
|
-
}) || ({} as any)
|
|
930
|
-
|
|
931
|
-
const context = {
|
|
932
|
-
...(parentMatch?.context ?? this?.options.context),
|
|
933
|
-
...routeContext,
|
|
934
|
-
} as any
|
|
935
|
-
|
|
936
|
-
return {
|
|
937
|
-
context,
|
|
938
|
-
routeContext,
|
|
939
|
-
}
|
|
940
|
-
} catch (err) {
|
|
941
|
-
route.options.onError?.(err)
|
|
942
|
-
throw err
|
|
943
|
-
}
|
|
944
|
-
})()
|
|
945
|
-
|
|
946
|
-
Object.assign(match, {
|
|
947
|
-
...contextInfo,
|
|
948
|
-
})
|
|
917
|
+
Object.assign(match, searchInfo)
|
|
949
918
|
})
|
|
950
919
|
|
|
951
920
|
return matches as any
|
|
952
921
|
}
|
|
953
922
|
|
|
954
923
|
loadMatches = async (
|
|
955
|
-
|
|
924
|
+
_resolvedMatches: AnyRouteMatch[],
|
|
956
925
|
opts?: {
|
|
957
926
|
preload?: boolean
|
|
958
927
|
maxAge?: number
|
|
959
928
|
},
|
|
960
929
|
) => {
|
|
930
|
+
const getFreshMatches = () =>
|
|
931
|
+
_resolvedMatches.map((d) => this.getRouteMatch(d.id)!)
|
|
932
|
+
|
|
961
933
|
if (!opts?.preload) {
|
|
962
|
-
|
|
934
|
+
getFreshMatches().forEach((match) => {
|
|
963
935
|
// Update each match with its latest route data
|
|
964
936
|
this.setRouteMatch(match.id, (s) => ({
|
|
965
937
|
...s,
|
|
@@ -980,7 +952,8 @@ export class Router<
|
|
|
980
952
|
|
|
981
953
|
// Check each match middleware to see if the route can be accessed
|
|
982
954
|
try {
|
|
983
|
-
for (const [index, match] of
|
|
955
|
+
for (const [index, match] of getFreshMatches().entries()) {
|
|
956
|
+
const parentMatch = getFreshMatches()[index - 1]
|
|
984
957
|
const route = this.getRoute(match.routeId)
|
|
985
958
|
|
|
986
959
|
const handleError = (err: any, code: string) => {
|
|
@@ -1020,10 +993,24 @@ export class Router<
|
|
|
1020
993
|
let didError = false
|
|
1021
994
|
|
|
1022
995
|
try {
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
996
|
+
const routeContext =
|
|
997
|
+
(await route.options.beforeLoad?.({
|
|
998
|
+
...match,
|
|
999
|
+
preload: !!opts?.preload,
|
|
1000
|
+
parentContext: parentMatch?.routeContext ?? {},
|
|
1001
|
+
context: parentMatch?.context ?? this?.options.context ?? {},
|
|
1002
|
+
})) ?? ({} as any)
|
|
1003
|
+
|
|
1004
|
+
const context = {
|
|
1005
|
+
...(parentMatch?.context ?? this?.options.context),
|
|
1006
|
+
...routeContext,
|
|
1007
|
+
} as any
|
|
1008
|
+
|
|
1009
|
+
this.setRouteMatch(match.id, (s) => ({
|
|
1010
|
+
...s,
|
|
1011
|
+
context,
|
|
1012
|
+
routeContext,
|
|
1013
|
+
}))
|
|
1027
1014
|
} catch (err) {
|
|
1028
1015
|
handleError(err, 'BEFORE_LOAD')
|
|
1029
1016
|
didError = true
|
|
@@ -1042,7 +1029,7 @@ export class Router<
|
|
|
1042
1029
|
throw err
|
|
1043
1030
|
}
|
|
1044
1031
|
|
|
1045
|
-
const validResolvedMatches =
|
|
1032
|
+
const validResolvedMatches = getFreshMatches().slice(0, firstBadMatchIndex)
|
|
1046
1033
|
const matchPromises: Promise<any>[] = []
|
|
1047
1034
|
|
|
1048
1035
|
validResolvedMatches.forEach((match, index) => {
|
|
@@ -1154,14 +1141,6 @@ export class Router<
|
|
|
1154
1141
|
await Promise.all(matchPromises)
|
|
1155
1142
|
}
|
|
1156
1143
|
|
|
1157
|
-
reload = () => {
|
|
1158
|
-
return this.navigate({
|
|
1159
|
-
fromCurrent: true,
|
|
1160
|
-
replace: true,
|
|
1161
|
-
search: true,
|
|
1162
|
-
} as any)
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
1144
|
resolvePath = (from: string, path: string) => {
|
|
1166
1145
|
return resolvePath(this.basepath!, from, cleanPath(path))
|
|
1167
1146
|
}
|
|
@@ -1854,7 +1833,11 @@ export class Router<
|
|
|
1854
1833
|
}
|
|
1855
1834
|
|
|
1856
1835
|
if (opts?.reload ?? true) {
|
|
1857
|
-
return this.
|
|
1836
|
+
return this.navigate({
|
|
1837
|
+
fromCurrent: true,
|
|
1838
|
+
replace: true,
|
|
1839
|
+
search: true,
|
|
1840
|
+
} as any)
|
|
1858
1841
|
}
|
|
1859
1842
|
}
|
|
1860
1843
|
}
|