@tanstack/router-core 0.0.1-beta.4 → 0.0.1-beta.6
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/packages/router-core/src/index.js +1 -0
- package/build/cjs/packages/router-core/src/index.js.map +1 -1
- package/build/cjs/packages/router-core/src/router.js +36 -12
- package/build/cjs/packages/router-core/src/router.js.map +1 -1
- package/build/cjs/packages/router-core/src/utils.js +6 -0
- package/build/cjs/packages/router-core/src/utils.js.map +1 -1
- package/build/esm/index.js +42 -13
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +134 -134
- package/build/types/index.d.ts +9 -2
- package/build/umd/index.development.js +42 -12
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/router.ts +74 -15
- package/src/utils.ts +6 -0
package/package.json
CHANGED
package/src/router.ts
CHANGED
|
@@ -45,6 +45,7 @@ import { defaultParseSearch, defaultStringifySearch } from './searchParams'
|
|
|
45
45
|
import {
|
|
46
46
|
functionalUpdate,
|
|
47
47
|
last,
|
|
48
|
+
pick,
|
|
48
49
|
PickAsRequired,
|
|
49
50
|
PickRequired,
|
|
50
51
|
replaceEqualDeep,
|
|
@@ -172,7 +173,6 @@ export interface RouterState {
|
|
|
172
173
|
location: Location
|
|
173
174
|
matches: RouteMatch[]
|
|
174
175
|
lastUpdated: number
|
|
175
|
-
loaderData: unknown
|
|
176
176
|
currentAction?: ActionState
|
|
177
177
|
latestAction?: ActionState
|
|
178
178
|
actions: Record<string, Action>
|
|
@@ -225,6 +225,22 @@ type LinkCurrentTargetElement = {
|
|
|
225
225
|
preloadTimeout?: null | ReturnType<typeof setTimeout>
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
interface DehydratedRouterState
|
|
229
|
+
extends Pick<RouterState, 'status' | 'location' | 'lastUpdated'> {
|
|
230
|
+
matches: DehydratedRouteMatch[]
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
interface DehydratedRouteMatch
|
|
234
|
+
extends Pick<
|
|
235
|
+
RouteMatch<any, any>,
|
|
236
|
+
| 'matchId'
|
|
237
|
+
| 'status'
|
|
238
|
+
| 'routeLoaderData'
|
|
239
|
+
| 'loaderData'
|
|
240
|
+
| 'isInvalid'
|
|
241
|
+
| 'invalidAt'
|
|
242
|
+
> {}
|
|
243
|
+
|
|
228
244
|
export interface Router<
|
|
229
245
|
TRouteConfig extends AnyRouteConfig = RouteConfig,
|
|
230
246
|
TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>,
|
|
@@ -303,6 +319,8 @@ export interface Router<
|
|
|
303
319
|
>(
|
|
304
320
|
opts: LinkOptions<TAllRouteInfo, TFrom, TTo>,
|
|
305
321
|
) => LinkInfo
|
|
322
|
+
dehydrateState: () => DehydratedRouterState
|
|
323
|
+
hydrateState: (state: DehydratedRouterState) => void
|
|
306
324
|
__: {
|
|
307
325
|
buildRouteTree: (
|
|
308
326
|
routeConfig: RouteConfig,
|
|
@@ -366,7 +384,6 @@ export function createRouter<
|
|
|
366
384
|
matches: [],
|
|
367
385
|
actions: {},
|
|
368
386
|
loaders: {},
|
|
369
|
-
loaderData: {} as any,
|
|
370
387
|
lastUpdated: Date.now(),
|
|
371
388
|
isFetching: false,
|
|
372
389
|
isPreloading: false,
|
|
@@ -398,6 +415,47 @@ export function createRouter<
|
|
|
398
415
|
router.listeners.forEach((listener) => listener(router))
|
|
399
416
|
},
|
|
400
417
|
|
|
418
|
+
dehydrateState: () => {
|
|
419
|
+
const {} = router.state
|
|
420
|
+
|
|
421
|
+
return {
|
|
422
|
+
...pick(router.state, ['status', 'location', 'lastUpdated']),
|
|
423
|
+
matches: router.state.matches.map((match) =>
|
|
424
|
+
pick(match, [
|
|
425
|
+
'matchId',
|
|
426
|
+
'status',
|
|
427
|
+
'routeLoaderData',
|
|
428
|
+
'loaderData',
|
|
429
|
+
'isInvalid',
|
|
430
|
+
'invalidAt',
|
|
431
|
+
]),
|
|
432
|
+
),
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
|
|
436
|
+
hydrateState: (dehydratedState) => {
|
|
437
|
+
// Match the routes
|
|
438
|
+
const matches = router.matchRoutes(router.location.pathname, {
|
|
439
|
+
strictParseParams: true,
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
router.state = {
|
|
443
|
+
...router.state,
|
|
444
|
+
...dehydratedState,
|
|
445
|
+
matches: matches.map((match) => {
|
|
446
|
+
const dehydratedMatch = dehydratedState.matches.find(
|
|
447
|
+
(d: any) => d.matchId === match.matchId,
|
|
448
|
+
)
|
|
449
|
+
invariant(
|
|
450
|
+
dehydratedMatch,
|
|
451
|
+
'Oh no! Dehydrated route matches did not match the active state of the router 😬',
|
|
452
|
+
)
|
|
453
|
+
Object.assign(match, dehydratedMatch)
|
|
454
|
+
return match
|
|
455
|
+
}),
|
|
456
|
+
}
|
|
457
|
+
},
|
|
458
|
+
|
|
401
459
|
mount: () => {
|
|
402
460
|
const next = router.__.buildLocation({
|
|
403
461
|
to: '.',
|
|
@@ -409,11 +467,12 @@ export function createRouter<
|
|
|
409
467
|
// to the current location. Otherwise, load the current location.
|
|
410
468
|
if (next.href !== router.location.href) {
|
|
411
469
|
router.__.commitLocation(next, true)
|
|
412
|
-
} else {
|
|
413
|
-
router.loadLocation()
|
|
414
470
|
}
|
|
415
471
|
|
|
416
|
-
|
|
472
|
+
router.loadLocation()
|
|
473
|
+
|
|
474
|
+
const unsub = router.history.listen((event) => {
|
|
475
|
+
console.log(event.location)
|
|
417
476
|
router.loadLocation(
|
|
418
477
|
router.__.parseLocation(event.location, router.location),
|
|
419
478
|
)
|
|
@@ -440,6 +499,15 @@ export function createRouter<
|
|
|
440
499
|
},
|
|
441
500
|
|
|
442
501
|
update: (opts) => {
|
|
502
|
+
const newHistory = opts?.history !== router.history
|
|
503
|
+
if (!router.location || newHistory) {
|
|
504
|
+
if (opts?.history) {
|
|
505
|
+
router.history = opts.history
|
|
506
|
+
}
|
|
507
|
+
router.location = router.__.parseLocation(router.history.location)
|
|
508
|
+
router.state.location = router.location
|
|
509
|
+
}
|
|
510
|
+
|
|
443
511
|
Object.assign(router.options, opts)
|
|
444
512
|
|
|
445
513
|
const { basepath, routeConfig } = router.options
|
|
@@ -487,7 +555,7 @@ export function createRouter<
|
|
|
487
555
|
router.cancelMatches()
|
|
488
556
|
|
|
489
557
|
// Match the routes
|
|
490
|
-
const matches = router.matchRoutes(location.pathname, {
|
|
558
|
+
const matches = router.matchRoutes(router.location.pathname, {
|
|
491
559
|
strictParseParams: true,
|
|
492
560
|
})
|
|
493
561
|
|
|
@@ -1011,12 +1079,6 @@ export function createRouter<
|
|
|
1011
1079
|
return routeConfigs.map((routeConfig) => {
|
|
1012
1080
|
const routeOptions = routeConfig.options
|
|
1013
1081
|
const route = createRoute(routeConfig, routeOptions, parent, router)
|
|
1014
|
-
|
|
1015
|
-
// {
|
|
1016
|
-
// pendingMs: routeOptions.pendingMs ?? router.defaultPendingMs,
|
|
1017
|
-
// pendingMinMs: routeOptions.pendingMinMs ?? router.defaultPendingMinMs,
|
|
1018
|
-
// }
|
|
1019
|
-
|
|
1020
1082
|
const existingRoute = (router.routesById as any)[route.routeId]
|
|
1021
1083
|
|
|
1022
1084
|
if (existingRoute) {
|
|
@@ -1211,9 +1273,6 @@ export function createRouter<
|
|
|
1211
1273
|
},
|
|
1212
1274
|
}
|
|
1213
1275
|
|
|
1214
|
-
router.location = router.__.parseLocation(history.location)
|
|
1215
|
-
router.state.location = router.location
|
|
1216
|
-
|
|
1217
1276
|
router.update(userOptions)
|
|
1218
1277
|
|
|
1219
1278
|
// Allow frameworks to hook into the router creation
|
package/src/utils.ts
CHANGED
|
@@ -155,3 +155,9 @@ export function functionalUpdate<TResult>(
|
|
|
155
155
|
|
|
156
156
|
return updater
|
|
157
157
|
}
|
|
158
|
+
|
|
159
|
+
export function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {
|
|
160
|
+
return keys.reduce((obj: any, key: K) => {
|
|
161
|
+
obj[key] = parent[key]
|
|
162
|
+
}, {} as any)
|
|
163
|
+
}
|