@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +233 -2
- package/bin/ui-kit.js +12 -0
- package/dist/alchemy/app.d.ts +34 -0
- package/dist/alchemy/app.js +88 -0
- package/dist/alchemy/index.d.ts +11 -0
- package/dist/alchemy/index.js +11 -0
- package/dist/alchemy/live-rooms.d.ts +21 -0
- package/dist/alchemy/live-rooms.js +31 -0
- package/dist/alchemy/migrations.d.ts +28 -0
- package/dist/alchemy/migrations.js +56 -0
- package/dist/alchemy/stage.d.ts +20 -0
- package/dist/alchemy/stage.js +20 -0
- package/dist/alchemy/wrangler-config.d.ts +76 -0
- package/dist/alchemy/wrangler-config.js +82 -0
- package/dist/build/app-modules.d.ts +11 -0
- package/dist/build/app-modules.js +40 -0
- package/dist/build/check.d.ts +4 -0
- package/dist/build/check.js +170 -0
- package/dist/build/cli.d.ts +1 -0
- package/dist/build/cli.js +79 -0
- package/dist/build/feature.d.ts +8 -0
- package/dist/build/feature.js +24 -0
- package/dist/build/index.d.ts +13 -0
- package/dist/build/index.js +13 -0
- package/dist/build/measure-cpu.d.ts +2 -0
- package/dist/build/measure-cpu.js +196 -0
- package/dist/build/prerender.d.ts +5 -0
- package/dist/build/prerender.js +109 -0
- package/dist/build/preview.d.ts +7 -0
- package/dist/build/preview.js +41 -0
- package/dist/build/seo-files.d.ts +27 -0
- package/dist/build/seo-files.js +24 -0
- package/dist/client/head-swap.d.ts +45 -0
- package/dist/client/head-swap.js +183 -0
- package/dist/client/hydration-pass.d.ts +56 -0
- package/dist/client/hydration-pass.js +186 -0
- package/dist/client/hydration-reset.d.ts +82 -0
- package/dist/client/hydration-reset.js +154 -0
- package/dist/client/index.d.ts +37 -0
- package/dist/client/index.js +65 -0
- package/dist/client/nav-client.d.ts +35 -0
- package/dist/client/nav-client.js +371 -0
- package/dist/live/collab/doc-provider.d.ts +74 -0
- package/dist/live/collab/doc-provider.js +412 -0
- package/dist/live/collab/index.d.ts +51 -0
- package/dist/live/collab/index.jsx +142 -0
- package/dist/live/collab-status.d.ts +25 -0
- package/dist/live/collab-status.js +17 -0
- package/dist/live/cursors/cursor-engine.d.ts +57 -0
- package/dist/live/cursors/cursor-engine.js +243 -0
- package/dist/live/cursors/index.d.ts +46 -0
- package/dist/live/cursors/index.jsx +110 -0
- package/dist/live/declaration.d.ts +113 -0
- package/dist/live/declaration.js +94 -0
- package/dist/live/index.d.ts +8 -0
- package/dist/live/index.js +8 -0
- package/dist/live/protocol.d.ts +224 -0
- package/dist/live/protocol.js +272 -0
- package/dist/live/server/doc-store.d.ts +68 -0
- package/dist/live/server/doc-store.js +187 -0
- package/dist/live/server/index.d.ts +60 -0
- package/dist/live/server/index.js +77 -0
- package/dist/live/server/room-presence.d.ts +24 -0
- package/dist/live/server/room-presence.js +123 -0
- package/dist/live/server/room-sockets.d.ts +65 -0
- package/dist/live/server/room-sockets.js +97 -0
- package/dist/live/server/room.d.ts +20 -0
- package/dist/live/server/room.js +348 -0
- package/dist/live/server/route.d.ts +56 -0
- package/dist/live/server/route.js +159 -0
- package/dist/live/server/types.d.ts +163 -0
- package/dist/live/server/types.js +2 -0
- package/dist/logic/agent-actions.d.ts +133 -0
- package/dist/logic/agent-actions.js +150 -0
- package/dist/logic/command-types.d.ts +53 -0
- package/dist/logic/command-types.js +0 -0
- package/dist/logic/dev.d.ts +9 -0
- package/dist/logic/dev.js +10 -0
- package/dist/logic/draft-rebase.d.ts +59 -0
- package/dist/logic/draft-rebase.js +71 -0
- package/dist/logic/form-state.d.ts +105 -0
- package/dist/logic/form-state.js +93 -0
- package/dist/logic/head.d.ts +145 -0
- package/dist/logic/head.js +140 -0
- package/dist/logic/index.d.ts +27 -0
- package/dist/logic/index.js +25 -0
- package/dist/logic/island-def.d.ts +89 -0
- package/dist/logic/island-def.js +59 -0
- package/dist/logic/kit-commands.d.ts +104 -0
- package/dist/logic/kit-commands.js +72 -0
- package/dist/logic/kit-errors.d.ts +25 -0
- package/dist/logic/kit-errors.js +6 -0
- package/dist/logic/listen.d.ts +99 -0
- package/dist/logic/listen.js +92 -0
- package/dist/logic/mutation-def.d.ts +65 -0
- package/dist/logic/mutation-def.js +43 -0
- package/dist/logic/navigation-data.d.ts +50 -0
- package/dist/logic/navigation-data.js +55 -0
- package/dist/logic/navigation-policy.d.ts +74 -0
- package/dist/logic/navigation-policy.js +127 -0
- package/dist/logic/page-model-def.d.ts +108 -0
- package/dist/logic/page-model-def.js +31 -0
- package/dist/logic/path.d.ts +31 -0
- package/dist/logic/path.js +48 -0
- package/dist/logic/query-def.d.ts +56 -0
- package/dist/logic/query-def.js +37 -0
- package/dist/logic/query-key.d.ts +24 -0
- package/dist/logic/query-key.js +35 -0
- package/dist/logic/query-types.d.ts +28 -0
- package/dist/logic/query-types.js +2 -0
- package/dist/logic/resources.d.ts +163 -0
- package/dist/logic/resources.js +62 -0
- package/dist/logic/result.d.ts +30 -0
- package/dist/logic/result.js +23 -0
- package/dist/logic/route-model.d.ts +139 -0
- package/dist/logic/route-model.js +207 -0
- package/dist/logic/route-params.d.ts +80 -0
- package/dist/logic/route-params.js +114 -0
- package/dist/logic/route-table.d.ts +308 -0
- package/dist/logic/route-table.js +206 -0
- package/dist/logic/seo.d.ts +61 -0
- package/dist/logic/seo.js +101 -0
- package/dist/logic/site.d.ts +12 -0
- package/dist/logic/site.js +0 -0
- package/dist/logic/subscriptions.d.ts +177 -0
- package/dist/logic/subscriptions.js +152 -0
- package/dist/render/index.d.ts +32 -0
- package/dist/render/index.js +52 -0
- package/dist/runtime/island-wake.d.ts +38 -0
- package/dist/runtime/island-wake.js +147 -0
- package/dist/runtime/query-scope.d.ts +224 -0
- package/dist/runtime/query-scope.js +222 -0
- package/dist/runtime/room-hub.d.ts +107 -0
- package/dist/runtime/room-hub.js +307 -0
- package/dist/runtime/subscription-runner.d.ts +71 -0
- package/dist/runtime/subscription-runner.js +240 -0
- package/dist/server/bindings.d.ts +41 -0
- package/dist/server/bindings.js +74 -0
- package/dist/server/browser.d.ts +9 -0
- package/dist/server/browser.js +9 -0
- package/dist/server/edge-cache.d.ts +28 -0
- package/dist/server/edge-cache.js +35 -0
- package/dist/server/errors.d.ts +45 -0
- package/dist/server/errors.js +36 -0
- package/dist/server/form-actions-setup.d.ts +1 -0
- package/dist/server/form-actions-setup.js +15 -0
- package/dist/server/form-actions.d.ts +121 -0
- package/dist/server/form-actions.js +211 -0
- package/dist/server/index.d.ts +36 -0
- package/dist/server/index.js +35 -0
- package/dist/server/inputs.d.ts +45 -0
- package/dist/server/inputs.js +77 -0
- package/dist/server/rate-limit.d.ts +29 -0
- package/dist/server/rate-limit.js +32 -0
- package/dist/server/read-cache.d.ts +74 -0
- package/dist/server/read-cache.js +58 -0
- package/dist/server/request-info.d.ts +17 -0
- package/dist/server/request-info.js +11 -0
- package/dist/server/request.d.ts +52 -0
- package/dist/server/request.js +73 -0
- package/dist/server/response-cookies.d.ts +23 -0
- package/dist/server/response-cookies.js +24 -0
- package/dist/server/run-effect.d.ts +42 -0
- package/dist/server/run-effect.js +92 -0
- package/dist/server/run.d.ts +63 -0
- package/dist/server/run.js +66 -0
- package/dist/server/session.d.ts +125 -0
- package/dist/server/session.js +247 -0
- package/dist/test/fake-bindings.d.ts +32 -0
- package/dist/test/fake-bindings.js +108 -0
- package/dist/test/fake-room.d.ts +66 -0
- package/dist/test/fake-room.js +216 -0
- package/dist/test/index.d.ts +19 -0
- package/dist/test/index.js +44 -0
- package/dist/test/probe.d.ts +11 -0
- package/dist/test/probe.js +11 -0
- package/dist/test/solid-jsx.d.ts +23 -0
- package/dist/test/solid-jsx.js +28 -0
- package/dist/test/solid-preload.d.ts +1 -0
- package/dist/test/solid-preload.js +29 -0
- package/dist/ui/code-editor/index.d.ts +21 -0
- package/dist/ui/code-editor/index.jsx +286 -0
- package/dist/ui/collab-text/index.d.ts +29 -0
- package/dist/ui/collab-text/index.jsx +96 -0
- package/dist/ui/components/controls.d.ts +69 -0
- package/dist/ui/components/controls.jsx +81 -0
- package/dist/ui/components/feedback.d.ts +26 -0
- package/dist/ui/components/feedback.jsx +80 -0
- package/dist/ui/components/layout.d.ts +74 -0
- package/dist/ui/components/layout.jsx +146 -0
- package/dist/ui/components/lists.d.ts +69 -0
- package/dist/ui/components/lists.jsx +133 -0
- package/dist/ui/components/navigation.d.ts +73 -0
- package/dist/ui/components/navigation.jsx +112 -0
- package/dist/ui/components/text.d.ts +34 -0
- package/dist/ui/components/text.jsx +163 -0
- package/dist/ui/index.d.ts +7 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/jsx-runtime.d.ts +19 -0
- package/dist/ui/jsx-runtime.js +0 -0
- package/dist/ui/list-keys.d.ts +31 -0
- package/dist/ui/list-keys.js +45 -0
- package/dist/ui/styles/base.css +38 -0
- package/dist/ui/styles/components.css +608 -0
- package/dist/ui/styles/tokens.css +27 -0
- package/dist/ui/styles/transitions.css +23 -0
- package/dist/ui/styles/workbench.css +428 -0
- package/dist/ui/styles.css +9 -0
- package/dist/view/agent-actions-view.d.ts +30 -0
- package/dist/view/agent-actions-view.js +27 -0
- package/dist/view/agent-surface.d.ts +42 -0
- package/dist/view/agent-surface.js +123 -0
- package/dist/view/app-scope.d.ts +51 -0
- package/dist/view/app-scope.js +75 -0
- package/dist/view/boundary.d.ts +18 -0
- package/dist/view/boundary.jsx +26 -0
- package/dist/view/command-runner.d.ts +16 -0
- package/dist/view/command-runner.js +245 -0
- package/dist/view/form.d.ts +82 -0
- package/dist/view/form.jsx +109 -0
- package/dist/view/head-view.d.ts +31 -0
- package/dist/view/head-view.js +46 -0
- package/dist/view/index.d.ts +41 -0
- package/dist/view/index.js +40 -0
- package/dist/view/island-root.d.ts +30 -0
- package/dist/view/island-root.js +36 -0
- package/dist/view/island.d.ts +102 -0
- package/dist/view/island.jsx +154 -0
- package/dist/view/match.d.ts +41 -0
- package/dist/view/match.js +111 -0
- package/dist/view/model.d.ts +92 -0
- package/dist/view/model.js +162 -0
- package/dist/view/navigation-head.d.ts +29 -0
- package/dist/view/navigation-head.jsx +38 -0
- package/dist/view/page-model.d.ts +46 -0
- package/dist/view/page-model.js +146 -0
- package/dist/view/query-effects.d.ts +87 -0
- package/dist/view/query-effects.js +147 -0
- package/dist/view/query-ok.d.ts +45 -0
- package/dist/view/query-ok.js +96 -0
- package/dist/view/query.d.ts +115 -0
- package/dist/view/query.jsx +179 -0
- package/dist/view/result-view.d.ts +14 -0
- package/dist/view/result-view.jsx +15 -0
- package/dist/view/route-pages.d.ts +43 -0
- package/dist/view/route-pages.js +59 -0
- package/dist/view/router.d.ts +44 -0
- package/dist/view/router.js +123 -0
- package/dist/vite/dev-middleware.d.ts +12 -0
- package/dist/vite/dev-middleware.js +39 -0
- package/dist/vite/dev-objects.d.ts +25 -0
- package/dist/vite/dev-objects.js +127 -0
- package/dist/vite/dev-platform.d.ts +19 -0
- package/dist/vite/dev-platform.js +77 -0
- package/dist/vite/dev-seo-files.d.ts +11 -0
- package/dist/vite/dev-seo-files.js +40 -0
- package/dist/vite/index.d.ts +53 -0
- package/dist/vite/index.js +148 -0
- package/dist/vite/local-config.d.ts +34 -0
- package/dist/vite/local-config.js +72 -0
- package/dist/vite/worker-build.d.ts +24 -0
- package/dist/vite/worker-build.js +84 -0
- package/dist/worker/create-worker.d.ts +52 -0
- package/dist/worker/create-worker.js +90 -0
- package/dist/worker/index.d.ts +12 -0
- package/dist/worker/index.js +12 -0
- package/dist/worker/page-routes.d.ts +88 -0
- package/dist/worker/page-routes.js +100 -0
- package/dist/worker/page-status.d.ts +41 -0
- package/dist/worker/page-status.js +23 -0
- package/dist/worker/request-prelude.d.ts +69 -0
- package/dist/worker/request-prelude.js +107 -0
- package/dist/worker/security.d.ts +76 -0
- package/dist/worker/security.js +137 -0
- package/dist/worker/server-app.d.ts +54 -0
- package/dist/worker/server-app.js +76 -0
- package/guards/browser-only.js +5 -0
- package/guards/node-only.js +6 -0
- package/guards/server-only.js +5 -0
- package/migrations/0001-sessions.sql +22 -0
- package/package.json +233 -5
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { ISLAND_ATTR } from '../logic/island-def.js';
|
|
2
|
+
import { CLIENT_NAV_DATA_TYPE } from '../logic/navigation-data.js';
|
|
3
|
+
/**
|
|
4
|
+
* The document side of a client navigation (`nav-client.ts`): which inline
|
|
5
|
+
* scripts the fetched page's CSP allows, the managed head tags it replaces,
|
|
6
|
+
* its stylesheets and island modules, scrolling, focus and the screen
|
|
7
|
+
* reader announcement. DOM only; no Solid.
|
|
8
|
+
*/
|
|
9
|
+
/** Head tags that belong to a page (replaced on every swap). */
|
|
10
|
+
export const MANAGED_HEAD = [
|
|
11
|
+
'title',
|
|
12
|
+
'meta[name]:not([name="viewport"])',
|
|
13
|
+
'meta[property]',
|
|
14
|
+
'link[rel="canonical"]',
|
|
15
|
+
'link[rel="alternate"]',
|
|
16
|
+
'script[type="application/ld+json"]',
|
|
17
|
+
'script[type="application/kit-agent+json"]',
|
|
18
|
+
`script[type="${CLIENT_NAV_DATA_TYPE}"]`,
|
|
19
|
+
].join(', ');
|
|
20
|
+
export const isExecutable = (script) => !script.hasAttribute('src') &&
|
|
21
|
+
(script.type === '' ||
|
|
22
|
+
script.type === 'text/javascript' ||
|
|
23
|
+
script.type === 'module');
|
|
24
|
+
/** The nonce this document's scripts run with (`""` on a page served without CSP, e.g. `vite dev`). */
|
|
25
|
+
export function documentNonce() {
|
|
26
|
+
return (document.querySelector('script[nonce]')?.nonce ?? '');
|
|
27
|
+
}
|
|
28
|
+
/** The island client entry a document loads (a new deploy changes it). */
|
|
29
|
+
export function entryOf(doc) {
|
|
30
|
+
for (const script of doc.querySelectorAll('script[type="module"][src]')) {
|
|
31
|
+
const src = script.getAttribute('src') ?? '';
|
|
32
|
+
if (src.includes('entry-client'))
|
|
33
|
+
return src;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
export async function sha256Base64(text) {
|
|
38
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
|
|
39
|
+
let binary = '';
|
|
40
|
+
for (const byte of new Uint8Array(digest))
|
|
41
|
+
binary += String.fromCharCode(byte);
|
|
42
|
+
return btoa(binary);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The fetched page's inline scripts its response's CSP allows. `undefined`
|
|
46
|
+
* when the navigation must be a full load instead (this document enforces a
|
|
47
|
+
* nonce CSP but the response has none, or the response's script CSP has no
|
|
48
|
+
* nonce: a hash-CSP page). A document and response both without CSP (`vite
|
|
49
|
+
* dev`) allow every inline script, as a direct load would.
|
|
50
|
+
*/
|
|
51
|
+
export async function scriptsAllowedBy(csp, next, documentHasNonce) {
|
|
52
|
+
const inline = [
|
|
53
|
+
...next.querySelectorAll('script'),
|
|
54
|
+
].filter(isExecutable);
|
|
55
|
+
if (csp === null)
|
|
56
|
+
return documentHasNonce ? undefined : new Set(inline);
|
|
57
|
+
const scriptSrc = /(?:^|;)\s*script-src([^;]*)/.exec(csp)?.[1] ?? '';
|
|
58
|
+
const nonce = /'nonce-([^']+)'/.exec(scriptSrc)?.[1];
|
|
59
|
+
if (nonce === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
const hashes = new Set([...scriptSrc.matchAll(/'sha256-([^']+)'/g)].map((m) => m[1]));
|
|
62
|
+
const allowed = new Set();
|
|
63
|
+
for (const script of inline) {
|
|
64
|
+
if (script.getAttribute('nonce') === nonce) {
|
|
65
|
+
allowed.add(script);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (hashes.size > 0 &&
|
|
69
|
+
hashes.has(await sha256Base64(script.textContent ?? ''))) {
|
|
70
|
+
allowed.add(script);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
console.error("[nav] skipped an inline script the page's CSP does not allow");
|
|
74
|
+
}
|
|
75
|
+
return allowed;
|
|
76
|
+
}
|
|
77
|
+
export function viewTransitionsWanted() {
|
|
78
|
+
return (typeof document
|
|
79
|
+
.startViewTransition === 'function' &&
|
|
80
|
+
!matchMedia('(prefers-reduced-motion: reduce)').matches);
|
|
81
|
+
}
|
|
82
|
+
export const round = (ms) => Math.round(ms * 10) / 10;
|
|
83
|
+
/** Resolves when `p` settles or after `ms`, whichever is first. */
|
|
84
|
+
export function settled(p, ms) {
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
const timer = setTimeout(resolve, ms);
|
|
87
|
+
p.then(() => {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
resolve();
|
|
90
|
+
}, () => {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
resolve();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/** Adds the next page's stylesheets this document lacks and waits for them. */
|
|
97
|
+
export function loadStyles(next) {
|
|
98
|
+
const have = new Set([...document.querySelectorAll('link[rel="stylesheet"]')].map((l) => l.getAttribute('href')));
|
|
99
|
+
const waits = [];
|
|
100
|
+
for (const link of next.querySelectorAll('link[rel="stylesheet"]')) {
|
|
101
|
+
if (have.has(link.getAttribute('href')))
|
|
102
|
+
continue;
|
|
103
|
+
const copy = document.importNode(link, true);
|
|
104
|
+
waits.push(new Promise((resolve) => ((copy.onload = resolve), (copy.onerror = resolve))));
|
|
105
|
+
document.head.append(copy);
|
|
106
|
+
}
|
|
107
|
+
return Promise.all(waits);
|
|
108
|
+
}
|
|
109
|
+
/** Loads the next page's hydrated island modules (the pass then renders without waiting on the network). */
|
|
110
|
+
export function preloadIslands(next, table) {
|
|
111
|
+
const names = new Set();
|
|
112
|
+
for (const el of next.querySelectorAll(`[${ISLAND_ATTR.name}]`)) {
|
|
113
|
+
if (el.hasAttribute(ISLAND_ATTR.wake))
|
|
114
|
+
continue;
|
|
115
|
+
names.add(el.getAttribute(ISLAND_ATTR.name) ?? '');
|
|
116
|
+
}
|
|
117
|
+
return Promise.all([...names].map((name) => table[name]?.load()));
|
|
118
|
+
}
|
|
119
|
+
/** Replaces the page-owned head tags and adds the next page's modulepreloads. */
|
|
120
|
+
export function swapHead(next) {
|
|
121
|
+
for (const el of document.head.querySelectorAll(MANAGED_HEAD))
|
|
122
|
+
el.remove();
|
|
123
|
+
for (const el of next.head.querySelectorAll(MANAGED_HEAD))
|
|
124
|
+
document.head.append(document.importNode(el, true));
|
|
125
|
+
const have = new Set([...document.head.querySelectorAll('link[href]')].map((l) => l.getAttribute('href')));
|
|
126
|
+
for (const link of next.head.querySelectorAll('link[rel="modulepreload"]')) {
|
|
127
|
+
if (!have.has(link.getAttribute('href')))
|
|
128
|
+
document.head.append(document.importNode(link, true));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** Re-runs the next page's allowed inline scripts (Solid's bootstrap is a no-op the second time; serialization fills `_$HY.r`). */
|
|
132
|
+
export function runScripts(next, allowed, nonce) {
|
|
133
|
+
for (const script of next.querySelectorAll('script')) {
|
|
134
|
+
if (!allowed.has(script))
|
|
135
|
+
continue;
|
|
136
|
+
const run = document.createElement('script');
|
|
137
|
+
if (nonce !== '')
|
|
138
|
+
run.nonce = nonce;
|
|
139
|
+
if (script.type !== '')
|
|
140
|
+
run.type = script.type;
|
|
141
|
+
run.textContent = script.textContent;
|
|
142
|
+
document.head.append(run);
|
|
143
|
+
run.remove();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export function scrollAfterSwap(url, restore) {
|
|
147
|
+
if (restore !== undefined) {
|
|
148
|
+
window.scrollTo(0, restore);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const id = url.hash ? decodeURIComponent(url.hash.slice(1)) : '';
|
|
152
|
+
const anchor = id ? document.getElementById(id) : null;
|
|
153
|
+
if (anchor)
|
|
154
|
+
anchor.scrollIntoView();
|
|
155
|
+
else
|
|
156
|
+
window.scrollTo(0, 0);
|
|
157
|
+
}
|
|
158
|
+
/** A polite live region that moves with every swap (a region created with its text is often not read). */
|
|
159
|
+
export function createAnnouncer() {
|
|
160
|
+
const el = document.createElement('div');
|
|
161
|
+
el.className = 'kit-sr-only';
|
|
162
|
+
el.setAttribute('role', 'status');
|
|
163
|
+
el.setAttribute('aria-live', 'polite');
|
|
164
|
+
el.setAttribute('aria-atomic', 'true');
|
|
165
|
+
el.setAttribute('data-kit-announcer', '');
|
|
166
|
+
document.body.append(el);
|
|
167
|
+
return el;
|
|
168
|
+
}
|
|
169
|
+
/** Focus the new page's first heading (or `main`) without scrolling, and announce its title. */
|
|
170
|
+
export function focusAndAnnounce(announcer) {
|
|
171
|
+
const target = document.querySelector('main h1') ??
|
|
172
|
+
document.querySelector('main');
|
|
173
|
+
if (target) {
|
|
174
|
+
if (!target.hasAttribute('tabindex'))
|
|
175
|
+
target.setAttribute('tabindex', '-1');
|
|
176
|
+
target.focus({ preventScroll: true });
|
|
177
|
+
}
|
|
178
|
+
announcer.textContent = '';
|
|
179
|
+
// A separate task, so assistive tech sees a change.
|
|
180
|
+
setTimeout(() => {
|
|
181
|
+
announcer.textContent = document.title;
|
|
182
|
+
}, 50);
|
|
183
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Component } from 'solid-js';
|
|
2
|
+
import { type IslandDef, type PageIslandWake } from '../logic/island-def.ts';
|
|
3
|
+
import { type KitScope } from '../runtime/query-scope.ts';
|
|
4
|
+
import type { SiteSettings } from '../view/app-scope.ts';
|
|
5
|
+
/**
|
|
6
|
+
* One hydration pass over a page's island roots: hydrated roots in one
|
|
7
|
+
* synchronous loop, lazy roots on their wake trigger. `startIslands`
|
|
8
|
+
* (`index.ts`) runs it on the first load, and client navigation
|
|
9
|
+
* (`nav-client.ts`) on every swapped-in page.
|
|
10
|
+
*/
|
|
11
|
+
/** Any island table (`defineIslands`). */
|
|
12
|
+
export type AnyIslandTable = {
|
|
13
|
+
readonly [name: string]: IslandDef<any, PageIslandWake>;
|
|
14
|
+
};
|
|
15
|
+
/** A hydrated island root found in the page: element, id, module, props. */
|
|
16
|
+
export interface IslandRoot {
|
|
17
|
+
readonly el: HTMLElement;
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly def: IslandDef<unknown>;
|
|
20
|
+
/** The island module as a resolved `lazy` (preloaded before the pass). */
|
|
21
|
+
readonly component: Component<object>;
|
|
22
|
+
readonly props: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The one hydration pass, as a value that can run once. `hydrateAll` throws
|
|
26
|
+
* if called again (or after the boot task), which is how "late hydration is
|
|
27
|
+
* impossible" holds at runtime too: the kit never calls `hydrate` outside
|
|
28
|
+
* this pass, and the pass cannot be re-entered.
|
|
29
|
+
*
|
|
30
|
+
* Kit-internal; exported for tests.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createHydrationPass(scope: KitScope, site: SiteSettings | null,
|
|
33
|
+
/** Where each hydrated root's disposer goes (client navigation disposes them). */
|
|
34
|
+
disposers?: Map<Element, () => void>): {
|
|
35
|
+
hydrateAll(roots: readonly IslandRoot[]): void;
|
|
36
|
+
};
|
|
37
|
+
/** What one islands pass leaves running (kit-internal; the client navigation runtime disposes it). */
|
|
38
|
+
export interface IslandsPass {
|
|
39
|
+
/** Each island root element of the pass, with its disposer (hydrated root, lazy render, or pending wake). */
|
|
40
|
+
readonly roots: Map<Element, () => void>;
|
|
41
|
+
/** Resolves once every hydrated root has hydrated and the page's models have started. */
|
|
42
|
+
readonly ready: Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* One islands pass over `root` (default: the document) in `scope`: loads
|
|
46
|
+
* every hydrated island module, hydrates every root in one synchronous
|
|
47
|
+
* loop, arms the lazy ones, and starts the page models once all have
|
|
48
|
+
* hydrated. Roots in `skip` (persisted islands, already live) are left
|
|
49
|
+
* alone. Kit-internal: `startIslands` runs it at load; the client
|
|
50
|
+
* navigation runtime runs it once per swap, after `reopenHydration()`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function runIslandsPass(table: AnyIslandTable, site: SiteSettings | null, scope: KitScope, options?: {
|
|
53
|
+
readonly root?: ParentNode;
|
|
54
|
+
readonly skip?: ReadonlySet<Element>;
|
|
55
|
+
}): IslandsPass;
|
|
56
|
+
export declare function hydrateRootsIn(table: AnyIslandTable, site: SiteSettings | null, scope: KitScope, disposers: Map<Element, () => void>, rootNode: ParentNode, skip: ReadonlySet<Element> | undefined): Promise<void>;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { hydrate, render } from '@solidjs/web';
|
|
2
|
+
import { createComponent, flush, onHydrationEnd, } from 'solid-js';
|
|
3
|
+
import { ISLAND_ATTR, } from '../logic/island-def.js';
|
|
4
|
+
import { onWake, recordInteractions, replayInto, } from '../runtime/island-wake.js';
|
|
5
|
+
import {} from '../runtime/query-scope.js';
|
|
6
|
+
import { ScopeProviders } from '../view/app-scope.js';
|
|
7
|
+
import { IslandBody, lazyIsland } from '../view/island-root.js';
|
|
8
|
+
function readProps(el) {
|
|
9
|
+
const text = el.getAttribute(ISLAND_ATTR.props);
|
|
10
|
+
try {
|
|
11
|
+
return text ? JSON.parse(text) : {};
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
console.error(`[islands] bad props on island "${el.getAttribute(ISLAND_ATTR.name)}":`, error);
|
|
15
|
+
return {};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The one hydration pass, as a value that can run once. `hydrateAll` throws
|
|
20
|
+
* if called again (or after the boot task), which is how "late hydration is
|
|
21
|
+
* impossible" holds at runtime too: the kit never calls `hydrate` outside
|
|
22
|
+
* this pass, and the pass cannot be re-entered.
|
|
23
|
+
*
|
|
24
|
+
* Kit-internal; exported for tests.
|
|
25
|
+
*/
|
|
26
|
+
export function createHydrationPass(scope, site,
|
|
27
|
+
/** Where each hydrated root's disposer goes (client navigation disposes them). */
|
|
28
|
+
disposers) {
|
|
29
|
+
let used = false;
|
|
30
|
+
return {
|
|
31
|
+
hydrateAll(roots) {
|
|
32
|
+
if (used) {
|
|
33
|
+
throw new Error('[islands] late hydration: every island root hydrates in the first pass at load (solidjs/solid#3845). A lazy island is a client-only render.');
|
|
34
|
+
}
|
|
35
|
+
used = true;
|
|
36
|
+
// Page models start once every root has hydrated (each reports from
|
|
37
|
+
// its `onSettled`) and Solid's hydration phase is over (a write
|
|
38
|
+
// while any root still claims DOM would render into the claim).
|
|
39
|
+
const pageHydrated = () => onHydrationEnd(() => scope.page.markHydrated());
|
|
40
|
+
let waiting = roots.length;
|
|
41
|
+
const settledOne = () => {
|
|
42
|
+
waiting -= 1;
|
|
43
|
+
if (waiting === 0)
|
|
44
|
+
pageHydrated();
|
|
45
|
+
};
|
|
46
|
+
if (waiting === 0) {
|
|
47
|
+
pageHydrated();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// One synchronous loop: every root starts hydrating in this task.
|
|
51
|
+
for (const root of roots) {
|
|
52
|
+
let reported = false;
|
|
53
|
+
const report = () => {
|
|
54
|
+
if (reported)
|
|
55
|
+
return;
|
|
56
|
+
reported = true;
|
|
57
|
+
settledOne();
|
|
58
|
+
};
|
|
59
|
+
try {
|
|
60
|
+
const dispose = hydrate(() => createComponent(IslandBody, {
|
|
61
|
+
scope,
|
|
62
|
+
site,
|
|
63
|
+
component: root.component,
|
|
64
|
+
props: root.props,
|
|
65
|
+
onHydrated: report,
|
|
66
|
+
}), root.el, { renderId: root.id });
|
|
67
|
+
disposers?.set(root.el, dispose);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
console.error(`[islands] hydrating "${root.def.name}" failed; its server HTML stays:`, error);
|
|
71
|
+
root.el.setAttribute(ISLAND_ATTR.state, 'failed');
|
|
72
|
+
report();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Client-only render of a lazy page island over its fallback, when it wakes. */
|
|
79
|
+
function wakeLazy(el, def, wake, scope, site, disposers) {
|
|
80
|
+
const recorder = recordInteractions(el);
|
|
81
|
+
let disposed = false;
|
|
82
|
+
let disposeRender;
|
|
83
|
+
const cancelWake = onWake(el, wake, () => {
|
|
84
|
+
def.load().then(
|
|
85
|
+
// A client render while Solid's hydration phase is open (an `idle`
|
|
86
|
+
// wake can fire before slow hydrated roots settle) would take the
|
|
87
|
+
// hydration path and throw: render only once the page has hydrated.
|
|
88
|
+
(mod) => scope.page.afterHydration(() => {
|
|
89
|
+
if (disposed)
|
|
90
|
+
return;
|
|
91
|
+
recorder.stop();
|
|
92
|
+
const props = readProps(el);
|
|
93
|
+
el.replaceChildren();
|
|
94
|
+
disposeRender = render(() => createComponent(ScopeProviders, {
|
|
95
|
+
scope,
|
|
96
|
+
site,
|
|
97
|
+
get children() {
|
|
98
|
+
return createComponent(mod.default, props);
|
|
99
|
+
},
|
|
100
|
+
}), el);
|
|
101
|
+
flush();
|
|
102
|
+
el.setAttribute(ISLAND_ATTR.state, 'active');
|
|
103
|
+
replayInto(el, recorder.last());
|
|
104
|
+
}), (error) => {
|
|
105
|
+
console.error(`[islands] failed to load "${def.name}"; keeping its fallback:`, error);
|
|
106
|
+
recorder.stop();
|
|
107
|
+
el.setAttribute(ISLAND_ATTR.state, 'failed');
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
disposers.set(el, () => {
|
|
111
|
+
disposed = true;
|
|
112
|
+
cancelWake();
|
|
113
|
+
recorder.stop();
|
|
114
|
+
disposeRender?.();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* One islands pass over `root` (default: the document) in `scope`: loads
|
|
119
|
+
* every hydrated island module, hydrates every root in one synchronous
|
|
120
|
+
* loop, arms the lazy ones, and starts the page models once all have
|
|
121
|
+
* hydrated. Roots in `skip` (persisted islands, already live) are left
|
|
122
|
+
* alone. Kit-internal: `startIslands` runs it at load; the client
|
|
123
|
+
* navigation runtime runs it once per swap, after `reopenHydration()`.
|
|
124
|
+
*/
|
|
125
|
+
export function runIslandsPass(table, site, scope, options = {}) {
|
|
126
|
+
const roots = new Map();
|
|
127
|
+
const ready = new Promise((resolve) => scope.page.afterHydration(resolve));
|
|
128
|
+
void hydrateRootsIn(table, site, scope, roots, options.root ?? document, options.skip);
|
|
129
|
+
return { roots, ready };
|
|
130
|
+
}
|
|
131
|
+
export async function hydrateRootsIn(table, site, scope, disposers, rootNode, skip) {
|
|
132
|
+
const pass = createHydrationPass(scope, site, disposers);
|
|
133
|
+
const found = [
|
|
134
|
+
...rootNode.querySelectorAll(`[${ISLAND_ATTR.name}]`),
|
|
135
|
+
].filter((el) => !skip?.has(el));
|
|
136
|
+
const eager = [];
|
|
137
|
+
for (const el of found) {
|
|
138
|
+
const name = el.getAttribute(ISLAND_ATTR.name) ?? '';
|
|
139
|
+
const def = table[name];
|
|
140
|
+
if (!def) {
|
|
141
|
+
console.error(`[islands] no island "${name}" in the island table`);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const wake = el.getAttribute(ISLAND_ATTR.wake);
|
|
145
|
+
if (wake === null) {
|
|
146
|
+
if (def.wake !== 'load') {
|
|
147
|
+
console.error(`[islands] "${name}" is a lazy island but its root has no wake`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
eager.push({ el, def });
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
// Lazy: the server HTML is its fallback; never hydrated.
|
|
154
|
+
wakeLazy(el, def, def.wake === 'load' ? 'visible' : def.wake, scope, site, disposers);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// One resolved `lazy` per island kind, preloaded: during the pass it
|
|
158
|
+
// renders synchronously (a pending module would defer the root).
|
|
159
|
+
const components = new Map();
|
|
160
|
+
const componentOf = (def) => {
|
|
161
|
+
let component = components.get(def.name);
|
|
162
|
+
if (!component) {
|
|
163
|
+
component = lazyIsland(def.load);
|
|
164
|
+
components.set(def.name, component);
|
|
165
|
+
}
|
|
166
|
+
return component;
|
|
167
|
+
};
|
|
168
|
+
const loaded = await Promise.allSettled(eager.map((root) => componentOf(root.def).preload()));
|
|
169
|
+
const roots = [];
|
|
170
|
+
loaded.forEach((outcome, i) => {
|
|
171
|
+
const root = eager[i];
|
|
172
|
+
if (outcome.status === 'rejected') {
|
|
173
|
+
console.error(`[islands] failed to load "${root.def.name}"; its server HTML stays:`, outcome.reason);
|
|
174
|
+
root.el.setAttribute(ISLAND_ATTR.state, 'failed');
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
roots.push({
|
|
178
|
+
el: root.el,
|
|
179
|
+
id: root.el.getAttribute(ISLAND_ATTR.id) ?? '',
|
|
180
|
+
def: root.def,
|
|
181
|
+
component: componentOf(root.def),
|
|
182
|
+
props: readProps(root.el),
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
pass.hydrateAll(roots);
|
|
186
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kit's only contact with Solid's private hydration globals, for the
|
|
3
|
+
* client navigation runtime's second (third, ...) hydration pass in one
|
|
4
|
+
* document (`docs/guides/ui-kit-page-transitions.md`, layer 2).
|
|
5
|
+
*
|
|
6
|
+
* Solid 2 hydrates server HTML once per document: `@solidjs/web`'s
|
|
7
|
+
* `hydrate()` starts with `if (_$HY.done) return render(...)` (a client
|
|
8
|
+
* render that throws the server DOM away), and `_$HY.done` turns true in a
|
|
9
|
+
* `setTimeout` after the first pass drains (solidjs/solid#3845, closed as
|
|
10
|
+
* intended: late hydration of DOM that has sat in the document is unsafe).
|
|
11
|
+
* A client navigation hydrates *fresh* DOM: parsed from the next page's
|
|
12
|
+
* response, inserted and claimed in the same task, with freshly serialized
|
|
13
|
+
* data under fresh keys. For that one case the kit:
|
|
14
|
+
*
|
|
15
|
+
* 1. gives every island root of the fetched page its own id namespace
|
|
16
|
+
* (`namespaceIslandIds`): Solid keeps per-id state at module level
|
|
17
|
+
* (`_fragments`, `_$HY.r`), and a reused `i0_` hangs its boundaries
|
|
18
|
+
* (rendered, never bound, no error);
|
|
19
|
+
* 2. re-opens hydration (`reopenHydration`): `done`, `completed`, `events`;
|
|
20
|
+
* 3. forgets the serialized data of roots that are gone (`forgetHydrationData`),
|
|
21
|
+
* or `_$HY.r` grows by every page's unclaimed entries.
|
|
22
|
+
*
|
|
23
|
+
* Pinned to Solid 2.0.4 / @solidjs/web 2.0.4 (`SOLID_HYDRATION_PIN`):
|
|
24
|
+
* `tests/hydration-reset.test.ts` reads the installed Solid sources and
|
|
25
|
+
* fails loudly when any line this relies on moves, and the browser check
|
|
26
|
+
* (`tests/e2e/client-nav.e2e.ts`) asserts 0 replaced nodes and bound
|
|
27
|
+
* handlers after a client navigation. If it breaks, the runtime falls back
|
|
28
|
+
* to full page loads (`hydrationReopenable()` is false).
|
|
29
|
+
*/
|
|
30
|
+
/** The Solid versions the reset is written against (exact pins in every package.json). */
|
|
31
|
+
export declare const SOLID_HYDRATION_PIN: {
|
|
32
|
+
readonly 'solid-js': '2.0.4';
|
|
33
|
+
readonly '@solidjs/web': '2.0.4';
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Whether this document's Solid looks like the one the reset was written
|
|
37
|
+
* for (the bootstrap ran and `_$HY` has the expected shape). The client
|
|
38
|
+
* navigation runtime checks it before every swap and does a full load when
|
|
39
|
+
* it is false.
|
|
40
|
+
*/
|
|
41
|
+
export declare function hydrationReopenable(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Re-opens Solid's hydration for one fresh pass. Call it after the fetched
|
|
44
|
+
* page's body is in the document and its serialization scripts have run,
|
|
45
|
+
* and before hydrating its island roots, all in the same task.
|
|
46
|
+
*
|
|
47
|
+
* @throws When `_$HY` is missing (the page never loaded Solid's bootstrap).
|
|
48
|
+
*/
|
|
49
|
+
export declare function reopenHydration(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Prefixes every `_$HY.r["<id>…` key in a script body for the island root
|
|
52
|
+
* ids `ids` (`i0_` → `n3i0_`). Pure; exported for tests.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* namespaceScriptKeys('_$HY.r["i0_0"]=1;_$HY.r["i10_"]=2', ["i0_"], "n3");
|
|
57
|
+
* // '_$HY.r["n3i0_0"]=1;_$HY.r["i10_"]=2'
|
|
58
|
+
* namespaceScriptKeys('_$HY.r["_assets"]=($R[9]={i0_001:"/a.js",i10_001:"/b.js"})', ["i0_"], "n3");
|
|
59
|
+
* // '_$HY.r["_assets"]=($R[9]={n3i0_001:"/a.js",i10_001:"/b.js"})'
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare function namespaceScriptKeys(text: string, ids: readonly string[], prefix: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Gives the fetched page's hydrated island roots a fresh id namespace:
|
|
65
|
+
* the root's `data-kit-island-id`, every `_hk` under it that starts with
|
|
66
|
+
* that id, and the `_$HY.r` keys in the page's inline scripts.
|
|
67
|
+
*
|
|
68
|
+
* @param doc - the fetched page, parsed (`DOMParser`), not yet in the document
|
|
69
|
+
* @param prefix - unique per navigation in this document (`n3`)
|
|
70
|
+
* @returns The new root ids (`n3i0_`, ...), for `forgetHydrationData` later.
|
|
71
|
+
*/
|
|
72
|
+
export declare function namespaceIslandIds(doc: Document, prefix: string): string[];
|
|
73
|
+
/**
|
|
74
|
+
* Deletes the serialized hydration data (`_$HY.r`) of island roots that
|
|
75
|
+
* are gone (disposed by a client navigation, or replaced by a persisted
|
|
76
|
+
* island). Root ids end in `_`, so prefixes never overlap (`i1_` vs `i10_`).
|
|
77
|
+
*
|
|
78
|
+
* @returns How many entries were deleted.
|
|
79
|
+
*/
|
|
80
|
+
export declare function forgetHydrationData(rootIds: readonly string[]): number;
|
|
81
|
+
/** Number of `_$HY.r` entries (leak checks in the browser tests). */
|
|
82
|
+
export declare function hydrationDataSize(): number;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { ISLAND_ATTR } from '../logic/island-def.js';
|
|
2
|
+
/**
|
|
3
|
+
* The kit's only contact with Solid's private hydration globals, for the
|
|
4
|
+
* client navigation runtime's second (third, ...) hydration pass in one
|
|
5
|
+
* document (`docs/guides/ui-kit-page-transitions.md`, layer 2).
|
|
6
|
+
*
|
|
7
|
+
* Solid 2 hydrates server HTML once per document: `@solidjs/web`'s
|
|
8
|
+
* `hydrate()` starts with `if (_$HY.done) return render(...)` (a client
|
|
9
|
+
* render that throws the server DOM away), and `_$HY.done` turns true in a
|
|
10
|
+
* `setTimeout` after the first pass drains (solidjs/solid#3845, closed as
|
|
11
|
+
* intended: late hydration of DOM that has sat in the document is unsafe).
|
|
12
|
+
* A client navigation hydrates *fresh* DOM: parsed from the next page's
|
|
13
|
+
* response, inserted and claimed in the same task, with freshly serialized
|
|
14
|
+
* data under fresh keys. For that one case the kit:
|
|
15
|
+
*
|
|
16
|
+
* 1. gives every island root of the fetched page its own id namespace
|
|
17
|
+
* (`namespaceIslandIds`): Solid keeps per-id state at module level
|
|
18
|
+
* (`_fragments`, `_$HY.r`), and a reused `i0_` hangs its boundaries
|
|
19
|
+
* (rendered, never bound, no error);
|
|
20
|
+
* 2. re-opens hydration (`reopenHydration`): `done`, `completed`, `events`;
|
|
21
|
+
* 3. forgets the serialized data of roots that are gone (`forgetHydrationData`),
|
|
22
|
+
* or `_$HY.r` grows by every page's unclaimed entries.
|
|
23
|
+
*
|
|
24
|
+
* Pinned to Solid 2.0.4 / @solidjs/web 2.0.4 (`SOLID_HYDRATION_PIN`):
|
|
25
|
+
* `tests/hydration-reset.test.ts` reads the installed Solid sources and
|
|
26
|
+
* fails loudly when any line this relies on moves, and the browser check
|
|
27
|
+
* (`tests/e2e/client-nav.e2e.ts`) asserts 0 replaced nodes and bound
|
|
28
|
+
* handlers after a client navigation. If it breaks, the runtime falls back
|
|
29
|
+
* to full page loads (`hydrationReopenable()` is false).
|
|
30
|
+
*/
|
|
31
|
+
/** The Solid versions the reset is written against (exact pins in every package.json). */
|
|
32
|
+
export const SOLID_HYDRATION_PIN = {
|
|
33
|
+
'solid-js': '2.0.4',
|
|
34
|
+
'@solidjs/web': '2.0.4',
|
|
35
|
+
};
|
|
36
|
+
function hydrationGlobal() {
|
|
37
|
+
return globalThis._$HY;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Whether this document's Solid looks like the one the reset was written
|
|
41
|
+
* for (the bootstrap ran and `_$HY` has the expected shape). The client
|
|
42
|
+
* navigation runtime checks it before every swap and does a full load when
|
|
43
|
+
* it is false.
|
|
44
|
+
*/
|
|
45
|
+
export function hydrationReopenable() {
|
|
46
|
+
const hy = hydrationGlobal();
|
|
47
|
+
return (hy !== undefined &&
|
|
48
|
+
typeof hy.r === 'object' &&
|
|
49
|
+
hy.r !== null &&
|
|
50
|
+
(hy.done === undefined || typeof hy.done === 'boolean'));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Re-opens Solid's hydration for one fresh pass. Call it after the fetched
|
|
54
|
+
* page's body is in the document and its serialization scripts have run,
|
|
55
|
+
* and before hydrating its island roots, all in the same task.
|
|
56
|
+
*
|
|
57
|
+
* @throws When `_$HY` is missing (the page never loaded Solid's bootstrap).
|
|
58
|
+
*/
|
|
59
|
+
export function reopenHydration() {
|
|
60
|
+
const hy = hydrationGlobal();
|
|
61
|
+
if (!hy)
|
|
62
|
+
throw new Error("[nav] no _$HY: this page never ran Solid's hydration bootstrap");
|
|
63
|
+
hy.done = false;
|
|
64
|
+
hy.completed = new WeakSet();
|
|
65
|
+
// No event capture: the swap and the pass run in one task, so no user
|
|
66
|
+
// event can land on the new DOM before it is claimed.
|
|
67
|
+
hy.events = null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Prefixes every `_$HY.r["<id>…` key in a script body for the island root
|
|
71
|
+
* ids `ids` (`i0_` → `n3i0_`). Pure; exported for tests.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* namespaceScriptKeys('_$HY.r["i0_0"]=1;_$HY.r["i10_"]=2', ["i0_"], "n3");
|
|
76
|
+
* // '_$HY.r["n3i0_0"]=1;_$HY.r["i10_"]=2'
|
|
77
|
+
* namespaceScriptKeys('_$HY.r["_assets"]=($R[9]={i0_001:"/a.js",i10_001:"/b.js"})', ["i0_"], "n3");
|
|
78
|
+
* // '_$HY.r["_assets"]=($R[9]={n3i0_001:"/a.js",i10_001:"/b.js"})'
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function namespaceScriptKeys(text, ids, prefix) {
|
|
82
|
+
if (ids.length === 0)
|
|
83
|
+
return text;
|
|
84
|
+
const alternatives = ids
|
|
85
|
+
.map((id) => id.replace(/[^a-zA-Z0-9_]/g, ''))
|
|
86
|
+
.join('|');
|
|
87
|
+
const keyed = text.replace(new RegExp(`_\\$HY\\.r\\["(${alternatives})`, 'g'), `_$HY.r["${prefix}$1`);
|
|
88
|
+
// The module maps (`_$HY.r["_assets"]`, `_$HY.r["<boundary id>_assets"]`
|
|
89
|
+
// = `{i0_00116: "/assets/x.js", ...}`):
|
|
90
|
+
// Solid looks a nested `lazy()` up by its hydration id in `_$HY.modules`.
|
|
91
|
+
const assetKey = new RegExp(`([{,])((?:${alternatives})[A-Za-z0-9_]*):`, 'g');
|
|
92
|
+
return keyed.replace(/(_\$HY\.r\["[^"]*_assets"\]=(?:\$R\[\d+\]=)?\(?(?:\$R\[\d+\]=)?)(\{[^}]*\})/g, (_m, head, map) => head + map.replace(assetKey, `$1${prefix}$2:`));
|
|
93
|
+
}
|
|
94
|
+
const isExecutable = (script) => !script.hasAttribute('src') &&
|
|
95
|
+
(script.type === '' ||
|
|
96
|
+
script.type === 'text/javascript' ||
|
|
97
|
+
script.type === 'module');
|
|
98
|
+
/**
|
|
99
|
+
* Gives the fetched page's hydrated island roots a fresh id namespace:
|
|
100
|
+
* the root's `data-kit-island-id`, every `_hk` under it that starts with
|
|
101
|
+
* that id, and the `_$HY.r` keys in the page's inline scripts.
|
|
102
|
+
*
|
|
103
|
+
* @param doc - the fetched page, parsed (`DOMParser`), not yet in the document
|
|
104
|
+
* @param prefix - unique per navigation in this document (`n3`)
|
|
105
|
+
* @returns The new root ids (`n3i0_`, ...), for `forgetHydrationData` later.
|
|
106
|
+
*/
|
|
107
|
+
export function namespaceIslandIds(doc, prefix) {
|
|
108
|
+
const ids = [];
|
|
109
|
+
for (const root of doc.querySelectorAll(`[${ISLAND_ATTR.id}]`)) {
|
|
110
|
+
const id = root.getAttribute(ISLAND_ATTR.id) ?? '';
|
|
111
|
+
if (id === '')
|
|
112
|
+
continue;
|
|
113
|
+
ids.push(id);
|
|
114
|
+
root.setAttribute(ISLAND_ATTR.id, prefix + id);
|
|
115
|
+
for (const node of root.querySelectorAll('[_hk]')) {
|
|
116
|
+
const hk = node.getAttribute('_hk') ?? '';
|
|
117
|
+
if (hk.startsWith(id))
|
|
118
|
+
node.setAttribute('_hk', prefix + hk);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (ids.length > 0) {
|
|
122
|
+
for (const script of doc.querySelectorAll('script')) {
|
|
123
|
+
if (isExecutable(script) && script.textContent) {
|
|
124
|
+
script.textContent = namespaceScriptKeys(script.textContent, ids, prefix);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return ids.map((id) => prefix + id);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Deletes the serialized hydration data (`_$HY.r`) of island roots that
|
|
132
|
+
* are gone (disposed by a client navigation, or replaced by a persisted
|
|
133
|
+
* island). Root ids end in `_`, so prefixes never overlap (`i1_` vs `i10_`).
|
|
134
|
+
*
|
|
135
|
+
* @returns How many entries were deleted.
|
|
136
|
+
*/
|
|
137
|
+
export function forgetHydrationData(rootIds) {
|
|
138
|
+
const r = hydrationGlobal()?.r;
|
|
139
|
+
if (!r || rootIds.length === 0)
|
|
140
|
+
return 0;
|
|
141
|
+
let deleted = 0;
|
|
142
|
+
for (const key of Object.keys(r)) {
|
|
143
|
+
if (rootIds.some((id) => key.startsWith(id))) {
|
|
144
|
+
delete r[key];
|
|
145
|
+
deleted += 1;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return deleted;
|
|
149
|
+
}
|
|
150
|
+
/** Number of `_$HY.r` entries (leak checks in the browser tests). */
|
|
151
|
+
export function hydrationDataSize() {
|
|
152
|
+
const r = hydrationGlobal()?.r;
|
|
153
|
+
return r ? Object.keys(r).length : 0;
|
|
154
|
+
}
|