@opentf/afw 0.35.0 → 0.37.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/index.js +4 -2
- package/package.json +1 -1
- package/runtime/code-block.js +4 -3
- package/runtime/hydrate.js +5 -3
- 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/index.js
CHANGED
|
@@ -7,7 +7,9 @@ export * from "./runtime/index.js";
|
|
|
7
7
|
// `Link` is shipped as JSX source and compiled by the consuming app's pipeline
|
|
8
8
|
// (the `.jsx` lands in the module graph and afwc transforms it). Its compiled
|
|
9
9
|
// module self-registers `afw-link` (CSR `customElements.define` / SSG `defineSSG`).
|
|
10
|
-
// The bare import forces that registration to be retained — `<Link>`
|
|
11
|
-
//
|
|
10
|
+
// The bare import forces that registration to be retained — call sites address `<Link>`
|
|
11
|
+
// by its literal `afw-link` tag (a framework built-in), not `Link.tag`, so the binding
|
|
12
|
+
// alone would be tree-shaken. (Other compiled components register on first `.tag` read
|
|
13
|
+
// and need no bare import.)
|
|
12
14
|
import "./components/Link.jsx";
|
|
13
15
|
export { default as Link } from "./components/Link.jsx";
|
package/package.json
CHANGED
package/runtime/code-block.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
//! Code block — a built-in that renders a (
|
|
1
|
+
//! Code block — a built-in that renders a code block (plain, or highlighted at build time)
|
|
2
2
|
//! and **owns its copy behavior**.
|
|
3
3
|
//
|
|
4
4
|
// <CodeFence html={"<div class='afw-code'>…<button class='afw-copy'>…</button><pre>…</pre></div>"} />
|
|
5
5
|
//
|
|
6
6
|
// The MDX front-end emits this for fenced code (compiles to `afw-internal-code-block`,
|
|
7
|
-
// like `<RawHtml>` → `afw-internal-raw-html`). The
|
|
8
|
-
// string from the
|
|
7
|
+
// like `<RawHtml>` → `afw-internal-raw-html`). The markup — escaped code, which
|
|
8
|
+
// afw-docs' build plugin highlights — is a trusted string from the build, so it's
|
|
9
|
+
// assigned to `innerHTML`; the difference from
|
|
9
10
|
// `RawHtml` is that on connect this element wires its own `.afw-copy` button to copy
|
|
10
11
|
// the `<pre>` text. That keeps copy working wherever a code block is rendered (docs,
|
|
11
12
|
// blog, anywhere) — no delegated listener in a layout. SSG renders the string inline
|
package/runtime/hydrate.js
CHANGED
|
@@ -76,9 +76,11 @@ export class HydrationMismatch extends Error {
|
|
|
76
76
|
//
|
|
77
77
|
// Why it initializes from the DOM sentinel, not `false`: a custom element upgrades — and
|
|
78
78
|
// its `connectedCallback` runs the build-vs-adopt switch — the instant its class is
|
|
79
|
-
// `customElements.define`d.
|
|
80
|
-
// (
|
|
81
|
-
//
|
|
79
|
+
// `customElements.define`d. Compiled components register on first use (the adopt walk's
|
|
80
|
+
// `claimElement(cur, X.tag)`), but framework built-ins register when their module loads
|
|
81
|
+
// (`<Link>` from `@opentf/afw`, the `afw-internal-*` elements), so they are defined and
|
|
82
|
+
// upgraded during the entry bundle's evaluation, **before** `mountApp` runs
|
|
83
|
+
// `beginHydration()`. If the flag were
|
|
82
84
|
// still `false` then, every server-rendered `<afw-link>` would take the *build* arm —
|
|
83
85
|
// re-wrapping the server subtree it should have adopted (a silent `<a><a>` double-build)
|
|
84
86
|
// — long before the router ever starts hydrating. So the flag is seeded synchronously
|
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
|
}
|