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