@opentf/afw 0.35.0 → 0.36.0
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/package.json +1 -1
- package/runtime/route-data.d.ts +9 -0
- package/runtime/route-data.js +26 -3
- package/runtime/router.js +14 -2
- package/server/loader.js +7 -0
package/package.json
CHANGED
package/runtime/route-data.d.ts
CHANGED
|
@@ -13,3 +13,12 @@ export function readInlineRouteData(): any;
|
|
|
13
13
|
export function __resetInlineRouteData(): void;
|
|
14
14
|
/** The reserved per-route data filename/URL suffix (`/todos` → `/todos/__data.json`). */
|
|
15
15
|
export const DATA_FILE: "__data.json";
|
|
16
|
+
/** The header the data endpoint sets when a loader redirected. */
|
|
17
|
+
export const REDIRECT_HEADER: "x-afw-redirect";
|
|
18
|
+
/** Returned by `fetchRouteData` in place of data when the route redirects. */
|
|
19
|
+
export class RouteRedirect {
|
|
20
|
+
constructor(location: string, options?: { reload?: boolean });
|
|
21
|
+
redirect: string;
|
|
22
|
+
/** A full page load is needed (the server redirected the data request itself). */
|
|
23
|
+
reload: boolean;
|
|
24
|
+
}
|
package/runtime/route-data.js
CHANGED
|
@@ -25,13 +25,36 @@ export function dataUrlFor(pathname, search = "") {
|
|
|
25
25
|
return (path === "/" ? `/${DATA_FILE}` : `${path}/${DATA_FILE}`) + (search || "");
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/** The header the data endpoint sets when a loader redirected (`throw redirect`). */
|
|
29
|
+
export const REDIRECT_HEADER = "x-afw-redirect";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* What `fetchRouteData` returns instead of data when the route redirects:
|
|
33
|
+
* `{ redirect: "/login", reload: false }`. `reload` asks for a full page load, for a
|
|
34
|
+
* redirect the browser hid (middleware redirecting the data request itself).
|
|
35
|
+
*/
|
|
36
|
+
export class RouteRedirect {
|
|
37
|
+
constructor(location, { reload = false } = {}) {
|
|
38
|
+
this.redirect = location;
|
|
39
|
+
this.reload = reload;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
28
43
|
/**
|
|
29
44
|
* Fetch a route's loader data. 200 → the parsed JSON; 404 → `undefined` (no
|
|
30
|
-
* loader / `notFound()`);
|
|
31
|
-
* commits the navigation with no data).
|
|
45
|
+
* loader / `notFound()`); a redirect → a {@link RouteRedirect}; anything else throws
|
|
46
|
+
* (the router reports it and commits the navigation with no data).
|
|
32
47
|
*/
|
|
33
48
|
export async function fetchRouteData(pathname, search = "") {
|
|
34
|
-
|
|
49
|
+
// "manual": a redirect is not data. Following it would fetch an HTML page.
|
|
50
|
+
const res = await fetch(dataUrlFor(pathname, search), { redirect: "manual" });
|
|
51
|
+
const location = res.headers.get(REDIRECT_HEADER);
|
|
52
|
+
if (location) return new RouteRedirect(location); // the loader redirected
|
|
53
|
+
// Middleware redirected the data request (an auth guard): the target is hidden
|
|
54
|
+
// from fetch, so load the page itself and let the server redirect it.
|
|
55
|
+
if (res.type === "opaqueredirect" || (res.status >= 300 && res.status < 400)) {
|
|
56
|
+
return new RouteRedirect(pathname + search, { reload: true });
|
|
57
|
+
}
|
|
35
58
|
if (res.status === 404) return undefined;
|
|
36
59
|
if (!res.ok) throw new Error(`route data request for ${pathname} failed (${res.status})`);
|
|
37
60
|
return res.json();
|
package/runtime/router.js
CHANGED
|
@@ -22,7 +22,7 @@ import { prepareRouteHead, updateRouteHead } from "./head.js";
|
|
|
22
22
|
import { beginHydration, cursor, endHydration, renderedForPath } from "./hydrate.js";
|
|
23
23
|
import { collectHotState, withHotCarry } from "./hot-carry.js";
|
|
24
24
|
import { buildScopedView, runCleanup, runMount } from "./mount.js";
|
|
25
|
-
import { fetchRouteData, readInlineRouteData } from "./route-data.js";
|
|
25
|
+
import { RouteRedirect, fetchRouteData, readInlineRouteData } from "./route-data.js";
|
|
26
26
|
import { setRouteDataRefresher } from "./invalidate.js";
|
|
27
27
|
import { compareRoutes, compileRoute, decodeParams } from "./route-match.js";
|
|
28
28
|
|
|
@@ -471,6 +471,15 @@ export function prefetch(path) {
|
|
|
471
471
|
* Reload the current page's loader data into `router.data` (`invalidate()`), and drop
|
|
472
472
|
* prefetched data, which may be stale too.
|
|
473
473
|
*/
|
|
474
|
+
/** Go where a loader (or the server) redirected a navigation. */
|
|
475
|
+
function followRedirect(target) {
|
|
476
|
+
if (target.reload) {
|
|
477
|
+
window.location.assign(target.redirect);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
return navigate(target.redirect, true);
|
|
481
|
+
}
|
|
482
|
+
|
|
474
483
|
export async function refreshRouteData() {
|
|
475
484
|
prefetched.clear();
|
|
476
485
|
if (!isBrowser || !rootEl) return;
|
|
@@ -480,7 +489,9 @@ export async function refreshRouteData() {
|
|
|
480
489
|
const seq = navSeq;
|
|
481
490
|
try {
|
|
482
491
|
const data = await fetchRouteData(pathname, window.location.search);
|
|
483
|
-
if (seq
|
|
492
|
+
if (seq !== navSeq) return; // a navigation since owns the data
|
|
493
|
+
if (data instanceof RouteRedirect) return followRedirect(data);
|
|
494
|
+
state.data.value = data;
|
|
484
495
|
} catch (e) {
|
|
485
496
|
reportError(e, { phase: "data", path: pathname });
|
|
486
497
|
}
|
|
@@ -628,6 +639,7 @@ export async function navigate(path, replace = false, isPop = false, hydrate = f
|
|
|
628
639
|
reportError(e, { phase: "data", path: url.pathname });
|
|
629
640
|
}
|
|
630
641
|
if (seq !== navSeq) return; // a newer navigation superseded this one
|
|
642
|
+
if (data instanceof RouteRedirect) return followRedirect(data);
|
|
631
643
|
}
|
|
632
644
|
}
|
|
633
645
|
|
package/server/loader.js
CHANGED
|
@@ -155,6 +155,13 @@ export function createLoaderRegistry(loaderModules = {}, { appDir, i18n } = {})
|
|
|
155
155
|
return new Response(method === "HEAD" ? null : json || "null", { status: 200, headers });
|
|
156
156
|
} catch (e) {
|
|
157
157
|
if (isNotFound(e)) return new Response("null", { status: 404, headers });
|
|
158
|
+
if (e instanceof Response) {
|
|
159
|
+
// A redirect is answered as data the router follows: fetch() would follow a
|
|
160
|
+
// real 3xx to an HTML page. Any other thrown Response (401, 403…) passes through.
|
|
161
|
+
const location = e.status >= 300 && e.status < 400 ? e.headers.get("location") : null;
|
|
162
|
+
if (location) return new Response("null", { status: 200, headers: { ...headers, "x-afw-redirect": location } });
|
|
163
|
+
return e;
|
|
164
|
+
}
|
|
158
165
|
console.error(`✗ loader ${pagePath}:`, e?.stack ?? e);
|
|
159
166
|
return Response.json({ error: "Internal Server Error" }, { status: 500 });
|
|
160
167
|
}
|