@tanstack/router-core 0.0.1-beta.160 → 0.0.1-beta.161

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.
@@ -716,7 +716,6 @@ class Router {
716
716
  onUpdate: () => {
717
717
  const prev = this.state;
718
718
  const next = this.__store.state;
719
- console.log(Object.values(next.matchesById).find(d => d.status === 'error'));
720
719
  const matchesByIdChanged = prev.matchesById !== next.matchesById;
721
720
  let matchesChanged;
722
721
  let pendingMatchesChanged;
@@ -1143,7 +1142,6 @@ class Router {
1143
1142
  throw errorHandlerErr;
1144
1143
  }
1145
1144
  }
1146
- console.log('set error');
1147
1145
  this.setRouteMatch(match.id, s => ({
1148
1146
  ...s,
1149
1147
  error: err,
@@ -1196,17 +1194,17 @@ class Router {
1196
1194
  const latest = this.getRouteMatch(match.id);
1197
1195
  return latest && latest.fetchedAt !== fetchedAt ? latest.loadPromise : undefined;
1198
1196
  };
1197
+ const handleIfRedirect = err => {
1198
+ if (isRedirect(err)) {
1199
+ if (!opts?.preload) {
1200
+ this.navigate(err);
1201
+ }
1202
+ return true;
1203
+ }
1204
+ return false;
1205
+ };
1199
1206
  const load = async () => {
1200
1207
  let latestPromise;
1201
- const handleError = err => {
1202
- if (isRedirect(err)) {
1203
- if (!opts?.preload) {
1204
- this.navigate(err);
1205
- }
1206
- return true;
1207
- }
1208
- return false;
1209
- };
1210
1208
  try {
1211
1209
  const componentsPromise = Promise.all(componentTypes.map(async type => {
1212
1210
  const component = route.options[type];
@@ -1223,39 +1221,48 @@ class Router {
1223
1221
  if (latestPromise = checkLatest()) return await latestPromise;
1224
1222
  this.setRouteMatchData(match.id, () => loader, opts);
1225
1223
  } catch (loaderError) {
1224
+ let latestError = loaderError;
1226
1225
  if (latestPromise = checkLatest()) return await latestPromise;
1227
- handleError(loaderError);
1228
- let error = loaderError;
1229
- try {
1230
- if (route.options.onLoadError) {
1231
- route.options.onLoadError?.(loaderError);
1232
- } else {
1233
- route.options.onError?.(loaderError);
1226
+ if (handleIfRedirect(loaderError)) return;
1227
+ if (route.options.onLoadError) {
1228
+ try {
1229
+ route.options.onLoadError(loaderError);
1230
+ } catch (onLoadError) {
1231
+ latestError = onLoadError;
1232
+ if (handleIfRedirect(onLoadError)) return;
1233
+ }
1234
+ }
1235
+ if ((!route.options.onLoadError || latestError !== loaderError) && route.options.onError) {
1236
+ try {
1237
+ route.options.onError(latestError);
1238
+ } catch (onErrorError) {
1239
+ if (handleIfRedirect(onErrorError)) return;
1234
1240
  }
1235
- } catch (errorHandlerErr) {
1236
- error = errorHandlerErr;
1237
- handleError(error);
1238
1241
  }
1239
- console.log('set error');
1240
1242
  this.setRouteMatch(match.id, s => ({
1241
1243
  ...s,
1242
- error: error,
1244
+ error: loaderError,
1243
1245
  status: 'error',
1244
1246
  isFetching: false,
1245
1247
  updatedAt: Date.now()
1246
1248
  }));
1247
- console.log(this.getRouteMatch(match.id)?.status);
1248
1249
  }
1249
1250
  };
1250
- const loadPromise = load();
1251
- this.setRouteMatch(match.id, s => ({
1252
- ...s,
1253
- status: s.status !== 'success' ? 'pending' : s.status,
1254
- isFetching: true,
1255
- loadPromise,
1256
- fetchedAt,
1257
- invalid: false
1258
- }));
1251
+ let loadPromise;
1252
+ this.__store.batch(() => {
1253
+ this.setRouteMatch(match.id, s => ({
1254
+ ...s,
1255
+ // status: s.status !== 'success' ? 'pending' : s.status,
1256
+ isFetching: true,
1257
+ fetchedAt,
1258
+ invalid: false
1259
+ }));
1260
+ loadPromise = load();
1261
+ this.setRouteMatch(match.id, s => ({
1262
+ ...s,
1263
+ loadPromise
1264
+ }));
1265
+ });
1259
1266
  await loadPromise;
1260
1267
  })());
1261
1268
  });
@@ -1691,7 +1698,6 @@ class Router {
1691
1698
  const updatedAt = opts?.updatedAt ?? Date.now();
1692
1699
  const preloadInvalidAt = updatedAt + (opts?.maxAge ?? route.options.preloadMaxAge ?? this.options.defaultPreloadMaxAge ?? 5000);
1693
1700
  const invalidAt = updatedAt + (opts?.maxAge ?? route.options.maxAge ?? this.options.defaultMaxAge ?? Infinity);
1694
- console.log('set success');
1695
1701
  this.setRouteMatch(id, s => ({
1696
1702
  ...s,
1697
1703
  error: undefined,