@tanstack/react-router 0.0.1-beta.166 → 0.0.1-beta.168

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.
@@ -9,10 +9,10 @@
9
9
  * @license MIT
10
10
  */
11
11
  (function (global, factory) {
12
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('use-sync-external-store/shim/with-selector')) :
13
- typeof define === 'function' && define.amd ? define(['exports', 'react', 'use-sync-external-store/shim/with-selector'], factory) :
14
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactRouter = {}, global.React, global.withSelector));
15
- })(this, (function (exports, React, withSelector) { 'use strict';
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('use-sync-external-store/shim/with-selector'), require('react')) :
13
+ typeof define === 'function' && define.amd ? define(['exports', 'use-sync-external-store/shim/with-selector', 'react'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactRouter = {}, global.withSelector, global.React));
15
+ })(this, (function (exports, withSelector, React) { 'use strict';
16
16
 
17
17
  function _interopNamespace(e) {
18
18
  if (e && e.__esModule) return e;
@@ -34,21 +34,6 @@
34
34
 
35
35
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
36
36
 
37
- function _extends() {
38
- _extends = Object.assign ? Object.assign.bind() : function (target) {
39
- for (var i = 1; i < arguments.length; i++) {
40
- var source = arguments[i];
41
- for (var key in source) {
42
- if (Object.prototype.hasOwnProperty.call(source, key)) {
43
- target[key] = source[key];
44
- }
45
- }
46
- }
47
- return target;
48
- };
49
- return _extends.apply(this, arguments);
50
- }
51
-
52
37
  /**
53
38
  * @tanstack/store/src/index.ts
54
39
  *
@@ -726,6 +711,15 @@
726
711
 
727
712
  // The parse type here allows a zod schema to be passed directly to the validator
728
713
 
714
+ // T extends Record<PropertyKey, infer U>
715
+ // ? {
716
+ // [K in keyof T]: UseLoaderResultPromise<T[K]>
717
+ // }
718
+ // : UseLoaderResultPromise<T>
719
+
720
+ // export type UseLoaderResultPromise<T> = T extends Promise<infer U>
721
+ // ? StreamedPromise<U>
722
+ // : T
729
723
  class Route {
730
724
  // Set up in this.init()
731
725
 
@@ -988,7 +982,7 @@
988
982
  } = this.options;
989
983
  this.basepath = `/${trimPath(basepath ?? '') ?? ''}`;
990
984
  if (routeTree && routeTree !== this.routeTree) {
991
- this.#buildRouteTree(routeTree);
985
+ this.#processRoutes(routeTree);
992
986
  }
993
987
  return this;
994
988
  };
@@ -1308,7 +1302,6 @@
1308
1302
  return matches;
1309
1303
  };
1310
1304
  loadMatches = async (resolvedMatches, opts) => {
1311
- this.cleanMatches();
1312
1305
  if (!opts?.preload) {
1313
1306
  resolvedMatches.forEach(match => {
1314
1307
  // Update each match with its latest route data
@@ -1321,10 +1314,12 @@
1321
1314
  error: match.error,
1322
1315
  paramsError: match.paramsError,
1323
1316
  searchError: match.searchError,
1324
- params: match.params
1317
+ params: match.params,
1318
+ preloadInvalidAt: 0
1325
1319
  }));
1326
1320
  });
1327
1321
  }
1322
+ this.cleanMatches();
1328
1323
  let firstBadMatchIndex;
1329
1324
 
1330
1325
  // Check each match middleware to see if the route can be accessed
@@ -1460,6 +1455,7 @@
1460
1455
  })());
1461
1456
  });
1462
1457
  await Promise.all(matchPromises);
1458
+ this.cleanMatches();
1463
1459
  };
1464
1460
  reload = () => {
1465
1461
  return this.navigate({
@@ -1639,7 +1635,9 @@
1639
1635
  };
1640
1636
  dehydrate = () => {
1641
1637
  return {
1642
- state: pick(this.state, ['location', 'status', 'lastUpdated'])
1638
+ state: {
1639
+ dehydratedMatches: this.state.matches.map(d => pick(d, ['fetchedAt', 'invalid', 'invalidAt', 'id', 'loaderData', 'status', 'updatedAt']))
1640
+ }
1643
1641
  };
1644
1642
  };
1645
1643
  hydrate = async __do_not_use_server_ctx => {
@@ -1652,16 +1650,27 @@
1652
1650
  const ctx = _ctx;
1653
1651
  this.dehydratedData = ctx.payload;
1654
1652
  this.options.hydrate?.(ctx.payload);
1655
- const routerState = ctx.router.state;
1653
+ const {
1654
+ dehydratedMatches
1655
+ } = ctx.router.state;
1656
+ let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
1657
+ const dehydratedMatch = dehydratedMatches.find(d => d.id === match.id);
1658
+ invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
1659
+ if (dehydratedMatch) {
1660
+ return {
1661
+ ...match,
1662
+ ...dehydratedMatch
1663
+ };
1664
+ }
1665
+ return match;
1666
+ });
1656
1667
  this.__store.setState(s => {
1657
1668
  return {
1658
1669
  ...s,
1659
- ...routerState,
1660
- resolvedLocation: routerState.location
1670
+ matches,
1671
+ matchesById: this.#mergeMatches(s.matchesById, matches)
1661
1672
  };
1662
1673
  });
1663
- await this.load();
1664
- return;
1665
1674
  };
1666
1675
  injectedHtml = [];
1667
1676
  injectHtml = async html => {
@@ -1674,10 +1683,10 @@
1674
1683
  const id = `__TSR_DEHYDRATED__${strKey}`;
1675
1684
  const data = typeof getData === 'function' ? await getData() : getData;
1676
1685
  return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${escapeJSON(strKey)}"] = ${JSON.stringify(data)}
1677
- ;(() => {
1678
- var el = document.getElementById('${id}')
1679
- el.parentElement.removeChild(el)
1680
- })()
1686
+ // ;(() => {
1687
+ // var el = document.getElementById('${id}')
1688
+ // el.parentElement.removeChild(el)
1689
+ // })()
1681
1690
  </script>`;
1682
1691
  });
1683
1692
  return () => this.hydrateData(key);
@@ -1698,7 +1707,7 @@
1698
1707
  // ?.__promisesByKey[key]?.resolve(value)
1699
1708
  // }
1700
1709
 
1701
- #buildRouteTree = routeTree => {
1710
+ #processRoutes = routeTree => {
1702
1711
  this.routeTree = routeTree;
1703
1712
  this.routesById = {};
1704
1713
  this.routesByPath = {};
@@ -1901,7 +1910,6 @@
1901
1910
  preloadInvalidAt,
1902
1911
  invalidAt
1903
1912
  }));
1904
- if (this.state.matches.find(d => d.id === id)) ;
1905
1913
  };
1906
1914
  invalidate = async opts => {
1907
1915
  if (opts?.matchId) {
@@ -2110,19 +2118,41 @@
2110
2118
  }
2111
2119
  }
2112
2120
 
2113
- const useLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
2114
- function useScrollRestoration(options) {
2115
- const router = useRouter();
2116
- useLayoutEffect(() => {
2117
- return watchScrollPositions(router, options);
2118
- }, []);
2119
- useLayoutEffect(() => {
2120
- restoreScrollPositions(router, options);
2121
- });
2121
+ function defer(_promise) {
2122
+ const promise = _promise;
2123
+ if (!promise.__deferredState) {
2124
+ promise.__deferredState = {
2125
+ uid: Math.random().toString(36).slice(2),
2126
+ status: 'pending'
2127
+ };
2128
+ const state = promise.__deferredState;
2129
+ promise.then(data => {
2130
+ state.status = 'success';
2131
+ state.data = data;
2132
+ }).catch(error => {
2133
+ state.status = 'error';
2134
+ state.error = error;
2135
+ });
2136
+ }
2137
+ return promise;
2122
2138
  }
2123
- function ScrollRestoration(props) {
2124
- useScrollRestoration(props);
2125
- return null;
2139
+ function isDehydratedDeferred(obj) {
2140
+ return typeof obj === 'object' && obj !== null && !(obj instanceof Promise) && !obj.then && '__deferredState' in obj;
2141
+ }
2142
+
2143
+ function _extends() {
2144
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
2145
+ for (var i = 1; i < arguments.length; i++) {
2146
+ var source = arguments[i];
2147
+ for (var key in source) {
2148
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
2149
+ target[key] = source[key];
2150
+ }
2151
+ }
2152
+ }
2153
+ return target;
2154
+ };
2155
+ return _extends.apply(this, arguments);
2126
2156
  }
2127
2157
 
2128
2158
  Route.__onInit = route => {
@@ -2319,11 +2349,9 @@
2319
2349
  return unsub;
2320
2350
  }, [router]);
2321
2351
  const Wrap = router.options.Wrap || React__namespace.Fragment;
2322
- return /*#__PURE__*/React__namespace.createElement(React__namespace.Suspense, {
2323
- fallback: null
2324
- }, /*#__PURE__*/React__namespace.createElement(Wrap, null, /*#__PURE__*/React__namespace.createElement(routerContext.Provider, {
2352
+ return /*#__PURE__*/React__namespace.createElement(Wrap, null, /*#__PURE__*/React__namespace.createElement(routerContext.Provider, {
2325
2353
  value: router
2326
- }, /*#__PURE__*/React__namespace.createElement(Matches, null))));
2354
+ }, /*#__PURE__*/React__namespace.createElement(Matches, null)));
2327
2355
  }
2328
2356
  function Matches() {
2329
2357
  const router = useRouter();
@@ -2698,6 +2726,47 @@
2698
2726
  return true;
2699
2727
  }
2700
2728
 
2729
+ const useLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
2730
+ function useScrollRestoration(options) {
2731
+ const router = useRouter();
2732
+ useLayoutEffect(() => {
2733
+ return watchScrollPositions(router, options);
2734
+ }, []);
2735
+ useLayoutEffect(() => {
2736
+ restoreScrollPositions(router, options);
2737
+ });
2738
+ }
2739
+ function ScrollRestoration(props) {
2740
+ useScrollRestoration(props);
2741
+ return null;
2742
+ }
2743
+
2744
+ function useAwaited({
2745
+ promise
2746
+ }) {
2747
+ const router = useRouter();
2748
+ let state = promise.__deferredState;
2749
+ const key = `__TSR__DEFERRED__${state.uid}`;
2750
+ if (isDehydratedDeferred(promise)) {
2751
+ state = router.hydrateData(key);
2752
+ promise = Promise.resolve(state.data);
2753
+ promise.__deferredState = state;
2754
+ }
2755
+ if (state.status === 'pending') {
2756
+ throw promise;
2757
+ }
2758
+ if (state.status === 'error') {
2759
+ throw state.error;
2760
+ }
2761
+ router.dehydrateData(key, state);
2762
+ return [state.data];
2763
+ }
2764
+ function Await(props) {
2765
+ const awaited = useAwaited(props);
2766
+ return props.children(...awaited);
2767
+ }
2768
+
2769
+ exports.Await = Await;
2701
2770
  exports.Block = Block;
2702
2771
  exports.ErrorComponent = ErrorComponent;
2703
2772
  exports.FileRoute = FileRoute;
@@ -2721,10 +2790,12 @@
2721
2790
  exports.decode = decode;
2722
2791
  exports.defaultParseSearch = defaultParseSearch;
2723
2792
  exports.defaultStringifySearch = defaultStringifySearch;
2793
+ exports.defer = defer;
2724
2794
  exports.encode = encode;
2725
2795
  exports.functionalUpdate = functionalUpdate;
2726
2796
  exports.interpolatePath = interpolatePath;
2727
2797
  exports.invariant = invariant;
2798
+ exports.isDehydratedDeferred = isDehydratedDeferred;
2728
2799
  exports.isPlainObject = isPlainObject;
2729
2800
  exports.isRedirect = isRedirect;
2730
2801
  exports.joinPaths = joinPaths;
@@ -2749,6 +2820,7 @@
2749
2820
  exports.trimPath = trimPath;
2750
2821
  exports.trimPathLeft = trimPathLeft;
2751
2822
  exports.trimPathRight = trimPathRight;
2823
+ exports.useAwaited = useAwaited;
2752
2824
  exports.useBlocker = useBlocker;
2753
2825
  exports.useDehydrate = useDehydrate;
2754
2826
  exports.useHydrate = useHydrate;