@tanstack/router-core 0.0.1-beta.5 → 0.0.1-beta.7

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.
@@ -55,6 +55,7 @@ declare function replaceEqualDeep(prev: any, next: any): any;
55
55
  declare function last<T>(arr: T[]): T | undefined;
56
56
  declare function warning(cond: any, message: string): cond is true;
57
57
  declare function functionalUpdate<TResult>(updater: Updater<TResult>, previous: TResult): TResult;
58
+ declare function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K>;
58
59
 
59
60
  interface LocationState {
60
61
  }
@@ -185,6 +186,11 @@ interface MatchRouteOptions {
185
186
  pending: boolean;
186
187
  caseSensitive?: boolean;
187
188
  }
189
+ interface DehydratedRouterState extends Pick<RouterState, 'status' | 'location' | 'lastUpdated'> {
190
+ matches: DehydratedRouteMatch[];
191
+ }
192
+ interface DehydratedRouteMatch extends Pick<RouteMatch<any, any>, 'matchId' | 'status' | 'routeLoaderData' | 'loaderData' | 'isInvalid' | 'invalidAt'> {
193
+ }
188
194
  interface Router<TRouteConfig extends AnyRouteConfig = RouteConfig, TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>> {
189
195
  history: BrowserHistory | MemoryHistory | HashHistory;
190
196
  options: PickAsRequired<RouterOptions<TRouteConfig>, 'stringifySearch' | 'parseSearch'>;
@@ -240,6 +246,8 @@ interface Router<TRouteConfig extends AnyRouteConfig = RouteConfig, TAllRouteInf
240
246
  navigate: <TFrom extends ValidFromPath<TAllRouteInfo> = '/', TTo extends string = '.'>(opts: NavigateOptionsAbsolute<TAllRouteInfo, TFrom, TTo>) => Promise<void>;
241
247
  matchRoute: <TFrom extends ValidFromPath<TAllRouteInfo> = '/', TTo extends string = '.'>(matchLocation: ToOptions<TAllRouteInfo, TFrom, TTo>, opts?: MatchRouteOptions) => boolean;
242
248
  buildLink: <TFrom extends ValidFromPath<TAllRouteInfo> = '/', TTo extends string = '.'>(opts: LinkOptions<TAllRouteInfo, TFrom, TTo>) => LinkInfo;
249
+ dehydrateState: () => DehydratedRouterState;
250
+ hydrateState: (state: DehydratedRouterState) => void;
243
251
  __: {
244
252
  buildRouteTree: (routeConfig: RouteConfig) => Route<TAllRouteInfo, AnyRouteInfo>;
245
253
  parseLocation: (location: History['location'], previousLocation?: Location) => Location;
@@ -608,4 +616,4 @@ declare const defaultStringifySearch: (search: Record<string, any>) => string;
608
616
  declare function parseSearchWith(parser: (str: string) => any): (searchStr: string) => AnySearchSchema;
609
617
  declare function stringifySearchWith(stringify: (search: any) => string): (search: Record<string, any>) => string;
610
618
 
611
- export { Action, ActionFn, ActionState, AllRouteInfo, AnyAllRouteInfo, AnyLoaderData, AnyPathParams, AnyRoute, AnyRouteConfig, AnyRouteConfigWithChildren, AnyRouteInfo, AnySearchSchema, BuildNextOptions, CheckId, CheckIdError, CheckPath, CheckPathError, CheckRelativePath, DeepAwaited, DefaultAllRouteInfo, DefinedPathParamWarning, Expand, FilterRoutesFn, FrameworkGenerics, FromLocation, GetFrameworkGeneric, IsAny, IsAnyBoolean, IsKnown, LinkInfo, LinkOptions, ListenerFn, Loader, LoaderContext, LoaderFn, LoaderState, Location, LocationState, MatchCacheEntry, MatchLocation, MatchRouteOptions, NavigateOptionsAbsolute, NoInfer, ParentParams, ParsePathParams, ParseRouteConfig, PathParamMask, PendingState, PickAsPartial, PickAsRequired, PickExclude, PickExtra, PickExtract, PickRequired, PickUnsafe, RelativeToPathAutoComplete, ResolveRelativePath, RootRouteId, Route, RouteConfig, RouteConfigRoute, RouteInfo, RouteInfoById, RouteInfoByPath, RouteMatch, RouteMeta, RouteOptions, Router, RouterOptions, RouterState, RoutesById, RoutesInfoInner, SearchFilter, SearchParser, SearchSchemaValidator, SearchSchemaValidatorFn, SearchSchemaValidatorObj, SearchSerializer, Segment, Split, Timeout, ToIdOption, ToOptions, ToPathOption, UnloaderFn, Updater, ValidFromPath, ValueKeys, Values, cascadeLoaderData, cleanPath, createRoute, createRouteConfig, createRouteMatch, createRouter, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, joinPaths, last, matchByPath, matchPathname, parsePathname, parseSearchWith, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, warning };
619
+ export { Action, ActionFn, ActionState, AllRouteInfo, AnyAllRouteInfo, AnyLoaderData, AnyPathParams, AnyRoute, AnyRouteConfig, AnyRouteConfigWithChildren, AnyRouteInfo, AnySearchSchema, BuildNextOptions, CheckId, CheckIdError, CheckPath, CheckPathError, CheckRelativePath, DeepAwaited, DefaultAllRouteInfo, DefinedPathParamWarning, Expand, FilterRoutesFn, FrameworkGenerics, FromLocation, GetFrameworkGeneric, IsAny, IsAnyBoolean, IsKnown, LinkInfo, LinkOptions, ListenerFn, Loader, LoaderContext, LoaderFn, LoaderState, Location, LocationState, MatchCacheEntry, MatchLocation, MatchRouteOptions, NavigateOptionsAbsolute, NoInfer, ParentParams, ParsePathParams, ParseRouteConfig, PathParamMask, PendingState, PickAsPartial, PickAsRequired, PickExclude, PickExtra, PickExtract, PickRequired, PickUnsafe, RelativeToPathAutoComplete, ResolveRelativePath, RootRouteId, Route, RouteConfig, RouteConfigRoute, RouteInfo, RouteInfoById, RouteInfoByPath, RouteMatch, RouteMeta, RouteOptions, Router, RouterOptions, RouterState, RoutesById, RoutesInfoInner, SearchFilter, SearchParser, SearchSchemaValidator, SearchSchemaValidatorFn, SearchSchemaValidatorObj, SearchSerializer, Segment, Split, Timeout, ToIdOption, ToOptions, ToPathOption, UnloaderFn, Updater, ValidFromPath, ValueKeys, Values, cascadeLoaderData, cleanPath, createRoute, createRouteConfig, createRouteMatch, createRouter, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, joinPaths, last, matchByPath, matchPathname, parsePathname, parseSearchWith, pick, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, warning };
@@ -930,6 +930,12 @@
930
930
 
931
931
  return updater;
932
932
  }
933
+ function pick(parent, keys) {
934
+ return keys.reduce((obj, key) => {
935
+ obj[key] = parent[key];
936
+ return obj;
937
+ }, {});
938
+ }
933
939
 
934
940
  function joinPaths(paths) {
935
941
  return cleanPath(paths.filter(Boolean).join('/'));
@@ -1747,6 +1753,25 @@
1747
1753
  cascadeLoaderData(router.state.matches);
1748
1754
  router.listeners.forEach(listener => listener(router));
1749
1755
  },
1756
+ dehydrateState: () => {
1757
+ return _extends({}, pick(router.state, ['status', 'location', 'lastUpdated']), {
1758
+ matches: router.state.matches.map(match => pick(match, ['matchId', 'status', 'routeLoaderData', 'loaderData', 'isInvalid', 'invalidAt']))
1759
+ });
1760
+ },
1761
+ hydrateState: dehydratedState => {
1762
+ // Match the routes
1763
+ const matches = router.matchRoutes(router.location.pathname, {
1764
+ strictParseParams: true
1765
+ });
1766
+ router.state = _extends({}, router.state, dehydratedState, {
1767
+ matches: matches.map(match => {
1768
+ const dehydratedMatch = dehydratedState.matches.find(d => d.matchId === match.matchId);
1769
+ invariant(dehydratedMatch, 'Oh no! Dehydrated route matches did not match the active state of the router 😬');
1770
+ Object.assign(match, dehydratedMatch);
1771
+ return match;
1772
+ })
1773
+ });
1774
+ },
1750
1775
  mount: () => {
1751
1776
  const next = router.__.buildLocation({
1752
1777
  to: '.',
@@ -2306,11 +2331,7 @@
2306
2331
  const recurseRoutes = (routeConfigs, parent) => {
2307
2332
  return routeConfigs.map(routeConfig => {
2308
2333
  const routeOptions = routeConfig.options;
2309
- const route = createRoute(routeConfig, routeOptions, parent, router); // {
2310
- // pendingMs: routeOptions.pendingMs ?? router.defaultPendingMs,
2311
- // pendingMinMs: routeOptions.pendingMinMs ?? router.defaultPendingMinMs,
2312
- // }
2313
-
2334
+ const route = createRoute(routeConfig, routeOptions, parent, router);
2314
2335
  const existingRoute = router.routesById[route.routeId];
2315
2336
 
2316
2337
  if (existingRoute) {
@@ -2477,6 +2498,7 @@
2477
2498
  exports.matchPathname = matchPathname;
2478
2499
  exports.parsePathname = parsePathname;
2479
2500
  exports.parseSearchWith = parseSearchWith;
2501
+ exports.pick = pick;
2480
2502
  exports.replaceEqualDeep = replaceEqualDeep;
2481
2503
  exports.resolvePath = resolvePath;
2482
2504
  exports.rootRouteId = rootRouteId;