@tanstack/router-core 0.0.1-beta.159 → 0.0.1-beta.160
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.
- package/build/cjs/route.js +0 -2
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js +65 -48
- package/build/cjs/router.js.map +1 -1
- package/build/esm/index.js +65 -50
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +118 -118
- package/build/types/index.d.ts +13 -14
- package/build/umd/index.development.js +65 -50
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/route.ts +18 -38
- package/src/router.ts +87 -71
package/build/esm/index.js
CHANGED
|
@@ -549,8 +549,6 @@ function decode(str) {
|
|
|
549
549
|
|
|
550
550
|
const rootRouteId = '__root__';
|
|
551
551
|
|
|
552
|
-
// | ParseParamsObj<TPath, TParams>
|
|
553
|
-
|
|
554
552
|
// The parse type here allows a zod schema to be passed directly to the validator
|
|
555
553
|
|
|
556
554
|
class Route {
|
|
@@ -717,25 +715,27 @@ class Router {
|
|
|
717
715
|
this.__store = new Store(getInitialRouterState(), {
|
|
718
716
|
onUpdate: () => {
|
|
719
717
|
const prev = this.state;
|
|
720
|
-
|
|
721
|
-
|
|
718
|
+
const next = this.__store.state;
|
|
719
|
+
console.log(Object.values(next.matchesById).find(d => d.status === 'error'));
|
|
720
|
+
const matchesByIdChanged = prev.matchesById !== next.matchesById;
|
|
722
721
|
let matchesChanged;
|
|
723
722
|
let pendingMatchesChanged;
|
|
724
723
|
if (!matchesByIdChanged) {
|
|
725
|
-
matchesChanged = prev.matchIds.length !==
|
|
726
|
-
pendingMatchesChanged = prev.pendingMatchIds.length !==
|
|
724
|
+
matchesChanged = prev.matchIds.length !== next.matchIds.length || prev.matchIds.some((d, i) => d !== next.matchIds[i]);
|
|
725
|
+
pendingMatchesChanged = prev.pendingMatchIds.length !== next.pendingMatchIds.length || prev.pendingMatchIds.some((d, i) => d !== next.pendingMatchIds[i]);
|
|
727
726
|
}
|
|
728
727
|
if (matchesByIdChanged || matchesChanged) {
|
|
729
|
-
|
|
730
|
-
return
|
|
728
|
+
next.matches = next.matchIds.map(id => {
|
|
729
|
+
return next.matchesById[id];
|
|
731
730
|
});
|
|
732
731
|
}
|
|
733
732
|
if (matchesByIdChanged || pendingMatchesChanged) {
|
|
734
|
-
|
|
735
|
-
return
|
|
733
|
+
next.pendingMatches = next.pendingMatchIds.map(id => {
|
|
734
|
+
return next.matchesById[id];
|
|
736
735
|
});
|
|
737
736
|
}
|
|
738
|
-
|
|
737
|
+
next.isFetching = [...next.matches, ...next.pendingMatches].some(d => d.isFetching);
|
|
738
|
+
this.state = next;
|
|
739
739
|
},
|
|
740
740
|
defaultPriority: 'low'
|
|
741
741
|
});
|
|
@@ -816,11 +816,12 @@ class Router {
|
|
|
816
816
|
cancelMatch = id => {
|
|
817
817
|
this.getRouteMatch(id)?.abortController?.abort();
|
|
818
818
|
};
|
|
819
|
-
safeLoad = opts => {
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
819
|
+
safeLoad = async opts => {
|
|
820
|
+
try {
|
|
821
|
+
return this.load(opts);
|
|
822
|
+
} catch (err) {
|
|
823
|
+
// Don't do anything
|
|
824
|
+
}
|
|
824
825
|
};
|
|
825
826
|
latestLoadPromise = Promise.resolve();
|
|
826
827
|
load = async opts => {
|
|
@@ -857,11 +858,16 @@ class Router {
|
|
|
857
858
|
});
|
|
858
859
|
try {
|
|
859
860
|
// Load the matches
|
|
860
|
-
|
|
861
|
+
try {
|
|
862
|
+
await this.loadMatches(pendingMatches);
|
|
863
|
+
} catch (err) {
|
|
864
|
+
// swallow this error, since we'll display the
|
|
865
|
+
// errors on the route components
|
|
866
|
+
}
|
|
861
867
|
|
|
862
868
|
// Only apply the latest transition
|
|
863
869
|
if (latestPromise = checkLatest()) {
|
|
864
|
-
return
|
|
870
|
+
return latestPromise;
|
|
865
871
|
}
|
|
866
872
|
const prevLocation = this.state.resolvedLocation;
|
|
867
873
|
this.__store.setState(s => ({
|
|
@@ -878,7 +884,7 @@ class Router {
|
|
|
878
884
|
} catch (err) {
|
|
879
885
|
// Only apply the latest transition
|
|
880
886
|
if (latestPromise = checkLatest()) {
|
|
881
|
-
return
|
|
887
|
+
return latestPromise;
|
|
882
888
|
}
|
|
883
889
|
reject(err);
|
|
884
890
|
}
|
|
@@ -1137,6 +1143,7 @@ class Router {
|
|
|
1137
1143
|
throw errorHandlerErr;
|
|
1138
1144
|
}
|
|
1139
1145
|
}
|
|
1146
|
+
console.log('set error');
|
|
1140
1147
|
this.setRouteMatch(match.id, s => ({
|
|
1141
1148
|
...s,
|
|
1142
1149
|
error: err,
|
|
@@ -1189,19 +1196,8 @@ class Router {
|
|
|
1189
1196
|
const latest = this.getRouteMatch(match.id);
|
|
1190
1197
|
return latest && latest.fetchedAt !== fetchedAt ? latest.loadPromise : undefined;
|
|
1191
1198
|
};
|
|
1192
|
-
const
|
|
1199
|
+
const load = async () => {
|
|
1193
1200
|
let latestPromise;
|
|
1194
|
-
const componentsPromise = Promise.all(componentTypes.map(async type => {
|
|
1195
|
-
const component = route.options[type];
|
|
1196
|
-
if (component?.preload) {
|
|
1197
|
-
await component.preload();
|
|
1198
|
-
}
|
|
1199
|
-
}));
|
|
1200
|
-
const loaderPromise = route.options.loader?.({
|
|
1201
|
-
...match,
|
|
1202
|
-
preload: !!opts?.preload,
|
|
1203
|
-
parentMatchPromise
|
|
1204
|
-
});
|
|
1205
1201
|
const handleError = err => {
|
|
1206
1202
|
if (isRedirect(err)) {
|
|
1207
1203
|
if (!opts?.preload) {
|
|
@@ -1212,33 +1208,46 @@ class Router {
|
|
|
1212
1208
|
return false;
|
|
1213
1209
|
};
|
|
1214
1210
|
try {
|
|
1211
|
+
const componentsPromise = Promise.all(componentTypes.map(async type => {
|
|
1212
|
+
const component = route.options[type];
|
|
1213
|
+
if (component?.preload) {
|
|
1214
|
+
await component.preload();
|
|
1215
|
+
}
|
|
1216
|
+
}));
|
|
1217
|
+
const loaderPromise = route.options.loader?.({
|
|
1218
|
+
...match,
|
|
1219
|
+
preload: !!opts?.preload,
|
|
1220
|
+
parentMatchPromise
|
|
1221
|
+
});
|
|
1215
1222
|
const [_, loader] = await Promise.all([componentsPromise, loaderPromise]);
|
|
1216
1223
|
if (latestPromise = checkLatest()) return await latestPromise;
|
|
1217
1224
|
this.setRouteMatchData(match.id, () => loader, opts);
|
|
1218
|
-
} catch (
|
|
1225
|
+
} catch (loaderError) {
|
|
1219
1226
|
if (latestPromise = checkLatest()) return await latestPromise;
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
}
|
|
1223
|
-
const errorHandler = route.options.onLoadError ?? route.options.onError;
|
|
1224
|
-
let caughtError = err;
|
|
1227
|
+
handleError(loaderError);
|
|
1228
|
+
let error = loaderError;
|
|
1225
1229
|
try {
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
return;
|
|
1230
|
+
if (route.options.onLoadError) {
|
|
1231
|
+
route.options.onLoadError?.(loaderError);
|
|
1232
|
+
} else {
|
|
1233
|
+
route.options.onError?.(loaderError);
|
|
1231
1234
|
}
|
|
1235
|
+
} catch (errorHandlerErr) {
|
|
1236
|
+
error = errorHandlerErr;
|
|
1237
|
+
handleError(error);
|
|
1232
1238
|
}
|
|
1239
|
+
console.log('set error');
|
|
1233
1240
|
this.setRouteMatch(match.id, s => ({
|
|
1234
1241
|
...s,
|
|
1235
|
-
error:
|
|
1242
|
+
error: error,
|
|
1236
1243
|
status: 'error',
|
|
1237
1244
|
isFetching: false,
|
|
1238
1245
|
updatedAt: Date.now()
|
|
1239
1246
|
}));
|
|
1247
|
+
console.log(this.getRouteMatch(match.id)?.status);
|
|
1240
1248
|
}
|
|
1241
|
-
}
|
|
1249
|
+
};
|
|
1250
|
+
const loadPromise = load();
|
|
1242
1251
|
this.setRouteMatch(match.id, s => ({
|
|
1243
1252
|
...s,
|
|
1244
1253
|
status: s.status !== 'success' ? 'pending' : s.status,
|
|
@@ -1662,13 +1671,18 @@ class Router {
|
|
|
1662
1671
|
return this.state.matchesById[id];
|
|
1663
1672
|
};
|
|
1664
1673
|
setRouteMatch = (id, updater) => {
|
|
1665
|
-
this.__store.setState(prev =>
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
...prev.matchesById,
|
|
1669
|
-
[id]: updater(prev.matchesById[id])
|
|
1674
|
+
this.__store.setState(prev => {
|
|
1675
|
+
if (!prev.matchesById[id]) {
|
|
1676
|
+
console.warn(`No match found with id: ${id}`);
|
|
1670
1677
|
}
|
|
1671
|
-
|
|
1678
|
+
return {
|
|
1679
|
+
...prev,
|
|
1680
|
+
matchesById: {
|
|
1681
|
+
...prev.matchesById,
|
|
1682
|
+
[id]: updater(prev.matchesById[id])
|
|
1683
|
+
}
|
|
1684
|
+
};
|
|
1685
|
+
});
|
|
1672
1686
|
};
|
|
1673
1687
|
setRouteMatchData = (id, updater, opts) => {
|
|
1674
1688
|
const match = this.getRouteMatch(id);
|
|
@@ -1677,6 +1691,7 @@ class Router {
|
|
|
1677
1691
|
const updatedAt = opts?.updatedAt ?? Date.now();
|
|
1678
1692
|
const preloadInvalidAt = updatedAt + (opts?.maxAge ?? route.options.preloadMaxAge ?? this.options.defaultPreloadMaxAge ?? 5000);
|
|
1679
1693
|
const invalidAt = updatedAt + (opts?.maxAge ?? route.options.maxAge ?? this.options.defaultMaxAge ?? Infinity);
|
|
1694
|
+
console.log('set success');
|
|
1680
1695
|
this.setRouteMatch(id, s => ({
|
|
1681
1696
|
...s,
|
|
1682
1697
|
error: undefined,
|