@tanstack/router-core 1.131.12 → 1.131.14

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.
@@ -1,7 +1,7 @@
1
1
  import { Store, batch } from "@tanstack/store";
2
2
  import { createMemoryHistory, createBrowserHistory, parseHref } from "@tanstack/history";
3
3
  import invariant from "tiny-invariant";
4
- import { pick, createControlledPromise, deepEqual, replaceEqualDeep, last, functionalUpdate } from "./utils.js";
4
+ import { pick, createControlledPromise, isPromise, deepEqual, replaceEqualDeep, last, functionalUpdate } from "./utils.js";
5
5
  import { trimPath, resolvePath, cleanPath, matchPathname, trimPathRight, interpolatePath, joinPaths, trimPathLeft, parsePathname, SEGMENT_TYPE_PARAM, SEGMENT_TYPE_OPTIONAL_PARAM, SEGMENT_TYPE_WILDCARD, SEGMENT_TYPE_PATHNAME } from "./path.js";
6
6
  import { isNotFound } from "./not-found.js";
7
7
  import { setupScrollRestoration } from "./scroll-restoration.js";
@@ -727,42 +727,36 @@ class RouterCore {
727
727
  }
728
728
  const handleRedirectAndNotFound = (match, err) => {
729
729
  var _a, _b, _c;
730
- if (isRedirect(err) || isNotFound(err)) {
731
- if (isRedirect(err)) {
732
- if (err.redirectHandled) {
733
- if (!err.options.reloadDocument) {
734
- throw err;
735
- }
736
- }
737
- }
738
- if (match) {
739
- (_a = match._nonReactive.beforeLoadPromise) == null ? void 0 : _a.resolve();
740
- (_b = match._nonReactive.loaderPromise) == null ? void 0 : _b.resolve();
741
- match._nonReactive.beforeLoadPromise = void 0;
742
- match._nonReactive.loaderPromise = void 0;
743
- updateMatch(match.id, (prev) => ({
744
- ...prev,
745
- status: isRedirect(err) ? "redirected" : isNotFound(err) ? "notFound" : "error",
746
- isFetching: false,
747
- error: err
748
- }));
749
- if (!err.routeId) {
750
- err.routeId = match.routeId;
751
- }
752
- (_c = match._nonReactive.loadPromise) == null ? void 0 : _c.resolve();
753
- }
754
- if (isRedirect(err)) {
755
- rendered = true;
756
- err.options._fromLocation = location;
757
- err.redirectHandled = true;
758
- err = this.resolveRedirect(err);
759
- throw err;
760
- } else if (isNotFound(err)) {
761
- this._handleNotFound(matches, err, {
762
- updateMatch
763
- });
764
- throw err;
730
+ if (!isRedirect(err) && !isNotFound(err)) return;
731
+ if (isRedirect(err) && err.redirectHandled && !err.options.reloadDocument) {
732
+ throw err;
733
+ }
734
+ if (match) {
735
+ (_a = match._nonReactive.beforeLoadPromise) == null ? void 0 : _a.resolve();
736
+ (_b = match._nonReactive.loaderPromise) == null ? void 0 : _b.resolve();
737
+ match._nonReactive.beforeLoadPromise = void 0;
738
+ match._nonReactive.loaderPromise = void 0;
739
+ const status = isRedirect(err) ? "redirected" : "notFound";
740
+ updateMatch(match.id, (prev) => ({
741
+ ...prev,
742
+ status,
743
+ isFetching: false,
744
+ error: err
745
+ }));
746
+ if (isNotFound(err) && !err.routeId) {
747
+ err.routeId = match.routeId;
765
748
  }
749
+ (_c = match._nonReactive.loadPromise) == null ? void 0 : _c.resolve();
750
+ }
751
+ if (isRedirect(err)) {
752
+ rendered = true;
753
+ err.options._fromLocation = location;
754
+ err.redirectHandled = true;
755
+ err = this.resolveRedirect(err);
756
+ throw err;
757
+ } else {
758
+ this._handleNotFound(matches, err, updateMatch);
759
+ throw err;
766
760
  }
767
761
  };
768
762
  const shouldSkipLoader = (matchId) => {
@@ -1015,11 +1009,9 @@ class RouterCore {
1015
1009
  };
1016
1010
  });
1017
1011
  };
1018
- const potentialPendingMinPromise = async () => {
1012
+ const potentialPendingMinPromise = () => {
1019
1013
  const latestMatch = this.getMatch(matchId);
1020
- if (latestMatch._nonReactive.minPendingPromise) {
1021
- await latestMatch._nonReactive.minPendingPromise;
1022
- }
1014
+ return latestMatch._nonReactive.minPendingPromise;
1023
1015
  };
1024
1016
  const prevMatch = this.getMatch(matchId);
1025
1017
  if (shouldSkipLoader(matchId)) {
@@ -1083,27 +1075,36 @@ class RouterCore {
1083
1075
  var _a3, _b3, _c2, _d2;
1084
1076
  try {
1085
1077
  try {
1086
- if (!this.isServer || this.isServer && this.getMatch(matchId).ssr === true) {
1078
+ if (!this.isServer || this.getMatch(matchId).ssr === true) {
1087
1079
  this.loadRouteChunk(route);
1088
1080
  }
1089
- updateMatch(matchId, (prev) => ({
1090
- ...prev,
1091
- isFetching: "loader"
1092
- }));
1093
- const loaderData = await ((_b3 = (_a3 = route.options).loader) == null ? void 0 : _b3.call(_a3, getLoaderContext()));
1094
- handleRedirectAndNotFound(
1095
- this.getMatch(matchId),
1096
- loaderData
1097
- );
1098
- updateMatch(matchId, (prev) => ({
1099
- ...prev,
1100
- loaderData
1101
- }));
1102
- await route._lazyPromise;
1081
+ const loaderResult = (_b3 = (_a3 = route.options).loader) == null ? void 0 : _b3.call(_a3, getLoaderContext());
1082
+ const loaderResultIsPromise = route.options.loader && isPromise(loaderResult);
1083
+ const willLoadSomething = !!(loaderResultIsPromise || route._lazyPromise || route._componentsPromise || route.options.head || route.options.scripts || route.options.headers || this.getMatch(matchId)._nonReactive.minPendingPromise);
1084
+ if (willLoadSomething) {
1085
+ updateMatch(matchId, (prev) => ({
1086
+ ...prev,
1087
+ isFetching: "loader"
1088
+ }));
1089
+ }
1090
+ if (route.options.loader) {
1091
+ const loaderData = loaderResultIsPromise ? await loaderResult : loaderResult;
1092
+ handleRedirectAndNotFound(
1093
+ this.getMatch(matchId),
1094
+ loaderData
1095
+ );
1096
+ updateMatch(matchId, (prev) => ({
1097
+ ...prev,
1098
+ loaderData
1099
+ }));
1100
+ }
1101
+ if (route._lazyPromise) await route._lazyPromise;
1103
1102
  const headResult = executeHead();
1104
1103
  const head = headResult ? await headResult : void 0;
1105
- await potentialPendingMinPromise();
1106
- await route._componentsPromise;
1104
+ const pendingPromise = potentialPendingMinPromise();
1105
+ if (pendingPromise) await pendingPromise;
1106
+ if (route._componentsPromise)
1107
+ await route._componentsPromise;
1107
1108
  updateMatch(matchId, (prev) => ({
1108
1109
  ...prev,
1109
1110
  error: void 0,
@@ -1291,27 +1292,36 @@ class RouterCore {
1291
1292
  this.clearCache({ filter });
1292
1293
  };
1293
1294
  this.loadRouteChunk = (route) => {
1294
- if (route._lazyPromise === void 0) {
1295
+ if (!route._lazyLoaded && route._lazyPromise === void 0) {
1295
1296
  if (route.lazyFn) {
1296
1297
  route._lazyPromise = route.lazyFn().then((lazyRoute) => {
1297
1298
  const { id: _id, ...options2 } = lazyRoute.options;
1298
1299
  Object.assign(route.options, options2);
1300
+ route._lazyLoaded = true;
1301
+ route._lazyPromise = void 0;
1299
1302
  });
1300
1303
  } else {
1301
- route._lazyPromise = Promise.resolve();
1304
+ route._lazyLoaded = true;
1302
1305
  }
1303
1306
  }
1304
- if (route._componentsPromise === void 0) {
1305
- route._componentsPromise = route._lazyPromise.then(
1306
- () => Promise.all(
1307
- componentTypes.map(async (type) => {
1308
- const component = route.options[type];
1309
- if (component == null ? void 0 : component.preload) {
1310
- await component.preload();
1311
- }
1312
- })
1313
- )
1314
- );
1307
+ if (!route._componentsLoaded && route._componentsPromise === void 0) {
1308
+ const loadComponents = () => {
1309
+ var _a;
1310
+ const preloads = [];
1311
+ for (const type of componentTypes) {
1312
+ const preload = (_a = route.options[type]) == null ? void 0 : _a.preload;
1313
+ if (preload) preloads.push(preload());
1314
+ }
1315
+ if (preloads.length)
1316
+ return Promise.all(preloads).then(() => {
1317
+ route._componentsLoaded = true;
1318
+ route._componentsPromise = void 0;
1319
+ });
1320
+ route._componentsLoaded = true;
1321
+ route._componentsPromise = void 0;
1322
+ return;
1323
+ };
1324
+ route._componentsPromise = route._lazyPromise ? route._lazyPromise.then(loadComponents) : loadComponents();
1315
1325
  }
1316
1326
  return route._componentsPromise;
1317
1327
  };
@@ -1409,9 +1419,7 @@ class RouterCore {
1409
1419
  }
1410
1420
  return match;
1411
1421
  };
1412
- this._handleNotFound = (matches, err, {
1413
- updateMatch = this.updateMatch
1414
- } = {}) => {
1422
+ this._handleNotFound = (matches, err, updateMatch = this.updateMatch) => {
1415
1423
  var _a;
1416
1424
  const routeCursor = this.routesById[err.routeId ?? ""] ?? this.routeTree;
1417
1425
  const matchesByRouteId = {};
@@ -1438,9 +1446,7 @@ class RouterCore {
1438
1446
  }));
1439
1447
  if (err.routerCode === "BEFORE_LOAD" && routeCursor.parentRoute) {
1440
1448
  err.routeId = routeCursor.parentRoute.id;
1441
- this._handleNotFound(matches, err, {
1442
- updateMatch
1443
- });
1449
+ this._handleNotFound(matches, err, updateMatch);
1444
1450
  }
1445
1451
  };
1446
1452
  this.hasNotFoundMatch = () => {