@tanstack/react-router 0.0.1-beta.18 → 0.0.1-beta.19

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.
@@ -10,7 +10,6 @@
10
10
  */
11
11
  import * as React from 'react';
12
12
  import { useSyncExternalStore } from 'use-sync-external-store/shim';
13
- export { lazyWithPreload as lazy } from 'react-lazy-with-preload/lib/index';
14
13
 
15
14
  function _extends$2() {
16
15
  _extends$2 = Object.assign ? Object.assign.bind() : function (target) {
@@ -1534,11 +1533,19 @@ function createRouteMatch(router, route, opts) {
1534
1533
  }
1535
1534
  },
1536
1535
  fetch: async opts => {
1537
- const id = '' + Date.now() + Math.random();
1538
- routeMatch.__.latestId = id; // If the match was in an error state, set it
1536
+ const loadId = '' + Date.now() + Math.random();
1537
+ routeMatch.__.latestId = loadId;
1538
+
1539
+ const checkLatest = async () => {
1540
+ if (loadId !== routeMatch.__.latestId) {
1541
+ // warning(true, 'Data loader is out of date!')
1542
+ return new Promise(() => {});
1543
+ }
1544
+ }; // If the match was in an error state, set it
1539
1545
  // to a loading state again. Otherwise, keep it
1540
1546
  // as loading or resolved
1541
1547
 
1548
+
1542
1549
  if (routeMatch.status === 'idle') {
1543
1550
  routeMatch.status = 'loading';
1544
1551
  } // We started loading the route, so it's no longer invalid
@@ -1570,19 +1577,8 @@ function createRouteMatch(router, route, opts) {
1570
1577
  var _ref, _ref2, _opts$maxAge;
1571
1578
 
1572
1579
  if (routeMatch.options.loader) {
1573
- var _routeMatch$parentMat3;
1574
-
1575
- const data = await routeMatch.options.loader({
1576
- parentLoaderPromise: (_routeMatch$parentMat3 = routeMatch.parentMatch) == null ? void 0 : _routeMatch$parentMat3.__.dataPromise,
1577
- params: routeMatch.params,
1578
- search: routeMatch.routeSearch,
1579
- signal: routeMatch.__.abortController.signal
1580
- });
1581
-
1582
- if (id !== routeMatch.__.latestId) {
1583
- return routeMatch.__.loadPromise;
1584
- }
1585
-
1580
+ const data = await router.loadMatchData(routeMatch);
1581
+ await checkLatest();
1586
1582
  routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1587
1583
  }
1588
1584
 
@@ -1592,9 +1588,7 @@ function createRouteMatch(router, route, opts) {
1592
1588
  routeMatch.invalidAt = routeMatch.updatedAt + ((_ref = (_ref2 = (_opts$maxAge = opts == null ? void 0 : opts.maxAge) != null ? _opts$maxAge : routeMatch.options.loaderMaxAge) != null ? _ref2 : router.options.defaultLoaderMaxAge) != null ? _ref : 0);
1593
1589
  return routeMatch.routeLoaderData;
1594
1590
  } catch (err) {
1595
- if (id !== routeMatch.__.latestId) {
1596
- return routeMatch.__.loadPromise;
1597
- }
1591
+ await checkLatest();
1598
1592
 
1599
1593
  if (process.env.NODE_ENV !== 'production') {
1600
1594
  console.error(err);
@@ -1607,29 +1601,23 @@ function createRouteMatch(router, route, opts) {
1607
1601
  }
1608
1602
  });
1609
1603
 
1610
- try {
1611
- await Promise.all([routeMatch.__.componentsPromise, routeMatch.__.dataPromise.catch(() => {})]);
1612
-
1613
- if (id !== routeMatch.__.latestId) {
1614
- return routeMatch.__.loadPromise;
1615
- }
1616
- } finally {
1617
- if (id !== routeMatch.__.latestId) {
1618
- return routeMatch.__.loadPromise;
1619
- }
1620
-
1604
+ const after = async () => {
1605
+ await checkLatest();
1621
1606
  routeMatch.isFetching = false;
1607
+ delete routeMatch.__.loadPromise;
1622
1608
 
1623
1609
  routeMatch.__.notify();
1610
+ };
1611
+
1612
+ try {
1613
+ await Promise.all([routeMatch.__.componentsPromise, routeMatch.__.dataPromise.catch(() => {})]);
1614
+ after();
1615
+ } catch (_unused) {
1616
+ after();
1624
1617
  }
1625
1618
  });
1626
1619
  await routeMatch.__.loadPromise;
1627
-
1628
- if (id !== routeMatch.__.latestId) {
1629
- return routeMatch.__.loadPromise;
1630
- }
1631
-
1632
- delete routeMatch.__.loadPromise;
1620
+ await checkLatest();
1633
1621
  }
1634
1622
  });
1635
1623
 
@@ -1723,6 +1711,8 @@ function createRouter(userOptions) {
1723
1711
  });
1724
1712
 
1725
1713
  let router = {
1714
+ types: undefined,
1715
+ // public api
1726
1716
  history,
1727
1717
  options: originalOptions,
1728
1718
  listeners: [],
@@ -1731,7 +1721,6 @@ function createRouter(userOptions) {
1731
1721
  routeTree: undefined,
1732
1722
  routesById: {},
1733
1723
  location: undefined,
1734
- allRouteInfo: undefined,
1735
1724
  //
1736
1725
  navigationPromise: Promise.resolve(),
1737
1726
  resolveNavigation: () => {},
@@ -2114,6 +2103,11 @@ function createRouter(userOptions) {
2114
2103
  match.__.validate();
2115
2104
 
2116
2105
  match.load(loaderOpts);
2106
+ const search = match.search;
2107
+
2108
+ if (search.__data && search.__data.matchId !== match.matchId) {
2109
+ return;
2110
+ }
2117
2111
 
2118
2112
  if (match.__.loadPromise) {
2119
2113
  // Wait for the first sign of activity from the match
@@ -2123,6 +2117,37 @@ function createRouter(userOptions) {
2123
2117
  router.notify();
2124
2118
  await Promise.all(matchPromises);
2125
2119
  },
2120
+ loadMatchData: async routeMatch => {
2121
+ if (isServer || !router.options.useServerData) {
2122
+ var _await$routeMatch$opt;
2123
+
2124
+ return (_await$routeMatch$opt = await (routeMatch.options.loader == null ? void 0 : routeMatch.options.loader({
2125
+ // parentLoaderPromise: routeMatch.parentMatch?.__.dataPromise,
2126
+ params: routeMatch.params,
2127
+ search: routeMatch.routeSearch,
2128
+ signal: routeMatch.__.abortController.signal
2129
+ }))) != null ? _await$routeMatch$opt : {};
2130
+ } else {
2131
+ const next = router.buildNext({
2132
+ to: '.',
2133
+ search: d => _extends({}, d != null ? d : {}, {
2134
+ __data: {
2135
+ matchId: routeMatch.matchId
2136
+ }
2137
+ })
2138
+ });
2139
+ const res = await fetch(next.href, {
2140
+ method: 'GET' // signal: routeMatch.__.abortController.signal,
2141
+
2142
+ });
2143
+
2144
+ if (res.ok) {
2145
+ return res.json();
2146
+ }
2147
+
2148
+ throw new Error('Failed to fetch match data');
2149
+ }
2150
+ },
2126
2151
  invalidateRoute: opts => {
2127
2152
  var _router$state$pending5, _router$state$pending6;
2128
2153
 
@@ -2499,9 +2524,38 @@ function cascadeLoaderData(matches) {
2499
2524
  const _excluded = ["type", "children", "target", "activeProps", "inactiveProps", "activeOptions", "disabled", "hash", "search", "params", "to", "preload", "preloadDelay", "preloadMaxAge", "replace", "style", "className", "onClick", "onFocus", "onMouseEnter", "onMouseLeave", "onTouchStart", "onTouchEnd"],
2500
2525
  _excluded2 = ["pending", "caseSensitive", "children"],
2501
2526
  _excluded3 = ["children", "router"];
2527
+ function lazy(importer) {
2528
+ const lazyComp = /*#__PURE__*/React.lazy(importer);
2529
+ let promise;
2530
+ let resolvedComp;
2531
+ const forwardedComp = /*#__PURE__*/React.forwardRef((props, ref) => {
2532
+ const resolvedCompRef = React.useRef(resolvedComp || lazyComp);
2533
+ return /*#__PURE__*/React.createElement(resolvedCompRef.current, _extends$2({}, ref ? {
2534
+ ref
2535
+ } : {}, props));
2536
+ });
2537
+ const finalComp = forwardedComp;
2538
+
2539
+ finalComp.preload = () => {
2540
+ if (!promise) {
2541
+ promise = importer().then(module => {
2542
+ resolvedComp = module.default;
2543
+ return resolvedComp;
2544
+ });
2545
+ }
2546
+
2547
+ return promise;
2548
+ };
2549
+
2550
+ return finalComp;
2551
+ }
2502
2552
  //
2553
+ function Link(props) {
2554
+ const router = useRouter();
2555
+ return /*#__PURE__*/React.createElement(router.Link, props);
2556
+ }
2503
2557
  const matchesContext = /*#__PURE__*/React.createContext(null);
2504
- const routerContext = /*#__PURE__*/React.createContext(null); // Detect if we're in the DOM
2558
+ const routerContext = /*#__PURE__*/React.createContext(null);
2505
2559
  function MatchesProvider(props) {
2506
2560
  return /*#__PURE__*/React.createElement(matchesContext.Provider, props);
2507
2561
  }
@@ -2684,11 +2738,12 @@ function RouterProvider(_ref2) {
2684
2738
  router.update(rest);
2685
2739
  useRouterSubscription(router);
2686
2740
  React.useEffect(() => {
2741
+ console.log('hello');
2687
2742
  return router.mount();
2688
2743
  }, [router]);
2689
2744
  return /*#__PURE__*/React.createElement(routerContext.Provider, {
2690
2745
  value: {
2691
- router
2746
+ router: router
2692
2747
  }
2693
2748
  }, /*#__PURE__*/React.createElement(MatchesProvider, {
2694
2749
  value: router.state.matches
@@ -2703,6 +2758,10 @@ function useRouter() {
2703
2758
  function useMatches() {
2704
2759
  return React.useContext(matchesContext);
2705
2760
  }
2761
+ function useMatch(routeId, opts) {
2762
+ const router = useRouter();
2763
+ return router.useMatch(routeId, opts);
2764
+ }
2706
2765
  function Outlet() {
2707
2766
  var _ref3, _match$__$pendingComp, _match$__$errorCompon;
2708
2767
 
@@ -2734,12 +2793,8 @@ function Outlet() {
2734
2793
  return /*#__PURE__*/React.createElement((_ref4 = (_ref5 = match.__.component) != null ? _ref5 : router.options.defaultComponent) != null ? _ref4 : Outlet);
2735
2794
  }
2736
2795
 
2737
- if (match.__.loadPromise) {
2738
- console.log(match.matchId, 'suspend');
2739
- throw match.__.loadPromise;
2740
- }
2741
-
2742
- invariant(false, 'This should never happen!');
2796
+ console.log(match.matchId, 'suspend');
2797
+ throw match.__.loadPromise;
2743
2798
  })())));
2744
2799
  }
2745
2800
 
@@ -2825,5 +2880,5 @@ function Prompt(_ref7) {
2825
2880
  return children != null ? children : null;
2826
2881
  }
2827
2882
 
2828
- export { DefaultErrorBoundary, MatchesProvider, Outlet, Prompt, RouterProvider, cleanPath, createBrowserHistory, createHashHistory, createMemoryHistory, createReactRouter, 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, useMatches, usePrompt, useRouter, warning };
2883
+ export { DefaultErrorBoundary, Link, MatchesProvider, Outlet, Prompt, RouterProvider, cleanPath, createBrowserHistory, createHashHistory, createMemoryHistory, createReactRouter, createRoute, createRouteConfig, createRouteMatch, createRouter, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, invariant, joinPaths, last, lazy, matchByPath, matchPathname, matchesContext, parsePathname, parseSearchWith, pick, replaceEqualDeep, resolvePath, rootRouteId, routerContext, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, useMatch, useMatches, usePrompt, useRouter, warning };
2829
2884
  //# sourceMappingURL=index.js.map