@tanstack/react-router 0.0.1-beta.12 → 0.0.1-beta.13

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,6 +10,7 @@
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';
13
14
 
14
15
  function _extends$2() {
15
16
  _extends$2 = Object.assign ? Object.assign.bind() : function (target) {
@@ -1427,7 +1428,7 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
1427
1428
  };
1428
1429
  };
1429
1430
 
1430
- const elementTypes = ['element', 'errorElement', 'catchElement', 'pendingElement'];
1431
+ const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
1431
1432
  function createRouteMatch(router, route, opts) {
1432
1433
  const routeMatch = _extends({}, route, opts, {
1433
1434
  router,
@@ -1437,7 +1438,6 @@ function createRouteMatch(router, route, opts) {
1437
1438
  status: 'idle',
1438
1439
  routeLoaderData: {},
1439
1440
  loaderData: {},
1440
- isPending: false,
1441
1441
  isFetching: false,
1442
1442
  isInvalid: false,
1443
1443
  invalidAt: Infinity,
@@ -1455,32 +1455,6 @@ function createRouteMatch(router, route, opts) {
1455
1455
 
1456
1456
  routeMatch.router.notify();
1457
1457
  },
1458
- startPending: () => {
1459
- var _routeMatch$options$p, _routeMatch$options$p2;
1460
-
1461
- const pendingMs = (_routeMatch$options$p = routeMatch.options.pendingMs) != null ? _routeMatch$options$p : router.options.defaultPendingMs;
1462
- const pendingMinMs = (_routeMatch$options$p2 = routeMatch.options.pendingMinMs) != null ? _routeMatch$options$p2 : router.options.defaultPendingMinMs;
1463
-
1464
- if (routeMatch.__.pendingTimeout || routeMatch.status !== 'loading' || typeof pendingMs === 'undefined') {
1465
- return;
1466
- }
1467
-
1468
- routeMatch.__.pendingTimeout = setTimeout(() => {
1469
- routeMatch.isPending = true;
1470
-
1471
- routeMatch.__.resolve();
1472
-
1473
- if (typeof pendingMinMs !== 'undefined') {
1474
- routeMatch.__.pendingMinPromise = new Promise(r => routeMatch.__.pendingMinTimeout = setTimeout(r, pendingMinMs));
1475
- }
1476
- }, pendingMs);
1477
- },
1478
- cancelPending: () => {
1479
- routeMatch.isPending = false;
1480
- clearTimeout(routeMatch.__.pendingTimeout);
1481
- clearTimeout(routeMatch.__.pendingMinTimeout);
1482
- delete routeMatch.__.pendingMinPromise;
1483
- },
1484
1458
  validate: () => {
1485
1459
  var _routeMatch$parentMat, _routeMatch$parentMat2;
1486
1460
 
@@ -1500,11 +1474,11 @@ function createRouteMatch(router, route, opts) {
1500
1474
 
1501
1475
  routeMatch.routeSearch = nextSearch;
1502
1476
  routeMatch.search = replaceEqualDeep(parentSearch, _extends({}, parentSearch, nextSearch));
1503
- elementTypes.map(async type => {
1504
- const routeElement = routeMatch.options[type];
1477
+ componentTypes.map(async type => {
1478
+ const component = routeMatch.options[type];
1505
1479
 
1506
1480
  if (typeof routeMatch.__[type] !== 'function') {
1507
- routeMatch.__[type] = routeElement;
1481
+ routeMatch.__[type] = component;
1508
1482
  }
1509
1483
  });
1510
1484
  } catch (err) {
@@ -1524,14 +1498,16 @@ function createRouteMatch(router, route, opts) {
1524
1498
  var _routeMatch$__$abortC;
1525
1499
 
1526
1500
  (_routeMatch$__$abortC = routeMatch.__.abortController) == null ? void 0 : _routeMatch$__$abortC.abort();
1527
-
1528
- routeMatch.__.cancelPending();
1529
1501
  },
1530
1502
  invalidate: () => {
1531
1503
  routeMatch.isInvalid = true;
1532
1504
  },
1533
1505
  hasLoaders: () => {
1534
- return !!(route.options.loader || elementTypes.some(d => typeof route.options[d] === 'function'));
1506
+ return !!(route.options.loader || componentTypes.some(d => {
1507
+ var _route$options$d;
1508
+
1509
+ return (_route$options$d = route.options[d]) == null ? void 0 : _route$options$d.preload;
1510
+ }));
1535
1511
  },
1536
1512
  load: async loaderOpts => {
1537
1513
  const now = Date.now();
@@ -1575,91 +1551,79 @@ function createRouteMatch(router, route, opts) {
1575
1551
  routeMatch.isFetching = true;
1576
1552
  routeMatch.__.resolve = resolve;
1577
1553
 
1578
- routeMatch.__.loaderDataPromise = (async () => {
1579
- // Load the elements and data in parallel
1580
- routeMatch.__.elementsPromise = (async () => {
1581
- // then run all element and data loaders in parallel
1582
- // For each element type, potentially load it asynchronously
1583
- await Promise.all(elementTypes.map(async type => {
1584
- const routeElement = routeMatch.options[type];
1554
+ routeMatch.__.componentsPromise = (async () => {
1555
+ // then run all component and data loaders in parallel
1556
+ // For each component type, potentially load it asynchronously
1557
+ await Promise.all(componentTypes.map(async type => {
1558
+ var _routeMatch$__$type;
1585
1559
 
1586
- if (typeof routeMatch.__[type] === 'function') {
1587
- routeMatch.__[type] = await router.options.createElement(routeElement);
1588
- }
1589
- }));
1590
- })();
1591
-
1592
- routeMatch.__.dataPromise = Promise.resolve().then(async () => {
1593
- try {
1594
- var _ref, _ref2, _opts$maxAge;
1595
-
1596
- if (routeMatch.options.loader) {
1597
- const data = await routeMatch.options.loader({
1598
- params: routeMatch.params,
1599
- search: routeMatch.routeSearch,
1600
- signal: routeMatch.__.abortController.signal
1601
- });
1602
-
1603
- if (id !== routeMatch.__.latestId) {
1604
- return routeMatch.__.loadPromise;
1605
- }
1560
+ const component = routeMatch.options[type];
1606
1561
 
1607
- routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1608
- }
1562
+ if ((_routeMatch$__$type = routeMatch.__[type]) != null && _routeMatch$__$type.preload) {
1563
+ routeMatch.__[type] = await router.options.loadComponent(component);
1564
+ }
1565
+ }));
1566
+ })();
1567
+
1568
+ routeMatch.__.dataPromise = Promise.resolve().then(async () => {
1569
+ try {
1570
+ var _ref, _ref2, _opts$maxAge;
1571
+
1572
+ if (routeMatch.options.loader) {
1573
+ const data = await routeMatch.options.loader({
1574
+ params: routeMatch.params,
1575
+ search: routeMatch.routeSearch,
1576
+ signal: routeMatch.__.abortController.signal
1577
+ });
1609
1578
 
1610
- routeMatch.error = undefined;
1611
- routeMatch.status = 'success';
1612
- routeMatch.updatedAt = Date.now();
1613
- 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);
1614
- } catch (err) {
1615
1579
  if (id !== routeMatch.__.latestId) {
1616
1580
  return routeMatch.__.loadPromise;
1617
1581
  }
1618
1582
 
1619
- if (process.env.NODE_ENV !== 'production') {
1620
- console.error(err);
1621
- }
1622
-
1623
- routeMatch.error = err;
1624
- routeMatch.status = 'error';
1625
- routeMatch.updatedAt = Date.now();
1583
+ routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1626
1584
  }
1627
- });
1628
-
1629
- try {
1630
- await Promise.all([routeMatch.__.elementsPromise, routeMatch.__.dataPromise]);
1631
1585
 
1586
+ routeMatch.error = undefined;
1587
+ routeMatch.status = 'success';
1588
+ routeMatch.updatedAt = Date.now();
1589
+ 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);
1590
+ } catch (err) {
1632
1591
  if (id !== routeMatch.__.latestId) {
1633
1592
  return routeMatch.__.loadPromise;
1634
1593
  }
1635
1594
 
1636
- if (routeMatch.__.pendingMinPromise) {
1637
- await routeMatch.__.pendingMinPromise;
1638
- delete routeMatch.__.pendingMinPromise;
1639
- }
1640
- } finally {
1641
- if (id !== routeMatch.__.latestId) {
1642
- return routeMatch.__.loadPromise;
1595
+ if (process.env.NODE_ENV !== 'production') {
1596
+ console.error(err);
1643
1597
  }
1644
1598
 
1645
- routeMatch.__.cancelPending();
1599
+ routeMatch.error = err;
1600
+ routeMatch.status = 'error';
1601
+ routeMatch.updatedAt = Date.now();
1602
+ }
1603
+ });
1646
1604
 
1647
- routeMatch.isPending = false;
1648
- routeMatch.isFetching = false;
1605
+ try {
1606
+ await Promise.all([routeMatch.__.componentsPromise, routeMatch.__.dataPromise]);
1649
1607
 
1650
- routeMatch.__.notify();
1608
+ if (id !== routeMatch.__.latestId) {
1609
+ return routeMatch.__.loadPromise;
1610
+ }
1611
+ } finally {
1612
+ if (id !== routeMatch.__.latestId) {
1613
+ return routeMatch.__.loadPromise;
1651
1614
  }
1652
- })();
1653
1615
 
1654
- await routeMatch.__.loaderDataPromise;
1616
+ routeMatch.isFetching = false;
1655
1617
 
1656
- if (id !== routeMatch.__.latestId) {
1657
- return routeMatch.__.loadPromise;
1618
+ routeMatch.__.notify();
1658
1619
  }
1659
-
1660
- delete routeMatch.__.loaderDataPromise;
1661
1620
  });
1662
1621
  await routeMatch.__.loadPromise;
1622
+
1623
+ if (id !== routeMatch.__.latestId) {
1624
+ return routeMatch.__.loadPromise;
1625
+ }
1626
+
1663
1627
  delete routeMatch.__.loadPromise;
1664
1628
  }
1665
1629
  });
@@ -1895,18 +1859,26 @@ function createRouter(userOptions) {
1895
1859
  const matches = router.matchRoutes(router.location.pathname, {
1896
1860
  strictParseParams: true
1897
1861
  });
1898
- router.state = _extends({}, router.state, {
1899
- pending: {
1862
+
1863
+ if (typeof document !== 'undefined') {
1864
+ router.state = _extends({}, router.state, {
1865
+ pending: {
1866
+ matches: matches,
1867
+ location: router.location
1868
+ },
1869
+ status: 'loading'
1870
+ });
1871
+ } else {
1872
+ router.state = _extends({}, router.state, {
1900
1873
  matches: matches,
1901
- location: router.location
1902
- },
1903
- status: 'loading'
1904
- });
1874
+ location: router.location,
1875
+ status: 'loading'
1876
+ });
1877
+ }
1878
+
1905
1879
  router.notify(); // Load the matches
1906
1880
 
1907
- await router.loadMatches(matches, {
1908
- withPending: true
1909
- });
1881
+ await router.loadMatches(matches);
1910
1882
 
1911
1883
  if (router.startedLoadingAt !== id) {
1912
1884
  // Ignore side-effects of match loading
@@ -2128,14 +2100,8 @@ function createRouter(userOptions) {
2128
2100
 
2129
2101
  match.load(loaderOpts);
2130
2102
 
2131
- if (match.status === 'loading') {
2132
- // If requested, start the pending timers
2133
- if (loaderOpts != null && loaderOpts.withPending) match.__.startPending();
2134
- }
2135
-
2136
2103
  if (match.__.loadPromise) {
2137
2104
  // Wait for the first sign of activity from the match
2138
- // This might be completion, error, or a pending state
2139
2105
  await match.__.loadPromise;
2140
2106
  }
2141
2107
  });
@@ -2587,6 +2553,12 @@ function createReactRouter(opts) {
2587
2553
  next
2588
2554
  } = linkInfo;
2589
2555
 
2556
+ const reactHandleClick = e => {
2557
+ React.startTransition(() => {
2558
+ handleClick(e);
2559
+ });
2560
+ };
2561
+
2590
2562
  const composeHandlers = handlers => e => {
2591
2563
  e.persist();
2592
2564
  handlers.forEach(handler => {
@@ -2600,7 +2572,7 @@ function createReactRouter(opts) {
2600
2572
  const resolvedInactiveProps = isActive ? {} : (_functionalUpdate2 = functionalUpdate(inactiveProps, {})) != null ? _functionalUpdate2 : {};
2601
2573
  return _extends$2({}, resolvedActiveProps, resolvedInactiveProps, rest, {
2602
2574
  href: disabled ? undefined : next.href,
2603
- onClick: composeHandlers([handleClick, onClick]),
2575
+ onClick: composeHandlers([reactHandleClick, onClick]),
2604
2576
  onFocus: composeHandlers([handleFocus, onFocus]),
2605
2577
  onMouseEnter: composeHandlers([handleEnter, onMouseEnter]),
2606
2578
  onMouseLeave: composeHandlers([handleLeave, onMouseLeave]),
@@ -2680,18 +2652,12 @@ function createReactRouter(opts) {
2680
2652
  const routeExt = makeRouteExt(route, router);
2681
2653
  Object.assign(route, routeExt);
2682
2654
  },
2683
- createElement: async element => {
2684
- if (typeof element === 'function') {
2685
- const res = await element(); // Support direct import() calls
2686
-
2687
- if (typeof res === 'object' && res.default) {
2688
- return /*#__PURE__*/React.createElement(res.default);
2689
- } else {
2690
- return res;
2691
- }
2655
+ loadComponent: async component => {
2656
+ if (component.preload && typeof document !== 'undefined') {
2657
+ component.preload(); // return await component.preload()
2692
2658
  }
2693
2659
 
2694
- return element;
2660
+ return component;
2695
2661
  }
2696
2662
  }));
2697
2663
  return coreRouter;
@@ -2728,64 +2694,43 @@ function useMatches() {
2728
2694
  return React.useContext(matchesContext);
2729
2695
  }
2730
2696
  function Outlet() {
2731
- var _match$options$catchE;
2697
+ var _ref3, _match$__$pendingComp, _match$__$errorCompon;
2732
2698
 
2733
2699
  const router = useRouter();
2734
2700
  const matches = useMatches().slice(1);
2735
2701
  const match = matches[0];
2702
+ const defaultPending = React.useCallback(() => null, []);
2736
2703
 
2737
2704
  if (!match) {
2738
2705
  return null;
2739
2706
  }
2740
2707
 
2741
- const element = (() => {
2742
- var _match$__$errorElemen, _ref4, _ref5;
2743
-
2744
- if (!match) {
2745
- return null;
2746
- }
2747
-
2748
- const errorElement = (_match$__$errorElemen = match.__.errorElement) != null ? _match$__$errorElemen : router.options.defaultErrorElement;
2749
-
2708
+ const PendingComponent = (_ref3 = (_match$__$pendingComp = match.__.pendingComponent) != null ? _match$__$pendingComp : router.options.defaultPendingComponent) != null ? _ref3 : defaultPending;
2709
+ const errorComponent = (_match$__$errorCompon = match.__.errorComponent) != null ? _match$__$errorCompon : router.options.defaultErrorComponent;
2710
+ return /*#__PURE__*/React.createElement(MatchesProvider, {
2711
+ value: matches
2712
+ }, /*#__PURE__*/React.createElement(React.Suspense, {
2713
+ fallback: /*#__PURE__*/React.createElement(PendingComponent, null)
2714
+ }, /*#__PURE__*/React.createElement(CatchBoundary, {
2715
+ errorComponent: errorComponent
2716
+ }, (() => {
2750
2717
  if (match.status === 'error') {
2751
- if (errorElement) {
2752
- return errorElement;
2753
- }
2754
-
2755
- if (match.options.useErrorBoundary || router.options.useErrorBoundary) {
2756
- throw match.error;
2757
- }
2758
-
2759
- return /*#__PURE__*/React.createElement(DefaultErrorBoundary, {
2760
- error: match.error
2761
- });
2718
+ throw match.error;
2762
2719
  }
2763
2720
 
2764
- if (match.status === 'loading' || match.status === 'idle') {
2765
- if (match.isPending) {
2766
- var _match$__$pendingElem;
2721
+ if (match.status === 'success') {
2722
+ var _ref4, _ref5;
2767
2723
 
2768
- const pendingElement = (_match$__$pendingElem = match.__.pendingElement) != null ? _match$__$pendingElem : router.options.defaultPendingElement;
2769
-
2770
- if (match.options.pendingMs || pendingElement) {
2771
- var _ref3;
2772
-
2773
- return (_ref3 = pendingElement) != null ? _ref3 : null;
2774
- }
2775
- }
2776
-
2777
- return null;
2724
+ return /*#__PURE__*/React.createElement((_ref4 = (_ref5 = match.__.component) != null ? _ref5 : router.options.defaultComponent) != null ? _ref4 : Outlet);
2778
2725
  }
2779
2726
 
2780
- return (_ref4 = (_ref5 = match.__.element) != null ? _ref5 : router.options.defaultElement) != null ? _ref4 : /*#__PURE__*/React.createElement(Outlet, null);
2781
- })();
2727
+ if (match.__.loadPromise) {
2728
+ console.log(match.matchId, 'suspend');
2729
+ throw match.__.loadPromise;
2730
+ }
2782
2731
 
2783
- const catchElement = (_match$options$catchE = match == null ? void 0 : match.options.catchElement) != null ? _match$options$catchE : router.options.defaultCatchElement;
2784
- return /*#__PURE__*/React.createElement(MatchesProvider, {
2785
- value: matches
2786
- }, /*#__PURE__*/React.createElement(CatchBoundary, {
2787
- catchElement: catchElement
2788
- }, element));
2732
+ invariant(false, 'This should never happen!');
2733
+ })())));
2789
2734
  }
2790
2735
 
2791
2736
  class CatchBoundary extends React.Component {
@@ -2805,12 +2750,12 @@ class CatchBoundary extends React.Component {
2805
2750
  }
2806
2751
 
2807
2752
  render() {
2808
- var _this$props$catchElem;
2753
+ var _this$props$errorComp;
2809
2754
 
2810
- const catchElement = (_this$props$catchElem = this.props.catchElement) != null ? _this$props$catchElem : DefaultErrorBoundary;
2755
+ const errorComponent = (_this$props$errorComp = this.props.errorComponent) != null ? _this$props$errorComp : DefaultErrorBoundary;
2811
2756
 
2812
2757
  if (this.state.error) {
2813
- return typeof catchElement === 'function' ? catchElement(this.state) : catchElement;
2758
+ return /*#__PURE__*/React.createElement(errorComponent, this.state);
2814
2759
  }
2815
2760
 
2816
2761
  return this.props.children;