@tanstack/react-router 1.16.2 → 1.16.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.
@@ -554,111 +554,94 @@ class Router {
554
554
  };
555
555
  });
556
556
  };
557
- try {
558
- for (let [index, match] of matches.entries()) {
559
- const parentMatch = matches[index - 1];
560
- const route2 = this.looseRoutesById[match.routeId];
561
- const abortController = new AbortController();
562
- const handleErrorAndRedirect = (err, code) => {
563
- var _a2, _b2;
564
- err.routerCode = code;
565
- firstBadMatchIndex = firstBadMatchIndex ?? index;
566
- if (redirects.isRedirect(err)) {
567
- throw err;
568
- }
569
- if (notFound.isNotFound(err)) {
570
- this.updateMatchesWithNotFound(matches, match, err);
571
- }
572
- try {
573
- (_b2 = (_a2 = route2.options).onError) == null ? void 0 : _b2.call(_a2, err);
574
- } catch (errorHandlerErr) {
575
- err = errorHandlerErr;
576
- if (redirects.isRedirect(errorHandlerErr)) {
577
- throw errorHandlerErr;
578
- }
579
- }
580
- matches[index] = match = {
581
- ...match,
582
- error: err,
583
- status: "error",
584
- updatedAt: Date.now(),
585
- abortController: new AbortController()
586
- };
587
- };
557
+ for (let [index, match] of matches.entries()) {
558
+ const parentMatch = matches[index - 1];
559
+ const route2 = this.looseRoutesById[match.routeId];
560
+ const abortController = new AbortController();
561
+ const handleError = (err, code) => {
562
+ var _a2, _b2;
563
+ err.routerCode = code;
564
+ firstBadMatchIndex = firstBadMatchIndex ?? index;
565
+ if (redirects.isRedirect(err)) {
566
+ throw err;
567
+ }
568
+ if (notFound.isNotFound(err)) {
569
+ err.routeId = match.routeId;
570
+ throw err;
571
+ }
588
572
  try {
589
- if (match.paramsError) {
590
- handleErrorAndRedirect(match.paramsError, "PARSE_PARAMS");
591
- }
592
- if (match.searchError) {
593
- handleErrorAndRedirect(match.searchError, "VALIDATE_SEARCH");
573
+ (_b2 = (_a2 = route2.options).onError) == null ? void 0 : _b2.call(_a2, err);
574
+ } catch (errorHandlerErr) {
575
+ err = errorHandlerErr;
576
+ if (redirects.isRedirect(errorHandlerErr)) {
577
+ throw errorHandlerErr;
594
578
  }
595
- const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? this.options.context ?? {};
596
- const pendingMs = route2.options.pendingMs ?? this.options.defaultPendingMs;
597
- const pendingPromise = typeof pendingMs === "number" && pendingMs <= 0 ? Promise.resolve() : new Promise((r) => setTimeout(r, pendingMs));
598
- const beforeLoadContext = await ((_b = (_a = route2.options).beforeLoad) == null ? void 0 : _b.call(_a, {
599
- search: match.search,
600
- abortController,
601
- params: match.params,
602
- preload: !!preload,
603
- context: parentContext,
604
- location: this.state.location,
605
- // TOOD: just expose state and router, etc
606
- navigate: (opts) => this.navigate({ ...opts, from: match.pathname }),
607
- buildLocation: this.buildLocation,
608
- cause: preload ? "preload" : match.cause
609
- })) ?? {};
610
- if (redirects.isRedirect(beforeLoadContext)) {
611
- throw beforeLoadContext;
612
- }
613
- const context = {
614
- ...parentContext,
615
- ...beforeLoadContext
616
- };
617
- matches[index] = match = {
618
- ...match,
619
- routeContext: utils.replaceEqualDeep(
620
- match.routeContext,
621
- beforeLoadContext
622
- ),
623
- context: utils.replaceEqualDeep(match.context, context),
624
- abortController,
625
- pendingPromise
626
- };
627
- } catch (err) {
628
- handleErrorAndRedirect(err, "BEFORE_LOAD");
629
- break;
630
579
  }
580
+ matches[index] = match = {
581
+ ...match,
582
+ error: err,
583
+ status: "error",
584
+ updatedAt: Date.now(),
585
+ abortController: new AbortController()
586
+ };
587
+ };
588
+ try {
589
+ if (match.paramsError) {
590
+ handleError(match.paramsError, "PARSE_PARAMS");
591
+ }
592
+ if (match.searchError) {
593
+ handleError(match.searchError, "VALIDATE_SEARCH");
594
+ }
595
+ const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? this.options.context ?? {};
596
+ const pendingMs = route2.options.pendingMs ?? this.options.defaultPendingMs;
597
+ const pendingPromise = typeof pendingMs === "number" && pendingMs <= 0 ? Promise.resolve() : new Promise((r) => setTimeout(r, pendingMs));
598
+ const beforeLoadContext = await ((_b = (_a = route2.options).beforeLoad) == null ? void 0 : _b.call(_a, {
599
+ search: match.search,
600
+ abortController,
601
+ params: match.params,
602
+ preload: !!preload,
603
+ context: parentContext,
604
+ location: this.state.location,
605
+ // TOOD: just expose state and router, etc
606
+ navigate: (opts) => this.navigate({ ...opts, from: match.pathname }),
607
+ buildLocation: this.buildLocation,
608
+ cause: preload ? "preload" : match.cause
609
+ })) ?? {};
610
+ if (redirects.isRedirect(beforeLoadContext)) {
611
+ throw beforeLoadContext;
612
+ }
613
+ const context = {
614
+ ...parentContext,
615
+ ...beforeLoadContext
616
+ };
617
+ matches[index] = match = {
618
+ ...match,
619
+ routeContext: utils.replaceEqualDeep(match.routeContext, beforeLoadContext),
620
+ context: utils.replaceEqualDeep(match.context, context),
621
+ abortController,
622
+ pendingPromise
623
+ };
624
+ } catch (err) {
625
+ handleError(err, "BEFORE_LOAD");
626
+ break;
631
627
  }
632
- } catch (err) {
633
- if (redirects.isRedirect(err)) {
634
- if (!preload)
635
- this.navigate(err);
636
- return matches;
637
- }
638
- throw err;
639
628
  }
640
629
  const validResolvedMatches = matches.slice(0, firstBadMatchIndex);
641
630
  const matchPromises = [];
642
631
  validResolvedMatches.forEach((match, index) => {
643
632
  matchPromises.push(
644
- new Promise(async (resolve) => {
633
+ new Promise(async (resolve, reject) => {
645
634
  var _a2;
646
635
  const parentMatchPromise = matchPromises[index - 1];
647
636
  const route2 = this.looseRoutesById[match.routeId];
648
- const handleErrorAndRedirect = (err) => {
637
+ const handleError = (err) => {
649
638
  if (redirects.isRedirect(err)) {
650
- if (!preload) {
651
- this.navigate(err);
652
- }
653
- return true;
639
+ throw err;
654
640
  }
655
641
  if (notFound.isNotFound(err)) {
656
- if (!preload) {
657
- this.updateMatchesWithNotFound(matches, match, err);
658
- }
659
- return true;
642
+ err.routeId = match.routeId;
643
+ throw err;
660
644
  }
661
- return false;
662
645
  };
663
646
  let loadPromise;
664
647
  matches[index] = match = {
@@ -726,10 +709,7 @@ class Router {
726
709
  const loaderData = await loadPromise;
727
710
  if (latestPromise = checkLatest())
728
711
  return await latestPromise;
729
- if (redirects.isRedirect(loaderData) || notFound.isNotFound(loaderData)) {
730
- if (handleErrorAndRedirect(loaderData))
731
- return;
732
- }
712
+ handleError(loaderData);
733
713
  if (didShowPending && pendingMinMs) {
734
714
  await new Promise((r) => setTimeout(r, pendingMinMs));
735
715
  }
@@ -737,6 +717,7 @@ class Router {
737
717
  return await latestPromise;
738
718
  const [meta, headers] = await Promise.all([
739
719
  (_d = (_c = route2.options).meta) == null ? void 0 : _d.call(_c, {
720
+ params: match.params,
740
721
  loaderData
741
722
  }),
742
723
  (_f = (_e = route2.options).headers) == null ? void 0 : _f.call(_e, {
@@ -757,14 +738,12 @@ class Router {
757
738
  } catch (error) {
758
739
  if (latestPromise = checkLatest())
759
740
  return await latestPromise;
760
- if (handleErrorAndRedirect(error))
761
- return;
741
+ handleError(error);
762
742
  try {
763
743
  (_h = (_g = route2.options).onError) == null ? void 0 : _h.call(_g, error);
764
744
  } catch (onErrorError) {
765
745
  error = onErrorError;
766
- if (handleErrorAndRedirect(onErrorError))
767
- return;
746
+ handleError(onErrorError);
768
747
  }
769
748
  matches[index] = match = {
770
749
  ...match,
@@ -784,23 +763,27 @@ class Router {
784
763
  ...match,
785
764
  preload: !!preload && !this.state.matches.find((d) => d.id === match.id)
786
765
  };
787
- if (match.status !== "success") {
788
- if (shouldPending) {
789
- (_a2 = match.pendingPromise) == null ? void 0 : _a2.then(async () => {
790
- if (latestPromise = checkLatest())
791
- return latestPromise;
792
- didShowPending = true;
793
- matches[index] = match = {
794
- ...match,
795
- showPending: true
796
- };
797
- updateMatch(match);
798
- resolve();
799
- });
766
+ try {
767
+ if (match.status !== "success") {
768
+ if (shouldPending) {
769
+ (_a2 = match.pendingPromise) == null ? void 0 : _a2.then(async () => {
770
+ if (latestPromise = checkLatest())
771
+ return latestPromise;
772
+ didShowPending = true;
773
+ matches[index] = match = {
774
+ ...match,
775
+ showPending: true
776
+ };
777
+ updateMatch(match);
778
+ resolve();
779
+ });
780
+ }
781
+ await fetch();
782
+ } else if (match.invalid || (shouldReload ?? age > staleAge)) {
783
+ fetch();
800
784
  }
801
- await fetch();
802
- } else if (match.invalid || (shouldReload ?? age > staleAge)) {
803
- fetch();
785
+ } catch (err) {
786
+ reject(err);
804
787
  }
805
788
  resolve();
806
789
  })
@@ -862,6 +845,11 @@ class Router {
862
845
  checkLatest: () => this.checkLatest(promise)
863
846
  });
864
847
  } catch (err) {
848
+ if (redirects.isRedirect(err)) {
849
+ this.handleRedirect(err);
850
+ } else if (notFound.isNotFound(err)) {
851
+ this.handleNotFound(pendingMatches, err);
852
+ }
865
853
  }
866
854
  if (latestPromise = this.checkLatest(promise)) {
867
855
  return latestPromise;
@@ -915,6 +903,15 @@ class Router {
915
903
  this.latestLoadPromise = promise;
916
904
  return this.latestLoadPromise;
917
905
  };
906
+ this.handleRedirect = (err) => {
907
+ if (!err.href) {
908
+ err.href = this.buildLocation(err).href;
909
+ }
910
+ if (utils.isServer) {
911
+ throw err;
912
+ }
913
+ this.navigate(err);
914
+ };
918
915
  this.cleanCache = () => {
919
916
  this.__store.setState((s) => {
920
917
  return {
@@ -953,12 +950,19 @@ class Router {
953
950
  }
954
951
  });
955
952
  });
956
- matches = await this.loadMatches({
957
- matches,
958
- preload: true,
959
- checkLatest: () => void 0
960
- });
961
- return matches;
953
+ try {
954
+ matches = await this.loadMatches({
955
+ matches,
956
+ preload: true,
957
+ checkLatest: () => void 0
958
+ });
959
+ return matches;
960
+ } catch (err) {
961
+ if (!redirects.isRedirect(err) && !notFound.isNotFound(err)) {
962
+ console.error(err);
963
+ }
964
+ return void 0;
965
+ }
962
966
  };
963
967
  this.matchRoute = (location, opts) => {
964
968
  const matchLocation = {
@@ -1094,6 +1098,7 @@ class Router {
1094
1098
  ...match,
1095
1099
  ...dehydratedMatch,
1096
1100
  meta: (_b2 = (_a2 = route2.options).meta) == null ? void 0 : _b2.call(_a2, {
1101
+ params: match.params,
1097
1102
  loaderData: dehydratedMatch.loaderData
1098
1103
  }),
1099
1104
  links: (_d = (_c2 = route2.options).links) == null ? void 0 : _d.call(_c2),
@@ -1110,27 +1115,29 @@ class Router {
1110
1115
  };
1111
1116
  });
1112
1117
  };
1113
- this.updateMatchesWithNotFound = (matches, currentMatch, err) => {
1118
+ this.handleNotFound = (matches, err) => {
1114
1119
  const matchesByRouteId = Object.fromEntries(
1115
1120
  matches.map((match) => [match.routeId, match])
1116
1121
  );
1117
- if (err.global) {
1118
- matchesByRouteId[route.rootRouteId].notFoundError = err;
1119
- } else {
1120
- let currentRoute = this.routesById[err.route ?? currentMatch.routeId];
1121
- while (!currentRoute.options.notFoundComponent) {
1122
- currentRoute = currentRoute == null ? void 0 : currentRoute.parentRoute;
1123
- invariant(
1124
- currentRoute,
1125
- "Found invalid route tree while trying to find not-found handler."
1126
- );
1127
- if (currentRoute.id === route.rootRouteId)
1128
- break;
1122
+ if (!err.global && err.routeId) {
1123
+ let currentRoute = this.looseRoutesById[err.routeId];
1124
+ if (currentRoute) {
1125
+ while (!currentRoute.options.notFoundComponent) {
1126
+ currentRoute = currentRoute == null ? void 0 : currentRoute.parentRoute;
1127
+ invariant(
1128
+ currentRoute,
1129
+ "Found invalid route tree while trying to find not-found handler."
1130
+ );
1131
+ if (currentRoute.id === route.rootRouteId)
1132
+ break;
1133
+ }
1134
+ const match = matchesByRouteId[currentRoute.id];
1135
+ invariant(match, "Could not find match for route: " + currentRoute.id);
1136
+ match.notFoundError = err;
1137
+ return;
1129
1138
  }
1130
- const match = matchesByRouteId[currentRoute.id];
1131
- invariant(match, "Could not find match for route: " + currentRoute.id);
1132
- match.notFoundError = err;
1133
1139
  }
1140
+ matchesByRouteId[route.rootRouteId].notFoundError = err;
1134
1141
  };
1135
1142
  this.hasNotFoundMatch = () => {
1136
1143
  return this.__store.state.matches.some((d) => d.notFoundError);