@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.
@@ -930,6 +930,11 @@ function functionalUpdate(updater, previous) {
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 @@ function createRouter(userOptions) {
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 @@ function createRouter(userOptions) {
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 @@ function createRouter(userOptions) {
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 @@ function createRouter(userOptions) {
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 @@ function createRouter(userOptions) {
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 @@ function createRouter(userOptions) {
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 @@ function createRouter(userOptions) {
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);
@@ -2447,5 +2476,5 @@ function isCtrlEvent(e) {
2447
2476
  return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
2448
2477
  }
2449
2478
 
2450
- export { cascadeLoaderData, cleanPath, createBrowserHistory, createHashHistory, createMemoryHistory, createRoute, createRouteConfig, createRouteMatch, createRouter, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, invariant, joinPaths, last, matchByPath, matchPathname, parsePathname, parseSearchWith, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, warning };
2479
+ export { cascadeLoaderData, cleanPath, createBrowserHistory, createHashHistory, createMemoryHistory, createRoute, createRouteConfig, createRouteMatch, createRouter, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, invariant, joinPaths, last, matchByPath, matchPathname, parsePathname, parseSearchWith, pick, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, warning };
2451
2480
  //# sourceMappingURL=index.js.map