@tanstack/router-core 0.0.1-beta.5 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/router-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.5",
4
+ "version": "0.0.1-beta.6",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
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,
@@ -224,6 +225,22 @@ type LinkCurrentTargetElement = {
224
225
  preloadTimeout?: null | ReturnType<typeof setTimeout>
225
226
  }
226
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
+
227
244
  export interface Router<
228
245
  TRouteConfig extends AnyRouteConfig = RouteConfig,
229
246
  TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>,
@@ -302,6 +319,8 @@ export interface Router<
302
319
  >(
303
320
  opts: LinkOptions<TAllRouteInfo, TFrom, TTo>,
304
321
  ) => LinkInfo
322
+ dehydrateState: () => DehydratedRouterState
323
+ hydrateState: (state: DehydratedRouterState) => void
305
324
  __: {
306
325
  buildRouteTree: (
307
326
  routeConfig: RouteConfig,
@@ -396,6 +415,47 @@ export function createRouter<
396
415
  router.listeners.forEach((listener) => listener(router))
397
416
  },
398
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
+
399
459
  mount: () => {
400
460
  const next = router.__.buildLocation({
401
461
  to: '.',
@@ -1019,12 +1079,6 @@ export function createRouter<
1019
1079
  return routeConfigs.map((routeConfig) => {
1020
1080
  const routeOptions = routeConfig.options
1021
1081
  const route = createRoute(routeConfig, routeOptions, parent, router)
1022
-
1023
- // {
1024
- // pendingMs: routeOptions.pendingMs ?? router.defaultPendingMs,
1025
- // pendingMinMs: routeOptions.pendingMinMs ?? router.defaultPendingMinMs,
1026
- // }
1027
-
1028
1082
  const existingRoute = (router.routesById as any)[route.routeId]
1029
1083
 
1030
1084
  if (existingRoute) {
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
+ }