@tanstack/router-core 1.131.19 → 1.131.21

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, isPromise, deepEqual, replaceEqualDeep, last, functionalUpdate } from "./utils.js";
4
+ import { pick, createControlledPromise, 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";
@@ -9,6 +9,7 @@ import { defaultParseSearch, defaultStringifySearch } from "./searchParams.js";
9
9
  import { rootRouteId } from "./root.js";
10
10
  import { redirect, isRedirect } from "./redirect.js";
11
11
  import { createLRUCache } from "./lru-cache.js";
12
+ import { loadMatches, loadRouteChunk, routeNeedsPreload } from "./load-matches.js";
12
13
  function defaultSerializeError(err) {
13
14
  if (err instanceof Error) {
14
15
  const obj = {
@@ -570,10 +571,12 @@ class RouterCore {
570
571
  location: next
571
572
  })
572
573
  });
573
- await this.loadMatches({
574
+ await loadMatches({
575
+ router: this,
574
576
  sync: opts == null ? void 0 : opts.sync,
575
577
  matches: this.state.pendingMatches,
576
578
  location: next,
579
+ updateMatch: this.updateMatch,
577
580
  // eslint-disable-next-line @typescript-eslint/require-await
578
581
  onReady: async () => {
579
582
  this.startViewTransition(async () => {
@@ -703,611 +706,6 @@ class RouterCore {
703
706
  const findFn = (d) => d.id === matchId;
704
707
  return this.state.cachedMatches.find(findFn) ?? ((_a = this.state.pendingMatches) == null ? void 0 : _a.find(findFn)) ?? this.state.matches.find(findFn);
705
708
  };
706
- this.triggerOnReady = (innerLoadContext) => {
707
- var _a;
708
- if (!innerLoadContext.rendered) {
709
- innerLoadContext.rendered = true;
710
- return (_a = innerLoadContext.onReady) == null ? void 0 : _a.call(innerLoadContext);
711
- }
712
- };
713
- this.resolvePreload = (innerLoadContext, matchId) => {
714
- return !!(innerLoadContext.preload && !this.state.matches.some((d) => d.id === matchId));
715
- };
716
- this.handleRedirectAndNotFound = (innerLoadContext, match, err) => {
717
- var _a, _b, _c;
718
- if (!isRedirect(err) && !isNotFound(err)) return;
719
- if (isRedirect(err) && err.redirectHandled && !err.options.reloadDocument) {
720
- throw err;
721
- }
722
- if (match) {
723
- (_a = match._nonReactive.beforeLoadPromise) == null ? void 0 : _a.resolve();
724
- (_b = match._nonReactive.loaderPromise) == null ? void 0 : _b.resolve();
725
- match._nonReactive.beforeLoadPromise = void 0;
726
- match._nonReactive.loaderPromise = void 0;
727
- const status = isRedirect(err) ? "redirected" : "notFound";
728
- innerLoadContext.updateMatch(match.id, (prev) => ({
729
- ...prev,
730
- status,
731
- isFetching: false,
732
- error: err
733
- }));
734
- if (isNotFound(err) && !err.routeId) {
735
- err.routeId = match.routeId;
736
- }
737
- (_c = match._nonReactive.loadPromise) == null ? void 0 : _c.resolve();
738
- }
739
- if (isRedirect(err)) {
740
- innerLoadContext.rendered = true;
741
- err.options._fromLocation = innerLoadContext.location;
742
- err.redirectHandled = true;
743
- err = this.resolveRedirect(err);
744
- throw err;
745
- } else {
746
- this._handleNotFound(innerLoadContext, err);
747
- throw err;
748
- }
749
- };
750
- this.shouldSkipLoader = (matchId) => {
751
- const match = this.getMatch(matchId);
752
- if (!this.isServer && match._nonReactive.dehydrated) {
753
- return true;
754
- }
755
- if (this.isServer) {
756
- if (match.ssr === false) {
757
- return true;
758
- }
759
- }
760
- return false;
761
- };
762
- this.handleSerialError = (innerLoadContext, index, err, routerCode) => {
763
- var _a, _b;
764
- const { id: matchId, routeId } = innerLoadContext.matches[index];
765
- const route = this.looseRoutesById[routeId];
766
- if (err instanceof Promise) {
767
- throw err;
768
- }
769
- err.routerCode = routerCode;
770
- innerLoadContext.firstBadMatchIndex ?? (innerLoadContext.firstBadMatchIndex = index);
771
- this.handleRedirectAndNotFound(
772
- innerLoadContext,
773
- this.getMatch(matchId),
774
- err
775
- );
776
- try {
777
- (_b = (_a = route.options).onError) == null ? void 0 : _b.call(_a, err);
778
- } catch (errorHandlerErr) {
779
- err = errorHandlerErr;
780
- this.handleRedirectAndNotFound(
781
- innerLoadContext,
782
- this.getMatch(matchId),
783
- err
784
- );
785
- }
786
- innerLoadContext.updateMatch(matchId, (prev) => {
787
- var _a2, _b2;
788
- (_a2 = prev._nonReactive.beforeLoadPromise) == null ? void 0 : _a2.resolve();
789
- prev._nonReactive.beforeLoadPromise = void 0;
790
- (_b2 = prev._nonReactive.loadPromise) == null ? void 0 : _b2.resolve();
791
- return {
792
- ...prev,
793
- error: err,
794
- status: "error",
795
- isFetching: false,
796
- updatedAt: Date.now(),
797
- abortController: new AbortController()
798
- };
799
- });
800
- };
801
- this.isBeforeLoadSsr = (innerLoadContext, matchId, index, route) => {
802
- var _a;
803
- const existingMatch = this.getMatch(matchId);
804
- const parentMatchId = (_a = innerLoadContext.matches[index - 1]) == null ? void 0 : _a.id;
805
- const parentMatch = parentMatchId ? this.getMatch(parentMatchId) : void 0;
806
- if (this.isShell()) {
807
- existingMatch.ssr = matchId === rootRouteId;
808
- return;
809
- }
810
- if ((parentMatch == null ? void 0 : parentMatch.ssr) === false) {
811
- existingMatch.ssr = false;
812
- return;
813
- }
814
- const parentOverride = (tempSsr2) => {
815
- if (tempSsr2 === true && (parentMatch == null ? void 0 : parentMatch.ssr) === "data-only") {
816
- return "data-only";
817
- }
818
- return tempSsr2;
819
- };
820
- const defaultSsr = this.options.defaultSsr ?? true;
821
- if (route.options.ssr === void 0) {
822
- existingMatch.ssr = parentOverride(defaultSsr);
823
- return;
824
- }
825
- if (typeof route.options.ssr !== "function") {
826
- existingMatch.ssr = parentOverride(route.options.ssr);
827
- return;
828
- }
829
- const { search, params } = this.getMatch(matchId);
830
- const ssrFnContext = {
831
- search: makeMaybe(search, existingMatch.searchError),
832
- params: makeMaybe(params, existingMatch.paramsError),
833
- location: innerLoadContext.location,
834
- matches: innerLoadContext.matches.map((match) => ({
835
- index: match.index,
836
- pathname: match.pathname,
837
- fullPath: match.fullPath,
838
- staticData: match.staticData,
839
- id: match.id,
840
- routeId: match.routeId,
841
- search: makeMaybe(match.search, match.searchError),
842
- params: makeMaybe(match.params, match.paramsError),
843
- ssr: match.ssr
844
- }))
845
- };
846
- const tempSsr = route.options.ssr(ssrFnContext);
847
- if (isPromise(tempSsr)) {
848
- return tempSsr.then((ssr) => {
849
- existingMatch.ssr = parentOverride(ssr ?? defaultSsr);
850
- });
851
- }
852
- existingMatch.ssr = parentOverride(tempSsr ?? defaultSsr);
853
- return;
854
- };
855
- this.setupPendingTimeout = (innerLoadContext, matchId, route) => {
856
- var _a;
857
- const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
858
- const shouldPending = !!(innerLoadContext.onReady && !this.isServer && !this.resolvePreload(innerLoadContext, matchId) && (route.options.loader || route.options.beforeLoad || routeNeedsPreload(route)) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? ((_a = this.options) == null ? void 0 : _a.defaultPendingComponent)));
859
- const match = this.getMatch(matchId);
860
- if (shouldPending && match._nonReactive.pendingTimeout === void 0) {
861
- const pendingTimeout = setTimeout(() => {
862
- this.triggerOnReady(innerLoadContext);
863
- }, pendingMs);
864
- match._nonReactive.pendingTimeout = pendingTimeout;
865
- }
866
- };
867
- this.shouldExecuteBeforeLoad = (innerLoadContext, matchId, route) => {
868
- const existingMatch = this.getMatch(matchId);
869
- if (!existingMatch._nonReactive.beforeLoadPromise && !existingMatch._nonReactive.loaderPromise)
870
- return true;
871
- this.setupPendingTimeout(innerLoadContext, matchId, route);
872
- const then = () => {
873
- let shouldExecuteBeforeLoad = true;
874
- const match = this.getMatch(matchId);
875
- if (match.status === "error") {
876
- shouldExecuteBeforeLoad = true;
877
- } else if (match.preload && (match.status === "redirected" || match.status === "notFound")) {
878
- this.handleRedirectAndNotFound(innerLoadContext, match, match.error);
879
- }
880
- return shouldExecuteBeforeLoad;
881
- };
882
- return existingMatch._nonReactive.beforeLoadPromise ? existingMatch._nonReactive.beforeLoadPromise.then(then) : then();
883
- };
884
- this.executeBeforeLoad = (innerLoadContext, matchId, index, route) => {
885
- var _a;
886
- const match = this.getMatch(matchId);
887
- match._nonReactive.beforeLoadPromise = createControlledPromise();
888
- const prevLoadPromise = match._nonReactive.loadPromise;
889
- match._nonReactive.loadPromise = createControlledPromise(() => {
890
- prevLoadPromise == null ? void 0 : prevLoadPromise.resolve();
891
- });
892
- const { paramsError, searchError } = match;
893
- if (paramsError) {
894
- this.handleSerialError(
895
- innerLoadContext,
896
- index,
897
- paramsError,
898
- "PARSE_PARAMS"
899
- );
900
- }
901
- if (searchError) {
902
- this.handleSerialError(
903
- innerLoadContext,
904
- index,
905
- searchError,
906
- "VALIDATE_SEARCH"
907
- );
908
- }
909
- this.setupPendingTimeout(innerLoadContext, matchId, route);
910
- const abortController = new AbortController();
911
- const parentMatchId = (_a = innerLoadContext.matches[index - 1]) == null ? void 0 : _a.id;
912
- const parentMatch = parentMatchId ? this.getMatch(parentMatchId) : void 0;
913
- const parentMatchContext = (parentMatch == null ? void 0 : parentMatch.context) ?? this.options.context ?? void 0;
914
- const context = { ...parentMatchContext, ...match.__routeContext };
915
- let isPending = false;
916
- const pending = () => {
917
- if (isPending) return;
918
- isPending = true;
919
- innerLoadContext.updateMatch(matchId, (prev) => ({
920
- ...prev,
921
- isFetching: "beforeLoad",
922
- fetchCount: prev.fetchCount + 1,
923
- abortController,
924
- context
925
- }));
926
- };
927
- const resolve = () => {
928
- var _a2;
929
- (_a2 = match._nonReactive.beforeLoadPromise) == null ? void 0 : _a2.resolve();
930
- match._nonReactive.beforeLoadPromise = void 0;
931
- innerLoadContext.updateMatch(matchId, (prev) => ({
932
- ...prev,
933
- isFetching: false
934
- }));
935
- };
936
- if (!route.options.beforeLoad) {
937
- batch(() => {
938
- pending();
939
- resolve();
940
- });
941
- return;
942
- }
943
- const { search, params, cause } = match;
944
- const preload = this.resolvePreload(innerLoadContext, matchId);
945
- const beforeLoadFnContext = {
946
- search,
947
- abortController,
948
- params,
949
- preload,
950
- context,
951
- location: innerLoadContext.location,
952
- navigate: (opts) => this.navigate({ ...opts, _fromLocation: innerLoadContext.location }),
953
- buildLocation: this.buildLocation,
954
- cause: preload ? "preload" : cause,
955
- matches: innerLoadContext.matches
956
- };
957
- const updateContext = (beforeLoadContext2) => {
958
- if (beforeLoadContext2 === void 0) {
959
- batch(() => {
960
- pending();
961
- resolve();
962
- });
963
- return;
964
- }
965
- if (isRedirect(beforeLoadContext2) || isNotFound(beforeLoadContext2)) {
966
- pending();
967
- this.handleSerialError(
968
- innerLoadContext,
969
- index,
970
- beforeLoadContext2,
971
- "BEFORE_LOAD"
972
- );
973
- }
974
- batch(() => {
975
- pending();
976
- innerLoadContext.updateMatch(matchId, (prev) => ({
977
- ...prev,
978
- __beforeLoadContext: beforeLoadContext2,
979
- context: {
980
- ...prev.context,
981
- ...beforeLoadContext2
982
- }
983
- }));
984
- resolve();
985
- });
986
- };
987
- let beforeLoadContext;
988
- try {
989
- beforeLoadContext = route.options.beforeLoad(beforeLoadFnContext);
990
- if (isPromise(beforeLoadContext)) {
991
- pending();
992
- return beforeLoadContext.catch((err) => {
993
- this.handleSerialError(innerLoadContext, index, err, "BEFORE_LOAD");
994
- }).then(updateContext);
995
- }
996
- } catch (err) {
997
- pending();
998
- this.handleSerialError(innerLoadContext, index, err, "BEFORE_LOAD");
999
- }
1000
- updateContext(beforeLoadContext);
1001
- return;
1002
- };
1003
- this.handleBeforeLoad = (innerLoadContext, index) => {
1004
- const { id: matchId, routeId } = innerLoadContext.matches[index];
1005
- const route = this.looseRoutesById[routeId];
1006
- const serverSsr = () => {
1007
- if (this.isServer) {
1008
- const maybePromise = this.isBeforeLoadSsr(
1009
- innerLoadContext,
1010
- matchId,
1011
- index,
1012
- route
1013
- );
1014
- if (isPromise(maybePromise)) return maybePromise.then(queueExecution);
1015
- }
1016
- return queueExecution();
1017
- };
1018
- const queueExecution = () => {
1019
- if (this.shouldSkipLoader(matchId)) return;
1020
- const shouldExecuteBeforeLoadResult = this.shouldExecuteBeforeLoad(
1021
- innerLoadContext,
1022
- matchId,
1023
- route
1024
- );
1025
- return isPromise(shouldExecuteBeforeLoadResult) ? shouldExecuteBeforeLoadResult.then(execute) : execute(shouldExecuteBeforeLoadResult);
1026
- };
1027
- const execute = (shouldExecuteBeforeLoad) => {
1028
- if (shouldExecuteBeforeLoad) {
1029
- return this.executeBeforeLoad(innerLoadContext, matchId, index, route);
1030
- }
1031
- return;
1032
- };
1033
- return serverSsr();
1034
- };
1035
- this.executeHead = (innerLoadContext, matchId, route) => {
1036
- var _a, _b, _c, _d, _e, _f;
1037
- const match = this.getMatch(matchId);
1038
- if (!match) {
1039
- return;
1040
- }
1041
- if (!route.options.head && !route.options.scripts && !route.options.headers) {
1042
- return;
1043
- }
1044
- const assetContext = {
1045
- matches: innerLoadContext.matches,
1046
- match,
1047
- params: match.params,
1048
- loaderData: match.loaderData
1049
- };
1050
- return Promise.all([
1051
- (_b = (_a = route.options).head) == null ? void 0 : _b.call(_a, assetContext),
1052
- (_d = (_c = route.options).scripts) == null ? void 0 : _d.call(_c, assetContext),
1053
- (_f = (_e = route.options).headers) == null ? void 0 : _f.call(_e, assetContext)
1054
- ]).then(([headFnContent, scripts, headers]) => {
1055
- const meta = headFnContent == null ? void 0 : headFnContent.meta;
1056
- const links = headFnContent == null ? void 0 : headFnContent.links;
1057
- const headScripts = headFnContent == null ? void 0 : headFnContent.scripts;
1058
- const styles = headFnContent == null ? void 0 : headFnContent.styles;
1059
- return {
1060
- meta,
1061
- links,
1062
- headScripts,
1063
- headers,
1064
- scripts,
1065
- styles
1066
- };
1067
- });
1068
- };
1069
- this.potentialPendingMinPromise = (matchId) => {
1070
- const latestMatch = this.getMatch(matchId);
1071
- return latestMatch._nonReactive.minPendingPromise;
1072
- };
1073
- this.getLoaderContext = (innerLoadContext, matchId, index, route) => {
1074
- const parentMatchPromise = innerLoadContext.matchPromises[index - 1];
1075
- const { params, loaderDeps, abortController, context, cause } = this.getMatch(matchId);
1076
- const preload = this.resolvePreload(innerLoadContext, matchId);
1077
- return {
1078
- params,
1079
- deps: loaderDeps,
1080
- preload: !!preload,
1081
- parentMatchPromise,
1082
- abortController,
1083
- context,
1084
- location: innerLoadContext.location,
1085
- navigate: (opts) => this.navigate({ ...opts, _fromLocation: innerLoadContext.location }),
1086
- cause: preload ? "preload" : cause,
1087
- route
1088
- };
1089
- };
1090
- this.runLoader = async (innerLoadContext, matchId, index, route) => {
1091
- var _a, _b, _c, _d;
1092
- try {
1093
- try {
1094
- if (!this.isServer || this.getMatch(matchId).ssr === true) {
1095
- this.loadRouteChunk(route);
1096
- }
1097
- const loaderResult = (_b = (_a = route.options).loader) == null ? void 0 : _b.call(
1098
- _a,
1099
- this.getLoaderContext(innerLoadContext, matchId, index, route)
1100
- );
1101
- const loaderResultIsPromise = route.options.loader && isPromise(loaderResult);
1102
- const willLoadSomething = !!(loaderResultIsPromise || route._lazyPromise || route._componentsPromise || route.options.head || route.options.scripts || route.options.headers || this.getMatch(matchId)._nonReactive.minPendingPromise);
1103
- if (willLoadSomething) {
1104
- innerLoadContext.updateMatch(matchId, (prev) => ({
1105
- ...prev,
1106
- isFetching: "loader"
1107
- }));
1108
- }
1109
- if (route.options.loader) {
1110
- const loaderData = loaderResultIsPromise ? await loaderResult : loaderResult;
1111
- this.handleRedirectAndNotFound(
1112
- innerLoadContext,
1113
- this.getMatch(matchId),
1114
- loaderData
1115
- );
1116
- if (loaderData !== void 0) {
1117
- innerLoadContext.updateMatch(matchId, (prev) => ({
1118
- ...prev,
1119
- loaderData
1120
- }));
1121
- }
1122
- }
1123
- if (route._lazyPromise) await route._lazyPromise;
1124
- const headResult = this.executeHead(innerLoadContext, matchId, route);
1125
- const head = headResult ? await headResult : void 0;
1126
- const pendingPromise = this.potentialPendingMinPromise(matchId);
1127
- if (pendingPromise) await pendingPromise;
1128
- if (route._componentsPromise) await route._componentsPromise;
1129
- innerLoadContext.updateMatch(matchId, (prev) => ({
1130
- ...prev,
1131
- error: void 0,
1132
- status: "success",
1133
- isFetching: false,
1134
- updatedAt: Date.now(),
1135
- ...head
1136
- }));
1137
- } catch (e) {
1138
- let error = e;
1139
- const pendingPromise = this.potentialPendingMinPromise(matchId);
1140
- if (pendingPromise) await pendingPromise;
1141
- this.handleRedirectAndNotFound(
1142
- innerLoadContext,
1143
- this.getMatch(matchId),
1144
- e
1145
- );
1146
- try {
1147
- (_d = (_c = route.options).onError) == null ? void 0 : _d.call(_c, e);
1148
- } catch (onErrorError) {
1149
- error = onErrorError;
1150
- this.handleRedirectAndNotFound(
1151
- innerLoadContext,
1152
- this.getMatch(matchId),
1153
- onErrorError
1154
- );
1155
- }
1156
- const headResult = this.executeHead(innerLoadContext, matchId, route);
1157
- const head = headResult ? await headResult : void 0;
1158
- innerLoadContext.updateMatch(matchId, (prev) => ({
1159
- ...prev,
1160
- error,
1161
- status: "error",
1162
- isFetching: false,
1163
- ...head
1164
- }));
1165
- }
1166
- } catch (err) {
1167
- const match = this.getMatch(matchId);
1168
- if (match) {
1169
- const headResult = this.executeHead(innerLoadContext, matchId, route);
1170
- if (headResult) {
1171
- const head = await headResult;
1172
- innerLoadContext.updateMatch(matchId, (prev) => ({
1173
- ...prev,
1174
- ...head
1175
- }));
1176
- }
1177
- match._nonReactive.loaderPromise = void 0;
1178
- }
1179
- this.handleRedirectAndNotFound(innerLoadContext, match, err);
1180
- }
1181
- };
1182
- this.loadRouteMatch = async (innerLoadContext, index) => {
1183
- var _a, _b;
1184
- const { id: matchId, routeId } = innerLoadContext.matches[index];
1185
- let loaderShouldRunAsync = false;
1186
- let loaderIsRunningAsync = false;
1187
- const route = this.looseRoutesById[routeId];
1188
- const prevMatch = this.getMatch(matchId);
1189
- if (this.shouldSkipLoader(matchId)) {
1190
- if (this.isServer) {
1191
- const headResult = this.executeHead(innerLoadContext, matchId, route);
1192
- if (headResult) {
1193
- const head = await headResult;
1194
- innerLoadContext.updateMatch(matchId, (prev) => ({
1195
- ...prev,
1196
- ...head
1197
- }));
1198
- }
1199
- return this.getMatch(matchId);
1200
- }
1201
- } else if (prevMatch._nonReactive.loaderPromise) {
1202
- if (prevMatch.status === "success" && !innerLoadContext.sync && !prevMatch.preload) {
1203
- return this.getMatch(matchId);
1204
- }
1205
- await prevMatch._nonReactive.loaderPromise;
1206
- const match2 = this.getMatch(matchId);
1207
- if (match2.error) {
1208
- this.handleRedirectAndNotFound(innerLoadContext, match2, match2.error);
1209
- }
1210
- } else {
1211
- const age = Date.now() - this.getMatch(matchId).updatedAt;
1212
- const preload = this.resolvePreload(innerLoadContext, matchId);
1213
- const staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
1214
- const shouldReloadOption = route.options.shouldReload;
1215
- const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(
1216
- this.getLoaderContext(innerLoadContext, matchId, index, route)
1217
- ) : shouldReloadOption;
1218
- const nextPreload = !!preload && !this.state.matches.some((d) => d.id === matchId);
1219
- const match2 = this.getMatch(matchId);
1220
- match2._nonReactive.loaderPromise = createControlledPromise();
1221
- if (nextPreload !== match2.preload) {
1222
- innerLoadContext.updateMatch(matchId, (prev) => ({
1223
- ...prev,
1224
- preload: nextPreload
1225
- }));
1226
- }
1227
- const { status, invalid } = this.getMatch(matchId);
1228
- loaderShouldRunAsync = status === "success" && (invalid || (shouldReload ?? age > staleAge));
1229
- if (preload && route.options.preload === false) ;
1230
- else if (loaderShouldRunAsync && !innerLoadContext.sync) {
1231
- loaderIsRunningAsync = true;
1232
- (async () => {
1233
- var _a2, _b2;
1234
- try {
1235
- await this.runLoader(innerLoadContext, matchId, index, route);
1236
- const match3 = this.getMatch(matchId);
1237
- (_a2 = match3._nonReactive.loaderPromise) == null ? void 0 : _a2.resolve();
1238
- (_b2 = match3._nonReactive.loadPromise) == null ? void 0 : _b2.resolve();
1239
- match3._nonReactive.loaderPromise = void 0;
1240
- } catch (err) {
1241
- if (isRedirect(err)) {
1242
- await this.navigate(err.options);
1243
- }
1244
- }
1245
- })();
1246
- } else if (status !== "success" || loaderShouldRunAsync && innerLoadContext.sync) {
1247
- await this.runLoader(innerLoadContext, matchId, index, route);
1248
- } else {
1249
- const headResult = this.executeHead(innerLoadContext, matchId, route);
1250
- if (headResult) {
1251
- const head = await headResult;
1252
- innerLoadContext.updateMatch(matchId, (prev) => ({
1253
- ...prev,
1254
- ...head
1255
- }));
1256
- }
1257
- }
1258
- }
1259
- const match = this.getMatch(matchId);
1260
- if (!loaderIsRunningAsync) {
1261
- (_a = match._nonReactive.loaderPromise) == null ? void 0 : _a.resolve();
1262
- (_b = match._nonReactive.loadPromise) == null ? void 0 : _b.resolve();
1263
- }
1264
- clearTimeout(match._nonReactive.pendingTimeout);
1265
- match._nonReactive.pendingTimeout = void 0;
1266
- if (!loaderIsRunningAsync) match._nonReactive.loaderPromise = void 0;
1267
- match._nonReactive.dehydrated = void 0;
1268
- const nextIsFetching = loaderIsRunningAsync ? match.isFetching : false;
1269
- if (nextIsFetching !== match.isFetching || match.invalid !== false) {
1270
- innerLoadContext.updateMatch(matchId, (prev) => ({
1271
- ...prev,
1272
- isFetching: nextIsFetching,
1273
- invalid: false
1274
- }));
1275
- }
1276
- return this.getMatch(matchId);
1277
- };
1278
- this.loadMatches = async (baseContext) => {
1279
- const innerLoadContext = baseContext;
1280
- innerLoadContext.updateMatch ?? (innerLoadContext.updateMatch = this.updateMatch);
1281
- innerLoadContext.matchPromises = [];
1282
- if (!this.isServer && this.state.matches.some((d) => d._forcePending)) {
1283
- this.triggerOnReady(innerLoadContext);
1284
- }
1285
- try {
1286
- for (let i = 0; i < innerLoadContext.matches.length; i++) {
1287
- const beforeLoad = this.handleBeforeLoad(innerLoadContext, i);
1288
- if (isPromise(beforeLoad)) await beforeLoad;
1289
- }
1290
- const max = innerLoadContext.firstBadMatchIndex ?? innerLoadContext.matches.length;
1291
- for (let i = 0; i < max; i++) {
1292
- innerLoadContext.matchPromises.push(
1293
- this.loadRouteMatch(innerLoadContext, i)
1294
- );
1295
- }
1296
- await Promise.all(innerLoadContext.matchPromises);
1297
- const readyPromise = this.triggerOnReady(innerLoadContext);
1298
- if (isPromise(readyPromise)) await readyPromise;
1299
- } catch (err) {
1300
- if (isNotFound(err) && !innerLoadContext.preload) {
1301
- const readyPromise = this.triggerOnReady(innerLoadContext);
1302
- if (isPromise(readyPromise)) await readyPromise;
1303
- throw err;
1304
- }
1305
- if (isRedirect(err)) {
1306
- throw err;
1307
- }
1308
- }
1309
- return innerLoadContext.matches;
1310
- };
1311
709
  this.invalidate = (opts) => {
1312
710
  const invalidate = (d) => {
1313
711
  var _a;
@@ -1376,40 +774,7 @@ class RouterCore {
1376
774
  };
1377
775
  this.clearCache({ filter });
1378
776
  };
1379
- this.loadRouteChunk = (route) => {
1380
- if (!route._lazyLoaded && route._lazyPromise === void 0) {
1381
- if (route.lazyFn) {
1382
- route._lazyPromise = route.lazyFn().then((lazyRoute) => {
1383
- const { id: _id, ...options2 } = lazyRoute.options;
1384
- Object.assign(route.options, options2);
1385
- route._lazyLoaded = true;
1386
- route._lazyPromise = void 0;
1387
- });
1388
- } else {
1389
- route._lazyLoaded = true;
1390
- }
1391
- }
1392
- if (!route._componentsLoaded && route._componentsPromise === void 0) {
1393
- const loadComponents = () => {
1394
- var _a;
1395
- const preloads = [];
1396
- for (const type of componentTypes) {
1397
- const preload = (_a = route.options[type]) == null ? void 0 : _a.preload;
1398
- if (preload) preloads.push(preload());
1399
- }
1400
- if (preloads.length)
1401
- return Promise.all(preloads).then(() => {
1402
- route._componentsLoaded = true;
1403
- route._componentsPromise = void 0;
1404
- });
1405
- route._componentsLoaded = true;
1406
- route._componentsPromise = void 0;
1407
- return;
1408
- };
1409
- route._componentsPromise = route._lazyPromise ? route._lazyPromise.then(loadComponents) : loadComponents();
1410
- }
1411
- return route._componentsPromise;
1412
- };
777
+ this.loadRouteChunk = loadRouteChunk;
1413
778
  this.preloadRoute = async (opts) => {
1414
779
  const next = this.buildLocation(opts);
1415
780
  let matches = this.matchRoutes(next, {
@@ -1437,7 +802,8 @@ class RouterCore {
1437
802
  });
1438
803
  });
1439
804
  try {
1440
- matches = await this.loadMatches({
805
+ matches = await loadMatches({
806
+ router: this,
1441
807
  matches,
1442
808
  location: next,
1443
809
  preload: true,
@@ -1504,36 +870,6 @@ class RouterCore {
1504
870
  }
1505
871
  return match;
1506
872
  };
1507
- this._handleNotFound = (innerLoadContext, err) => {
1508
- var _a;
1509
- const routeCursor = this.routesById[err.routeId ?? ""] ?? this.routeTree;
1510
- const matchesByRouteId = {};
1511
- for (const match of innerLoadContext.matches) {
1512
- matchesByRouteId[match.routeId] = match;
1513
- }
1514
- if (!routeCursor.options.notFoundComponent && ((_a = this.options) == null ? void 0 : _a.defaultNotFoundComponent)) {
1515
- routeCursor.options.notFoundComponent = this.options.defaultNotFoundComponent;
1516
- }
1517
- invariant(
1518
- routeCursor.options.notFoundComponent,
1519
- "No notFoundComponent found. Please set a notFoundComponent on your route or provide a defaultNotFoundComponent to the router."
1520
- );
1521
- const matchForRoute = matchesByRouteId[routeCursor.id];
1522
- invariant(
1523
- matchForRoute,
1524
- "Could not find match for route: " + routeCursor.id
1525
- );
1526
- innerLoadContext.updateMatch(matchForRoute.id, (prev) => ({
1527
- ...prev,
1528
- status: "notFound",
1529
- error: err,
1530
- isFetching: false
1531
- }));
1532
- if (err.routerCode === "BEFORE_LOAD" && routeCursor.parentRoute) {
1533
- err.routeId = routeCursor.parentRoute.id;
1534
- this._handleNotFound(innerLoadContext, err);
1535
- }
1536
- };
1537
873
  this.hasNotFoundMatch = () => {
1538
874
  return this.__store.state.matches.some(
1539
875
  (d) => d.status === "notFound" || d.globalNotFound
@@ -1770,12 +1106,6 @@ class SearchParamError extends Error {
1770
1106
  }
1771
1107
  class PathParamError extends Error {
1772
1108
  }
1773
- function makeMaybe(value, error) {
1774
- if (error) {
1775
- return { status: "error", error };
1776
- }
1777
- return { status: "success", value };
1778
- }
1779
1109
  const normalize = (str) => str.endsWith("/") && str.length > 1 ? str.slice(0, -1) : str;
1780
1110
  function comparePaths(a, b) {
1781
1111
  return normalize(a) === normalize(b);
@@ -1820,21 +1150,6 @@ function validateSearch(validateSearch2, input) {
1820
1150
  }
1821
1151
  return {};
1822
1152
  }
1823
- const componentTypes = [
1824
- "component",
1825
- "errorComponent",
1826
- "pendingComponent",
1827
- "notFoundComponent"
1828
- ];
1829
- function routeNeedsPreload(route) {
1830
- var _a;
1831
- for (const componentType of componentTypes) {
1832
- if ((_a = route.options[componentType]) == null ? void 0 : _a.preload) {
1833
- return true;
1834
- }
1835
- }
1836
- return false;
1837
- }
1838
1153
  const REQUIRED_PARAM_BASE_SCORE = 0.5;
1839
1154
  const OPTIONAL_PARAM_BASE_SCORE = 0.4;
1840
1155
  const WILDCARD_PARAM_BASE_SCORE = 0.25;
@@ -2107,7 +1422,6 @@ export {
2107
1422
  PathParamError,
2108
1423
  RouterCore,
2109
1424
  SearchParamError,
2110
- componentTypes,
2111
1425
  defaultSerializeError,
2112
1426
  getInitialRouterState,
2113
1427
  getLocationChangeInfo,