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