@tanstack/react-router 1.30.1 → 1.31.1

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.
Files changed (58) hide show
  1. package/dist/cjs/Matches.cjs.map +1 -1
  2. package/dist/cjs/Matches.d.cts +10 -10
  3. package/dist/cjs/RouterProvider.cjs.map +1 -1
  4. package/dist/cjs/RouterProvider.d.cts +7 -7
  5. package/dist/cjs/fileRoute.d.cts +1 -1
  6. package/dist/cjs/link.cjs.map +1 -1
  7. package/dist/cjs/link.d.cts +42 -42
  8. package/dist/cjs/redirects.cjs.map +1 -1
  9. package/dist/cjs/redirects.d.cts +5 -6
  10. package/dist/cjs/route.cjs.map +1 -1
  11. package/dist/cjs/route.d.cts +6 -6
  12. package/dist/cjs/routeInfo.d.cts +19 -2
  13. package/dist/cjs/router.cjs +69 -52
  14. package/dist/cjs/router.cjs.map +1 -1
  15. package/dist/cjs/router.d.cts +15 -15
  16. package/dist/cjs/routerContext.cjs.map +1 -1
  17. package/dist/cjs/routerContext.d.cts +1 -1
  18. package/dist/cjs/useNavigate.cjs.map +1 -1
  19. package/dist/cjs/useNavigate.d.cts +3 -4
  20. package/dist/cjs/useRouter.cjs.map +1 -1
  21. package/dist/cjs/useRouter.d.cts +3 -4
  22. package/dist/cjs/useRouterState.cjs.map +1 -1
  23. package/dist/cjs/useRouterState.d.cts +3 -4
  24. package/dist/esm/Matches.d.ts +10 -10
  25. package/dist/esm/Matches.js.map +1 -1
  26. package/dist/esm/RouterProvider.d.ts +7 -7
  27. package/dist/esm/RouterProvider.js.map +1 -1
  28. package/dist/esm/fileRoute.d.ts +1 -1
  29. package/dist/esm/link.d.ts +42 -42
  30. package/dist/esm/link.js.map +1 -1
  31. package/dist/esm/redirects.d.ts +5 -6
  32. package/dist/esm/redirects.js.map +1 -1
  33. package/dist/esm/route.d.ts +6 -6
  34. package/dist/esm/route.js.map +1 -1
  35. package/dist/esm/routeInfo.d.ts +19 -2
  36. package/dist/esm/router.d.ts +15 -15
  37. package/dist/esm/router.js +69 -52
  38. package/dist/esm/router.js.map +1 -1
  39. package/dist/esm/routerContext.d.ts +1 -1
  40. package/dist/esm/routerContext.js.map +1 -1
  41. package/dist/esm/useNavigate.d.ts +3 -4
  42. package/dist/esm/useNavigate.js.map +1 -1
  43. package/dist/esm/useRouter.d.ts +3 -4
  44. package/dist/esm/useRouter.js.map +1 -1
  45. package/dist/esm/useRouterState.d.ts +3 -4
  46. package/dist/esm/useRouterState.js.map +1 -1
  47. package/package.json +1 -1
  48. package/src/Matches.tsx +40 -22
  49. package/src/RouterProvider.tsx +34 -11
  50. package/src/link.tsx +124 -139
  51. package/src/redirects.ts +14 -14
  52. package/src/route.ts +3 -3
  53. package/src/routeInfo.ts +72 -4
  54. package/src/router.ts +132 -68
  55. package/src/routerContext.tsx +1 -1
  56. package/src/useNavigate.tsx +9 -10
  57. package/src/useRouter.tsx +4 -5
  58. package/src/useRouterState.tsx +5 -6
@@ -30,7 +30,6 @@ class Router {
30
30
  )}`;
31
31
  this.resetNextScroll = true;
32
32
  this.shouldViewTransition = void 0;
33
- this.navigateTimeout = null;
34
33
  this.latestLoadPromise = Promise.resolve();
35
34
  this.subscribers = /* @__PURE__ */ new Set();
36
35
  this.injectedHtml = [];
@@ -398,20 +397,20 @@ class Router {
398
397
  };
399
398
  this.buildLocation = (opts) => {
400
399
  const build = (dest = {}, matches) => {
401
- var _a, _b, _c, _d;
402
- const fromPath = dest.from || this.latestLocation.pathname;
403
- let fromSearch = ((_a = dest._fromLocation) == null ? void 0 : _a.search) || this.latestLocation.search;
404
- const fromMatches = this.matchRoutes(fromPath, fromSearch);
400
+ var _a, _b, _c;
401
+ let fromPath = this.latestLocation.pathname;
402
+ let fromSearch = dest.fromSearch || this.latestLocation.search;
403
+ const fromMatches = this.matchRoutes(
404
+ this.latestLocation.pathname,
405
+ fromSearch
406
+ );
407
+ fromPath = ((_a = fromMatches.find((d) => d.id === dest.from)) == null ? void 0 : _a.pathname) || fromPath;
405
408
  fromSearch = ((_b = utils.last(fromMatches)) == null ? void 0 : _b.search) || this.latestLocation.search;
406
409
  const stayingMatches = matches == null ? void 0 : matches.filter(
407
410
  (d) => fromMatches.find((e) => e.routeId === d.routeId)
408
411
  );
409
- const fromRoute = this.looseRoutesById[(_c = utils.last(fromMatches)) == null ? void 0 : _c.routeId];
410
- let pathname = dest.to ? this.resolvePathWithBase(
411
- dest.from ?? this.latestLocation.pathname,
412
- `${dest.to}`
413
- ) : this.resolvePathWithBase(fromRoute == null ? void 0 : fromRoute.fullPath, fromRoute == null ? void 0 : fromRoute.fullPath);
414
- const prevParams = { ...(_d = utils.last(fromMatches)) == null ? void 0 : _d.params };
412
+ let pathname = dest.to ? this.resolvePathWithBase(fromPath, `${dest.to}`) : this.resolvePathWithBase(fromPath, fromPath);
413
+ const prevParams = { ...(_c = utils.last(fromMatches)) == null ? void 0 : _c.params };
415
414
  let nextParams = (dest.params ?? true) === true ? prevParams : { ...prevParams, ...utils.functionalUpdate(dest.params, prevParams) };
416
415
  if (Object.keys(nextParams).length > 0) {
417
416
  matches == null ? void 0 : matches.map((d) => this.looseRoutesById[d.routeId].options.stringifyParams).filter(Boolean).forEach((fn) => {
@@ -505,8 +504,6 @@ class Router {
505
504
  viewTransition,
506
505
  ...next
507
506
  }) => {
508
- if (this.navigateTimeout)
509
- clearTimeout(this.navigateTimeout);
510
507
  const isSameUrl = this.latestLocation.href === next.href;
511
508
  if (!isSameUrl) {
512
509
  let { maskedLocation, ...nextHistory } = next;
@@ -620,14 +617,10 @@ class Router {
620
617
  err.routeId = match.routeId;
621
618
  }
622
619
  if (redirects.isRedirect(err)) {
623
- const redirect = this.resolveRedirect(err);
624
- if (!preload && !this.isServer) {
625
- this.navigate({ ...redirect, replace: true });
626
- }
627
- throw redirect;
620
+ err = this.resolveRedirect(err);
621
+ throw err;
628
622
  } else if (notFound.isNotFound(err)) {
629
- if (!preload)
630
- this.handleNotFound(matches, err);
623
+ this.handleNotFound(matches, err);
631
624
  throw err;
632
625
  }
633
626
  }
@@ -636,6 +629,20 @@ class Router {
636
629
  const parentMatch = matches[index - 1];
637
630
  const route2 = this.looseRoutesById[match.routeId];
638
631
  const abortController = new AbortController();
632
+ let loadPromise = match.loadPromise;
633
+ if (match.isFetching) {
634
+ continue;
635
+ }
636
+ const previousResolve = loadPromise.resolve;
637
+ loadPromise = utils.createControlledPromise(
638
+ // Resolve the old when we we resolve the new one
639
+ previousResolve
640
+ );
641
+ matches[index] = match = updateMatch(match.id, (prev) => ({
642
+ ...prev,
643
+ isFetching: "beforeLoad",
644
+ loadPromise
645
+ }));
639
646
  const handleSerialError = (err, routerCode) => {
640
647
  var _a2, _b2;
641
648
  err.routerCode = routerCode;
@@ -687,6 +694,8 @@ class Router {
687
694
  buildLocation: this.buildLocation,
688
695
  cause: preload ? "preload" : match.cause
689
696
  })) ?? {};
697
+ if (latestPromise = checkLatest())
698
+ return latestPromise;
690
699
  if (redirects.isRedirect(beforeLoadContext) || notFound.isNotFound(beforeLoadContext)) {
691
700
  handleSerialError(beforeLoadContext, "BEFORE_LOAD");
692
701
  }
@@ -710,6 +719,8 @@ class Router {
710
719
  updateMatch(match.id, () => match);
711
720
  }
712
721
  }
722
+ if (latestPromise = checkLatest())
723
+ return latestPromise;
713
724
  const validResolvedMatches = matches.slice(0, firstBadMatchIndex);
714
725
  const matchPromises = [];
715
726
  await Promise.all(
@@ -734,7 +745,6 @@ class Router {
734
745
  let lazyPromise = Promise.resolve();
735
746
  let componentsPromise = Promise.resolve();
736
747
  let loaderPromise = existing.loaderPromise;
737
- let loadPromise = existing.loadPromise;
738
748
  const potentialPendingMinPromise = async () => {
739
749
  const latestMatch = RouterProvider.getRouteMatch(this.state, match.id);
740
750
  if (latestMatch == null ? void 0 : latestMatch.minPendingPromise) {
@@ -748,12 +758,15 @@ class Router {
748
758
  }
749
759
  };
750
760
  try {
751
- if (!match.isFetching) {
752
- matches[index] = match = {
753
- ...match,
754
- isFetching: true,
755
- fetchCount: match.fetchCount + 1
756
- };
761
+ if (match.isFetching === "beforeLoad") {
762
+ matches[index] = match = updateMatch(
763
+ match.id,
764
+ (prev) => ({
765
+ ...prev,
766
+ isFetching: "loader",
767
+ fetchCount: match.fetchCount + 1
768
+ })
769
+ );
757
770
  lazyPromise = ((_a2 = route2.lazyFn) == null ? void 0 : _a2.call(route2).then((lazyRoute) => {
758
771
  Object.assign(route2.options, lazyRoute.options);
759
772
  })) || Promise.resolve();
@@ -771,17 +784,14 @@ class Router {
771
784
  if (latestPromise = checkLatest())
772
785
  return await latestPromise;
773
786
  loaderPromise = (_c = (_b2 = route2.options).loader) == null ? void 0 : _c.call(_b2, loaderContext);
774
- const previousResolve = loadPromise.resolve;
775
- loadPromise = utils.createControlledPromise(
776
- // Resolve the old when we we resolve the new one
777
- previousResolve
787
+ matches[index] = match = updateMatch(
788
+ match.id,
789
+ (prev) => ({
790
+ ...prev,
791
+ loaderPromise
792
+ })
778
793
  );
779
794
  }
780
- matches[index] = match = updateMatch(match.id, (prev) => ({
781
- ...prev,
782
- loaderPromise,
783
- loadPromise
784
- }));
785
795
  const loaderData = await loaderPromise;
786
796
  if (latestPromise = checkLatest())
787
797
  return await latestPromise;
@@ -832,7 +842,7 @@ class Router {
832
842
  await componentsPromise;
833
843
  if (latestPromise = checkLatest())
834
844
  return await latestPromise;
835
- loadPromise.resolve();
845
+ match.loadPromise.resolve();
836
846
  };
837
847
  const age = Date.now() - match.updatedAt;
838
848
  const staleAge = preload ? route2.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route2.options.staleTime ?? this.options.defaultStaleTime ?? 0;
@@ -932,21 +942,28 @@ class Router {
932
942
  });
933
943
  let redirect;
934
944
  let notFound$1;
935
- try {
936
- const loadMatchesPromise = this.loadMatches({
937
- matches: pendingMatches,
938
- location: next,
939
- checkLatest: () => this.checkLatest(promise)
940
- });
941
- if (previousMatches.length || this.isServer) {
942
- await loadMatchesPromise;
943
- }
944
- } catch (err) {
945
- if (redirects.isRedirect(err)) {
946
- redirect = err;
947
- } else if (notFound.isNotFound(err)) {
948
- notFound$1 = err;
945
+ const loadMatches = () => this.loadMatches({
946
+ matches: pendingMatches,
947
+ location: next,
948
+ checkLatest: () => this.checkLatest(promise)
949
+ });
950
+ if (previousMatches.length || this.isServer) {
951
+ try {
952
+ await loadMatches();
953
+ } catch (err) {
954
+ if (redirects.isRedirect(err)) {
955
+ redirect = err;
956
+ } else if (notFound.isNotFound(err)) {
957
+ notFound$1 = err;
958
+ }
949
959
  }
960
+ } else {
961
+ loadMatches().catch((err) => {
962
+ if (redirects.isRedirect(err)) {
963
+ this.navigate({ ...err, replace: true });
964
+ }
965
+ this.load();
966
+ });
950
967
  }
951
968
  if (latestPromise = this.checkLatest(promise)) {
952
969
  return latestPromise;
@@ -1063,7 +1080,7 @@ class Router {
1063
1080
  } catch (err) {
1064
1081
  if (redirects.isRedirect(err)) {
1065
1082
  return await this.preloadRoute({
1066
- _fromDest: next,
1083
+ fromSearch: next.search,
1067
1084
  from: next.pathname,
1068
1085
  ...err
1069
1086
  });