@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.
@@ -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
  }
@@ -145,7 +146,6 @@ interface RouterState {
145
146
  location: Location;
146
147
  matches: RouteMatch[];
147
148
  lastUpdated: number;
148
- loaderData: unknown;
149
149
  currentAction?: ActionState;
150
150
  latestAction?: ActionState;
151
151
  actions: Record<string, Action>;
@@ -186,6 +186,11 @@ interface MatchRouteOptions {
186
186
  pending: boolean;
187
187
  caseSensitive?: boolean;
188
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
+ }
189
194
  interface Router<TRouteConfig extends AnyRouteConfig = RouteConfig, TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>> {
190
195
  history: BrowserHistory | MemoryHistory | HashHistory;
191
196
  options: PickAsRequired<RouterOptions<TRouteConfig>, 'stringifySearch' | 'parseSearch'>;
@@ -241,6 +246,8 @@ interface Router<TRouteConfig extends AnyRouteConfig = RouteConfig, TAllRouteInf
241
246
  navigate: <TFrom extends ValidFromPath<TAllRouteInfo> = '/', TTo extends string = '.'>(opts: NavigateOptionsAbsolute<TAllRouteInfo, TFrom, TTo>) => Promise<void>;
242
247
  matchRoute: <TFrom extends ValidFromPath<TAllRouteInfo> = '/', TTo extends string = '.'>(matchLocation: ToOptions<TAllRouteInfo, TFrom, TTo>, opts?: MatchRouteOptions) => boolean;
243
248
  buildLink: <TFrom extends ValidFromPath<TAllRouteInfo> = '/', TTo extends string = '.'>(opts: LinkOptions<TAllRouteInfo, TFrom, TTo>) => LinkInfo;
249
+ dehydrateState: () => DehydratedRouterState;
250
+ hydrateState: (state: DehydratedRouterState) => void;
244
251
  __: {
245
252
  buildRouteTree: (routeConfig: RouteConfig) => Route<TAllRouteInfo, AnyRouteInfo>;
246
253
  parseLocation: (location: History['location'], previousLocation?: Location) => Location;
@@ -609,4 +616,4 @@ declare const defaultStringifySearch: (search: Record<string, any>) => string;
609
616
  declare function parseSearchWith(parser: (str: string) => any): (searchStr: string) => AnySearchSchema;
610
617
  declare function stringifySearchWith(stringify: (search: any) => string): (search: Record<string, any>) => string;
611
618
 
612
- 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,11 @@
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
+ }, {});
937
+ }
933
938
 
934
939
  function joinPaths(paths) {
935
940
  return cleanPath(paths.filter(Boolean).join('/'));
@@ -1725,7 +1730,6 @@
1725
1730
  matches: [],
1726
1731
  actions: {},
1727
1732
  loaders: {},
1728
- loaderData: {},
1729
1733
  lastUpdated: Date.now(),
1730
1734
  isFetching: false,
1731
1735
  isPreloading: false
@@ -1748,6 +1752,26 @@
1748
1752
  cascadeLoaderData(router.state.matches);
1749
1753
  router.listeners.forEach(listener => listener(router));
1750
1754
  },
1755
+ dehydrateState: () => {
1756
+ router.state;
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
+ },
1751
1775
  mount: () => {
1752
1776
  const next = router.__.buildLocation({
1753
1777
  to: '.',
@@ -1759,11 +1783,11 @@
1759
1783
 
1760
1784
  if (next.href !== router.location.href) {
1761
1785
  router.__.commitLocation(next, true);
1762
- } else {
1763
- router.loadLocation();
1764
1786
  }
1765
1787
 
1766
- const unsub = history.listen(event => {
1788
+ router.loadLocation();
1789
+ const unsub = router.history.listen(event => {
1790
+ console.log(event.location);
1767
1791
  router.loadLocation(router.__.parseLocation(event.location, router.location));
1768
1792
  }); // addEventListener does not exist in React Native, but window does
1769
1793
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
@@ -1785,6 +1809,17 @@
1785
1809
  router.loadLocation();
1786
1810
  },
1787
1811
  update: opts => {
1812
+ const newHistory = (opts == null ? void 0 : opts.history) !== router.history;
1813
+
1814
+ if (!router.location || newHistory) {
1815
+ if (opts != null && opts.history) {
1816
+ router.history = opts.history;
1817
+ }
1818
+
1819
+ router.location = router.__.parseLocation(router.history.location);
1820
+ router.state.location = router.location;
1821
+ }
1822
+
1788
1823
  Object.assign(router.options, opts);
1789
1824
  const {
1790
1825
  basepath,
@@ -1833,7 +1868,7 @@
1833
1868
 
1834
1869
  router.cancelMatches(); // Match the routes
1835
1870
 
1836
- const matches = router.matchRoutes(location.pathname, {
1871
+ const matches = router.matchRoutes(router.location.pathname, {
1837
1872
  strictParseParams: true
1838
1873
  });
1839
1874
  router.state = _extends({}, router.state, {
@@ -2296,11 +2331,7 @@
2296
2331
  const recurseRoutes = (routeConfigs, parent) => {
2297
2332
  return routeConfigs.map(routeConfig => {
2298
2333
  const routeOptions = routeConfig.options;
2299
- const route = createRoute(routeConfig, routeOptions, parent, router); // {
2300
- // pendingMs: routeOptions.pendingMs ?? router.defaultPendingMs,
2301
- // pendingMinMs: routeOptions.pendingMinMs ?? router.defaultPendingMinMs,
2302
- // }
2303
-
2334
+ const route = createRoute(routeConfig, routeOptions, parent, router);
2304
2335
  const existingRoute = router.routesById[route.routeId];
2305
2336
 
2306
2337
  if (existingRoute) {
@@ -2435,8 +2466,6 @@
2435
2466
  }
2436
2467
  }
2437
2468
  };
2438
- router.location = router.__.parseLocation(history.location);
2439
- router.state.location = router.location;
2440
2469
  router.update(userOptions); // Allow frameworks to hook into the router creation
2441
2470
 
2442
2471
  router.options.createRouter == null ? void 0 : router.options.createRouter(router);
@@ -2469,6 +2498,7 @@
2469
2498
  exports.matchPathname = matchPathname;
2470
2499
  exports.parsePathname = parsePathname;
2471
2500
  exports.parseSearchWith = parseSearchWith;
2501
+ exports.pick = pick;
2472
2502
  exports.replaceEqualDeep = replaceEqualDeep;
2473
2503
  exports.resolvePath = resolvePath;
2474
2504
  exports.rootRouteId = rootRouteId;