Skip to content

Commit b32d779

Browse files
authored
Merge pull request #37 from Semantic-Org/bug/error-page-routes
Bug: Fix Error During Data Loading
2 parents 019c922 + 10c2a8e commit b32d779

2 files changed

Lines changed: 108 additions & 112 deletions

File tree

‎src/route.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ export class Route {
304304
// what a view sees: the page's params and query, and the data of this route and the routes above it
305305
viewContext(page) {
306306
const activeRoutes = (page.activeRoutes ?? []).filter((activeRoute) => inArray(activeRoute.route, this.ancestors));
307-
const data = {};
308-
each(activeRoutes, (activeRoute) => extend(data, activeRoute.data));
309-
return { ...this.router.contextFor(this, page, { data, activeRoutes }), page };
307+
return { ...this.router.contextFor(this, page, { activeRoutes }), page };
310308
}
311309

312310
timeout(fn, ms) {

‎src/router.js‎

Lines changed: 107 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -197,35 +197,19 @@ export class Router {
197197
async applyNotFound(url, event) {
198198
const navigation = { signal: event.signal, reload: false, previous: null };
199199
await this.resolveIdentity(navigation);
200-
const [view, views, { data, activeRoutes }] = await Promise.all([
200+
const [view, views, { activeRoutes, error }] = await Promise.all([
201201
this.resolveStatusView(this.root, 'notFound'),
202202
this.resolveRouteViews([this.root]),
203203
this.loadRoutes([this.root], { params: {}, query: {} }, navigation),
204204
]);
205+
if (error) {
206+
throw error;
207+
}
205208
if (event.signal.aborted) {
206209
return;
207210
}
208-
const titles = this.getTitles(activeRoutes);
209-
const page = {
210-
view,
211-
views,
212-
unresolvedRoutes: [],
213-
data,
214-
activeRoutes,
215-
status: 'notFound',
216-
url,
217-
title: titles[0] ?? '',
218-
titles,
219-
user: this.user,
220-
};
221-
await this.batch(() => {
222-
this.page = page;
223-
this.settleRoutes(null, page);
224-
if (globalThis.document && page.title) {
225-
globalThis.document.title = page.title;
226-
}
227-
return this.renderPage(page);
228-
});
211+
// no route matched, so the page has none of its own: it lands as every page does
212+
await this.applyCascade({ url, routes: [] }, { status: 'notFound', view, views, activeRoutes }, event);
229213
}
230214

231215
// a route was added: a page standing as not-found for want of a route is matched again a
@@ -739,6 +723,11 @@ export class Router {
739723
from: globalThis.navigation?.transition?.from ?? null,
740724
url: pending?.url ?? this.navigator.entry?.url ?? null,
741725
};
726+
// a route's failed load was handed to onError as it failed, and its error page stands
727+
if (this.page?.error === error) {
728+
this.notify();
729+
return;
730+
}
742731
this.onError(error, pending && this.contextFor(pending.route, pending));
743732
// a failure before commit left the standing page in place; only a committed one is shown as an error
744733
if (!pending || pending.committed) {
@@ -757,19 +746,11 @@ export class Router {
757746
}
758747
const activeRoutes = previous?.activeRoutes
759748
?? (this.root.data === undefined ? [this.createActiveRoute(this.root, {})] : []);
760-
await this.batch(() => {
761-
this.page = {
762-
...(pending ? this.pageFor(pending) : (previous ?? {})),
763-
activeRoutes,
764-
unresolvedRoutes: this.getUnresolvedRoutes(activeRoutes, pending?.routes ?? []),
765-
views,
766-
view,
767-
status: 'error',
768-
error,
769-
};
770-
this.settleRoutes(previous, this.page);
771-
return this.renderPage(this.page);
772-
}).catch(noop);
749+
// landed as every page is, at the page the navigation was going to, or the one standing
750+
const matched = pending ?? { ...previous, routes: [] };
751+
const resolved = this.buildCascadeResult(matched, { status: 'error', error, view, views, activeRoutes });
752+
await this.applyCascade(matched, resolved).catch(() => this.notify());
753+
return;
773754
}
774755
this.notify();
775756
}
@@ -945,6 +926,11 @@ export class Router {
945926
}
946927

947928
await this.applyCascade(matched, resolved, event);
929+
// the error page stands, and the navigation still failed, which the platform and whoever
930+
// awaits it are told
931+
if (resolved?.status === 'error') {
932+
throw resolved.error;
933+
}
948934
}
949935
finally {
950936
// a cold boot's loading view, shown at the mount, leaves with a navigation that was aborted
@@ -1005,18 +991,18 @@ export class Router {
1005991
return (await this.runGuards(matched, navigation)) ?? this.runData(matched, navigation);
1006992
}
1007993

1008-
// what a hook sees. data is what the routes above settled, merged, and activeRoutes the same route by
1009-
// route, none before the data phase; the signal is the navigation's while one is in flight and
1010-
// the route's own otherwise
1011-
contextFor(route, matched, { data = {}, activeRoutes = [], signal = route.abortSignal } = {}) {
994+
// what a hook sees. activeRoutes is what the routes above settled, route by route, none before
995+
// the data phase, and data is theirs merged; the signal is the navigation's while one is in
996+
// flight and the route's own otherwise
997+
contextFor(route, matched, { activeRoutes = [], signal = route.abortSignal } = {}) {
1012998
return {
1013999
params: matched.params,
10141000
query: matched.query,
10151001
signal,
10161002
user: this.user,
10171003
route,
10181004
router: this,
1019-
data,
1005+
data: this.getData(activeRoutes),
10201006
activeRoutes,
10211007
};
10221008
}
@@ -1070,23 +1056,19 @@ export class Router {
10701056
const viewsReady = this.resolveRouteViews(routes);
10711057
viewsReady.catch(noop);
10721058
const routesAbove = routes.slice(0, routes.indexOf(ancestor));
1073-
const { data, activeRoutes, notFound } = await this.loadRoutes(routesAbove, matched, navigation);
1074-
if (notFound) {
1075-
return this.notFoundResult(matched, viewsReady, { data, activeRoutes });
1059+
const loaded = await this.loadRoutes(routesAbove, matched, navigation);
1060+
if (loaded.status) {
1061+
return this.statusResult(matched, viewsReady, loaded);
10761062
}
10771063
const [view, views] = await Promise.all([
10781064
this.resolveStatusView(ancestor, 'forbidden'),
10791065
viewsReady,
10801066
]);
1081-
const titles = this.getTitles(activeRoutes);
10821067
return this.buildCascadeResult(matched, {
10831068
status: 'forbidden',
10841069
view,
10851070
views,
1086-
data,
1087-
activeRoutes,
1088-
title: titles[0] ?? '',
1089-
titles,
1071+
activeRoutes: loaded.activeRoutes,
10901072
});
10911073
}
10921074
}
@@ -1117,89 +1099,87 @@ export class Router {
11171099
const viewsReady = this.resolveRouteViews(routes);
11181100
viewsReady.catch(noop);
11191101

1120-
const { data, activeRoutes, notFound } = await this.loadRoutes(routes, matched, navigation);
1121-
if (notFound) {
1122-
return this.notFoundResult(matched, viewsReady, { data, activeRoutes });
1102+
const loaded = await this.loadRoutes(routes, matched, navigation);
1103+
if (loaded.status) {
1104+
return this.statusResult(matched, viewsReady, loaded);
11231105
}
11241106
const views = await viewsReady;
1125-
const titles = this.getTitles(activeRoutes);
11261107
return this.buildCascadeResult(matched, {
11271108
status: 'ok',
11281109
view: views.at(-1)?.view ?? null,
11291110
views,
1130-
data,
1131-
activeRoutes,
1132-
title: titles[0] ?? '',
1133-
titles,
1111+
activeRoutes: loaded.activeRoutes,
11341112
});
11351113
}
11361114

11371115
// the page's active routes in route order, each seeing the data of the routes above it: a route keeps
11381116
// the entry it had when the url as it sees it did not move and runs its data otherwise, and one
1139-
// returning not-found ends the walk. the data path walks every route; a refusal walks the routes
1140-
// above the refusing one, so their layouts stand with what they read
1117+
// returning not-found, or throwing, ends the walk with that status. the data path walks every
1118+
// route; a refusal walks the routes above the refusing one, so their layouts stand with what they read
11411119
async loadRoutes(routes, matched, navigation) {
11421120
const { signal } = navigation;
1143-
const data = {};
11441121
const activeRoutes = [];
11451122
let flaggedNotFound = false;
11461123
const notFound = () => {
11471124
flaggedNotFound = true;
11481125
return NOT_FOUND;
11491126
};
11501127

1151-
for (const route of routes) {
1152-
const kept = this.keptActiveRoute(route, matched, navigation);
1153-
if (kept) {
1154-
activeRoutes.push(kept);
1155-
extend(data, kept.data);
1156-
continue;
1157-
}
1128+
try {
1129+
for (const route of routes) {
1130+
const kept = this.keptActiveRoute(route, matched, navigation);
1131+
if (kept) {
1132+
activeRoutes.push(kept);
1133+
continue;
1134+
}
11581135

1159-
let routeData = {};
1160-
if (route.data !== undefined) {
1161-
const rawData = await this.evaluateData(route, route.data, {
1162-
...this.contextFor(route, matched, { data: { ...data }, activeRoutes: [...activeRoutes], signal }),
1163-
notFound,
1164-
});
1165-
if (flaggedNotFound || rawData === NOT_FOUND) {
1166-
return { data, activeRoutes, notFound: true };
1136+
let routeData = {};
1137+
if (route.data !== undefined) {
1138+
const rawData = await this.evaluateData(route, route.data, {
1139+
...this.contextFor(route, matched, { activeRoutes: [...activeRoutes], signal }),
1140+
notFound,
1141+
});
1142+
if (flaggedNotFound || rawData === NOT_FOUND) {
1143+
return { activeRoutes, status: 'notFound' };
1144+
}
1145+
if (isPlainObject(rawData)) {
1146+
routeData = await this.resolveData(rawData);
1147+
}
11671148
}
1168-
if (isPlainObject(rawData)) {
1169-
routeData = await this.resolveData(rawData);
1149+
// the entry exists before the title and the routes below run, so they can read it
1150+
const activeRoute = this.createActiveRoute(route, routeData);
1151+
if (route.title !== undefined) {
1152+
activeRoute.title = await this.resolveTitle(route, matched, {
1153+
activeRoutes: [...activeRoutes, activeRoute],
1154+
titles: this.getTitles(activeRoutes),
1155+
signal,
1156+
});
11701157
}
1158+
activeRoutes.push(activeRoute);
11711159
}
1172-
// the entry exists before the title and the routes below run, so they can read it
1173-
const activeRoute = this.createActiveRoute(route, routeData);
1174-
extend(data, activeRoute.data);
1175-
if (route.title !== undefined) {
1176-
activeRoute.title = await this.resolveTitle(route, matched, {
1177-
data: { ...data },
1178-
activeRoutes: [...activeRoutes, activeRoute],
1179-
titles: this.getTitles(activeRoutes),
1180-
signal,
1181-
});
1160+
}
1161+
catch (error) {
1162+
// a navigation that was superseded or cancelled is not an error to show
1163+
if (signal.aborted || error?.name === 'AbortError') {
1164+
throw error;
11821165
}
1183-
activeRoutes.push(activeRoute);
1166+
return { activeRoutes, status: 'error', error };
11841167
}
11851168

1186-
return { data, activeRoutes, notFound: false };
1169+
return { activeRoutes };
11871170
}
11881171

1189-
async notFoundResult(matched, viewsReady, { data, activeRoutes }) {
1190-
// routes load in order, so the one that returned not-found is the first that is not active
1172+
// a route's load ended with no page for it, in not-found or in an error: the routes above it
1173+
// are the page, and the status view stands in for it and the routes below
1174+
async statusResult(matched, viewsReady, { activeRoutes, status, error }) {
1175+
// routes load in order, so the one the load ended at is the first that is not active
11911176
const route = matched.routes[activeRoutes.length];
1192-
const [views, view] = await Promise.all([viewsReady, this.resolveStatusView(route, 'notFound')]);
1193-
const titles = this.getTitles(activeRoutes);
1194-
return this.buildCascadeResult(matched, {
1195-
status: 'notFound',
1196-
view,
1197-
views,
1198-
data,
1199-
activeRoutes,
1200-
title: titles[0] ?? '',
1201-
titles,
1202-
});
1177+
if (error) {
1178+
// before anything is awaited, so a navigation superseded while its page loads has still said so
1179+
this.onError(error, this.contextFor(route, matched, { activeRoutes }));
1180+
}
1181+
const [views, view] = await Promise.all([viewsReady, this.resolveStatusView(route, status)]);
1182+
return this.buildCascadeResult(matched, { status, ...(error && { error }), view, views, activeRoutes });
12031183
}
12041184

12051185
// a route's share of the page. the reactive scaffold gives it a signal the routes below read
@@ -1213,18 +1193,25 @@ export class Router {
12131193
|| Object.keys(route.querySpecs).some((key) => !isEqual(previous.query?.[key], matched.query[key]));
12141194
}
12151195

1196+
// the data of the routes given, merged root first: what a route's context holds, and a page
1197+
getData(activeRoutes) {
1198+
const data = {};
1199+
each(activeRoutes, (activeRoute) => extend(data, activeRoute.data));
1200+
return data;
1201+
}
1202+
12161203
// the titles a page carries, nearest first: the leaf's, its parent's, up to the root's
12171204
getTitles(activeRoutes) {
12181205
return activeRoutes.map((activeRoute) => activeRoute.title).filter((title) => title !== null).reverse();
12191206
}
12201207

12211208
// a title function is handed the titles above it, nearest first, so the common full title is a
12221209
// spread: ({ data, titles }) => [data.member.name, ...titles].join(' | ')
1223-
async resolveTitle(route, matched, { data, activeRoutes, titles, signal }) {
1210+
async resolveTitle(route, matched, { activeRoutes, titles, signal }) {
12241211
const value = await this.evaluateTitle(
12251212
route,
12261213
route.title,
1227-
{ ...this.contextFor(route, matched, { data, activeRoutes, signal }), titles },
1214+
{ ...this.contextFor(route, matched, { activeRoutes, signal }), titles },
12281215
);
12291216
return this.titleText(value);
12301217
}
@@ -1319,23 +1306,29 @@ export class Router {
13191306
}
13201307
const { key } = this.getEntrySnapshot();
13211308
const previous = this.page;
1309+
const { activeRoutes } = resolved;
1310+
const titles = this.getTitles(activeRoutes);
13221311
// the page's write, its settle and its render are one moment to anything watching
13231312
const rendering = this.batch(() => {
13241313
// what stood at the mount with no route active leaves before the page's routes are created
13251314
this.clearMountView();
1315+
// a result holds what was loaded, and the page reads the rest from its active routes
13261316
this.page = {
13271317
...this.pageFor(matched),
13281318
...resolved,
1329-
unresolvedRoutes: this.getUnresolvedRoutes(resolved.activeRoutes, matched.routes),
1319+
unresolvedRoutes: this.getUnresolvedRoutes(activeRoutes, matched.routes),
1320+
data: this.getData(activeRoutes),
1321+
titles,
1322+
title: titles[0] ?? '',
13301323
key,
13311324
user: this.user,
13321325
};
13331326
this.settleRoutes(previous, this.page);
13341327
if (key && resolved.status === 'ok') {
13351328
this.entryCache?.set(key, { resolved, user: this.user });
13361329
}
1337-
if (globalThis.document && resolved.title) {
1338-
globalThis.document.title = resolved.title;
1330+
if (globalThis.document && this.page.title) {
1331+
globalThis.document.title = this.page.title;
13391332
}
13401333
return this.renderPage(this.page, event);
13411334
});
@@ -1572,11 +1565,16 @@ export class Router {
15721565
return;
15731566
}
15741567
const activeRoutes = changes ? previous.activeRoutes.slice(0, index) : previous.activeRoutes;
1575-
const data = {};
1576-
each(activeRoutes, (activeRoute) => extend(data, activeRoute.data));
15771568
const titles = this.getTitles(activeRoutes);
15781569
const rendering = this.batch(() => {
1579-
this.page = { ...previous, ...changes, activeRoutes, data, title: titles[0] ?? '', titles };
1570+
this.page = {
1571+
...previous,
1572+
...changes,
1573+
activeRoutes,
1574+
data: this.getData(activeRoutes),
1575+
titles,
1576+
title: titles[0] ?? '',
1577+
};
15801578
if (globalThis.document && this.page.title) {
15811579
globalThis.document.title = this.page.title;
15821580
}

0 commit comments

Comments
 (0)