@tanstack/react-router 1.28.3 → 1.28.5

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.
@@ -595,42 +595,51 @@ class Router {
595
595
  });
596
596
  return updated;
597
597
  };
598
- const handleMatchSpecialError = (match, err) => {
599
- updateMatch(match.id, (prev) => ({
600
- ...prev,
601
- status: isRedirect(err) ? "redirected" : isNotFound(err) ? "notFound" : "error",
602
- isFetching: false,
603
- error: err
604
- }));
605
- if (!err.routeId) {
606
- err.routeId = match.routeId;
607
- }
608
- throw err;
609
- };
610
598
  try {
611
599
  await new Promise((resolveAll, rejectAll) => {
612
600
  ;
613
601
  (async () => {
614
602
  var _a, _b;
615
603
  try {
604
+ const handleRedirectAndNotFound = (match, err) => {
605
+ if (isRedirect(err) || isNotFound(err)) {
606
+ updateMatch(match.id, (prev) => ({
607
+ ...prev,
608
+ status: isRedirect(err) ? "redirected" : isNotFound(err) ? "notFound" : "error",
609
+ isFetching: false,
610
+ error: err
611
+ }));
612
+ if (!err.routeId) {
613
+ ;
614
+ err.routeId = match.routeId;
615
+ }
616
+ if (isRedirect(err)) {
617
+ const redirect = this.resolveRedirect(err);
618
+ if (!preload && !this.isServer) {
619
+ this.navigate({ ...redirect, replace: true });
620
+ }
621
+ throw redirect;
622
+ } else if (isNotFound(err)) {
623
+ if (!preload)
624
+ this.handleNotFound(matches, err);
625
+ throw err;
626
+ }
627
+ }
628
+ };
616
629
  for (let [index, match] of matches.entries()) {
617
630
  const parentMatch = matches[index - 1];
618
631
  const route = this.looseRoutesById[match.routeId];
619
632
  const abortController = new AbortController();
620
- const handleSerialError = (err, code) => {
633
+ const handleSerialError = (err, routerCode) => {
621
634
  var _a2, _b2;
622
- err.routerCode = code;
635
+ err.routerCode = routerCode;
623
636
  firstBadMatchIndex = firstBadMatchIndex ?? index;
624
- if (isRedirect(err) || isNotFound(err)) {
625
- handleMatchSpecialError(match, err);
626
- }
637
+ handleRedirectAndNotFound(match, err);
627
638
  try {
628
639
  (_b2 = (_a2 = route.options).onError) == null ? void 0 : _b2.call(_a2, err);
629
640
  } catch (errorHandlerErr) {
630
641
  err = errorHandlerErr;
631
- if (isRedirect(err) || isNotFound(err)) {
632
- handleMatchSpecialError(match, errorHandlerErr);
633
- }
642
+ handleRedirectAndNotFound(match, err);
634
643
  }
635
644
  matches[index] = match = {
636
645
  ...match,
@@ -679,15 +688,15 @@ class Router {
679
688
  ...parentContext,
680
689
  ...beforeLoadContext
681
690
  };
682
- matches[index] = match = {
683
- ...match,
691
+ matches[index] = match = updateMatch(match.id, (prev) => ({
692
+ ...prev,
684
693
  routeContext: replaceEqualDeep(
685
694
  match.routeContext,
686
695
  beforeLoadContext
687
696
  ),
688
697
  context: replaceEqualDeep(match.context, context),
689
698
  abortController
690
- };
699
+ }));
691
700
  } catch (err) {
692
701
  handleSerialError(err, "BEFORE_LOAD");
693
702
  break;
@@ -699,11 +708,6 @@ class Router {
699
708
  validResolvedMatches.map(async (match, index) => {
700
709
  const parentMatchPromise = matchPromises[index - 1];
701
710
  const route = this.looseRoutesById[match.routeId];
702
- const handleError = (err) => {
703
- if (isRedirect(err) || isNotFound(err)) {
704
- handleMatchSpecialError(match, err);
705
- }
706
- };
707
711
  const loaderContext = {
708
712
  params: match.params,
709
713
  deps: match.loaderDeps,
@@ -773,7 +777,7 @@ class Router {
773
777
  const loaderData = await loaderPromise;
774
778
  if (latestPromise = checkLatest())
775
779
  return await latestPromise;
776
- handleError(loaderData);
780
+ handleRedirectAndNotFound(match, loaderData);
777
781
  if (latestPromise = checkLatest())
778
782
  return await latestPromise;
779
783
  await potentialPendingMinPromise();
@@ -803,12 +807,12 @@ class Router {
803
807
  await potentialPendingMinPromise();
804
808
  if (latestPromise = checkLatest())
805
809
  return await latestPromise;
806
- handleError(e);
810
+ handleRedirectAndNotFound(match, e);
807
811
  try {
808
812
  (_i = (_h = route.options).onError) == null ? void 0 : _i.call(_h, e);
809
813
  } catch (onErrorError) {
810
814
  error = onErrorError;
811
- handleError(onErrorError);
815
+ handleRedirectAndNotFound(match, onErrorError);
812
816
  }
813
817
  matches[index] = match = updateMatch(match.id, (prev) => ({
814
818
  ...prev,
@@ -830,32 +834,21 @@ class Router {
830
834
  ...match,
831
835
  preload: !!preload && !this.state.matches.find((d) => d.id === match.id)
832
836
  };
833
- const fetchWithRedirect = async () => {
837
+ const fetchWithRedirectAndNotFound = async () => {
834
838
  try {
835
839
  await fetch();
836
840
  } catch (err) {
837
841
  if (latestPromise = checkLatest())
838
842
  return await latestPromise;
839
- if (isRedirect(err)) {
840
- const redirect = this.resolveRedirect(err);
841
- if (!preload && !this.isServer) {
842
- this.navigate({ ...redirect, replace: true });
843
- }
844
- throw redirect;
845
- } else if (isNotFound(err)) {
846
- if (!preload)
847
- this.handleNotFound(matches, err);
848
- throw err;
849
- }
850
- handleError(err);
843
+ handleRedirectAndNotFound(match, err);
851
844
  }
852
845
  };
853
846
  if (match.status === "success" && (match.invalid || (shouldReload ?? age > staleAge))) {
854
- fetchWithRedirect();
847
+ fetchWithRedirectAndNotFound();
855
848
  return;
856
849
  }
857
850
  if (match.status !== "success") {
858
- await fetchWithRedirect();
851
+ await fetchWithRedirectAndNotFound();
859
852
  }
860
853
  })
861
854
  );