what-core 0.14.0 → 0.15.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/LICENSE +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/hydration-data.js
CHANGED
|
@@ -1,26 +1,103 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// The hydration data channel: what the server rendered with, read back by the
|
|
2
|
+
// client so it can hydrate without fetching anything again.
|
|
3
|
+
//
|
|
4
|
+
// One format, one reader. A buffered page carries a single element
|
|
5
|
+
//
|
|
6
|
+
// <script id="__what_data" type="application/json">PAYLOAD</script>
|
|
7
|
+
//
|
|
8
|
+
// and a streamed page carries that element in its shell plus one
|
|
9
|
+
//
|
|
10
|
+
// <script type="application/json" data-what-data>PAYLOAD</script>
|
|
11
|
+
//
|
|
12
|
+
// per streamed boundary, sent before the boundary's content is swapped in.
|
|
13
|
+
// Every element is serialized with what-server's serializeState, so it is
|
|
14
|
+
// script-safe JSON that a strict CSP does not block and an agent can read.
|
|
15
|
+
// Only <script type="application/json"> elements are read: the page's own
|
|
16
|
+
// markup can carry any id or attribute (a heading slug, a user's profile
|
|
17
|
+
// anchor), but it cannot make a script element, so it cannot stand in for the
|
|
18
|
+
// payload. PAYLOAD holds any of:
|
|
19
|
+
//
|
|
20
|
+
// loaderData the page loader's result (shell only)
|
|
21
|
+
// islandStores island store snapshots
|
|
22
|
+
// resources { [key]: value } for resources created with a `key`
|
|
23
|
+
// owned { [ownerKey]: value } for keyless resources (see ownerKey)
|
|
24
|
+
// owners { [ownerPath]: componentName } for the components behind
|
|
25
|
+
// the `owned` keys
|
|
26
|
+
// errors { [key]: { message, code? } } for resources that failed
|
|
27
|
+
// ownedErrors { [ownerKey]: { message, code? } }
|
|
28
|
+
// caught { [ownerPath]: { message, code? } } for the <ErrorBoundary>s
|
|
29
|
+
// that showed their fallback for a value that failed: the
|
|
30
|
+
// client keeps that fallback, showing that error
|
|
31
|
+
// failed { [boundaryId]: { message, code? } } for streamed boundaries
|
|
32
|
+
// the server could not render; their fallback stays and the
|
|
33
|
+
// client renders them itself
|
|
34
|
+
// head { [boundaryId]: { title?, meta?, link? } } <Head> tags a
|
|
35
|
+
// streamed boundary set after the shell (noScripts streams)
|
|
36
|
+
// loaded { [key]: 1 } for the `resources` the page's loader filled
|
|
37
|
+
// before the render began (shell only)
|
|
38
|
+
//
|
|
39
|
+
// Every read first merges the elements it has not seen yet, so data that
|
|
40
|
+
// arrives with a late boundary is found by the same lookups.
|
|
5
41
|
|
|
6
|
-
|
|
42
|
+
import { isHydrating, _resumeBoundary, _rowKeyDigest, _rowKeySalt, _installRowEntry } from './render.js';
|
|
43
|
+
import { getCurrentComponent } from './dom.js';
|
|
44
|
+
import { _currentComponent, _installSeedOps, _installStoreSeedOps, _serverStoreKey, __DEV__ } from './reactive.js';
|
|
45
|
+
import { getServerContext, isServerRender } from './server-context.js';
|
|
46
|
+
import { report } from './diagnostics.js';
|
|
7
47
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
48
|
+
/** @type {any} */
|
|
49
|
+
let _data;
|
|
50
|
+
/** @type {Set<Element> | null} */
|
|
51
|
+
let _merged = null;
|
|
52
|
+
|
|
53
|
+
const MAPS = ['resources', 'owned', 'owners', 'errors', 'ownedErrors', 'caught', 'failed', 'islandStores', 'head', 'loaded'];
|
|
54
|
+
|
|
55
|
+
const DATA_ELEMENTS = 'script[type="application/json"]#__what_data, script[type="application/json"][data-what-data]';
|
|
56
|
+
|
|
57
|
+
// Only scan() calls this, once it has created _merged.
|
|
58
|
+
function mergeElement(el) {
|
|
59
|
+
/** @type {Set<Element>} */ (_merged).add(el);
|
|
60
|
+
let payload;
|
|
13
61
|
try {
|
|
14
|
-
|
|
62
|
+
payload = JSON.parse(el.textContent);
|
|
15
63
|
} catch {
|
|
16
|
-
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!payload || typeof payload !== 'object') return;
|
|
67
|
+
if (_data === null) _data = {};
|
|
68
|
+
if ('loaderData' in payload) _data.loaderData = payload.loaderData;
|
|
69
|
+
for (const name of MAPS) {
|
|
70
|
+
if (payload[name]) _data[name] = Object.assign(_data[name] || {}, payload[name]);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function scan() {
|
|
75
|
+
if (typeof document === 'undefined') return;
|
|
76
|
+
if (_merged === null) {
|
|
77
|
+
_merged = new Set();
|
|
78
|
+
_data = null;
|
|
79
|
+
}
|
|
80
|
+
for (const el of document.querySelectorAll(DATA_ELEMENTS)) {
|
|
81
|
+
if (!_merged.has(el)) mergeElement(el);
|
|
17
82
|
}
|
|
18
|
-
return _cache;
|
|
19
83
|
}
|
|
20
84
|
|
|
21
|
-
|
|
85
|
+
function isDataElement(el) {
|
|
86
|
+
return el.tagName === 'SCRIPT' && el.getAttribute('type') === 'application/json'
|
|
87
|
+
&& (el.id === '__what_data' || el.hasAttribute('data-what-data'));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The merged payload, or null when the page carries none. */
|
|
91
|
+
export function __readHydrationData() {
|
|
92
|
+
if (typeof document === 'undefined') return null;
|
|
93
|
+
scan();
|
|
94
|
+
return _data;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Test/HMR hook: forget everything read so far; the next read parses again. */
|
|
22
98
|
export function __resetHydrationData() {
|
|
23
|
-
|
|
99
|
+
_data = undefined;
|
|
100
|
+
_merged = null;
|
|
24
101
|
}
|
|
25
102
|
|
|
26
103
|
export function getLoaderData() {
|
|
@@ -28,7 +105,773 @@ export function getLoaderData() {
|
|
|
28
105
|
return data ? data.loaderData : undefined;
|
|
29
106
|
}
|
|
30
107
|
|
|
108
|
+
function lookup(map, key) {
|
|
109
|
+
const data = __readHydrationData();
|
|
110
|
+
return data && data[map] && Object.hasOwn(data[map], key) ? data[map][key] : undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
31
113
|
export function getResource(key) {
|
|
114
|
+
return lookup('resources', key);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Whether the page's loader filled the keyed resource before the render began. */
|
|
118
|
+
export function _loadedResource(key) {
|
|
119
|
+
return lookup('loaded', key) !== undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The server's error record for a keyed resource, or undefined. */
|
|
123
|
+
export function getResourceError(key) {
|
|
124
|
+
return lookup('errors', key);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// --- Owner keys ---
|
|
128
|
+
//
|
|
129
|
+
// A keyless resource is named by where it was created: the owner path of its
|
|
130
|
+
// component plus its position among that component's keyless resources, e.g.
|
|
131
|
+
// "0A10:1". Owner paths name a component by its position in the component tree
|
|
132
|
+
// counted from the render root, so they do not depend on the order in which
|
|
133
|
+
// asynchronous work finished. That is what lets a streamed boundary that
|
|
134
|
+
// resolves late still produce the key the client computes in document order.
|
|
135
|
+
//
|
|
136
|
+
// The id format is adapted from Solid's formatId
|
|
137
|
+
// (packages/signals/src/core/owner.ts; MIT, Copyright (c) 2016-2026 Ryan
|
|
138
|
+
// Carniato): each step is the child index in base 36, prefixed with a letter
|
|
139
|
+
// giving its extra length when it needs more than one digit, so a path is
|
|
140
|
+
// unambiguous without separators.
|
|
141
|
+
|
|
142
|
+
function formatId(prefix, index) {
|
|
143
|
+
const digits = index.toString(36);
|
|
144
|
+
const extra = digits.length - 1;
|
|
145
|
+
return prefix + (extra ? String.fromCharCode(64 + extra) : '') + digits;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The step formatId wrote at `at` in `path`: its end and its index, or null.
|
|
149
|
+
function stepAt(path, at) {
|
|
150
|
+
const c = path.charCodeAt(at);
|
|
151
|
+
const extra = c >= 65 && c <= 90 ? c - 64 : 0;
|
|
152
|
+
const start = extra ? at + 1 : at;
|
|
153
|
+
const end = start + extra + 1;
|
|
154
|
+
if (end > path.length) return null;
|
|
155
|
+
const index = parseInt(path.slice(start, end), 36);
|
|
156
|
+
return Number.isNaN(index) ? null : { end, index };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The built-ins what-compiler lowers away (<Show>, <For>, <Switch>,
|
|
160
|
+
// <Match>: compiled JSX renders them as a conditional or a list, with no
|
|
161
|
+
// component of their own) take no step either when they run as components
|
|
162
|
+
// (h(), the JSX runtime), so a subtree has the same owner paths whichever way
|
|
163
|
+
// each side rendered it. Such a frame numbers its children as its parent's.
|
|
164
|
+
function holderOf(frame, root) {
|
|
165
|
+
return frame === null ? root : frame._ownerHolder || frame;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The object that counts the children of `frame` (null: the root's). @internal */
|
|
169
|
+
export function _ownerCounter(frame, root) {
|
|
170
|
+
return holderOf(frame, root);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Give a component frame its owner path. `parent` is the frame it renders
|
|
175
|
+
* under (null at the root); `root` is the object that counts root-level
|
|
176
|
+
* components (the render context on the server). Server renderers call this
|
|
177
|
+
* once per frame, in creation order. Returns whether the frame took a step
|
|
178
|
+
* (false for a built-in the compiler lowers away), and so has a hydration key
|
|
179
|
+
* of its own.
|
|
180
|
+
*
|
|
181
|
+
* Once an app defines a server action, it also gives the frame its place
|
|
182
|
+
* among the keyed rows (placeInRow, below); `site` is the number
|
|
183
|
+
* what-compiler gave the JSX that renders it, if any.
|
|
184
|
+
*/
|
|
185
|
+
export function _assignOwnerPath(frame, parent, root, site) {
|
|
186
|
+
const holder = holderOf(parent, root);
|
|
187
|
+
const prefix = parent ? parent._ownerPath || '' : '';
|
|
188
|
+
if (frame.Component !== undefined && frame.Component._lowered === true) {
|
|
189
|
+
frame._ownerPath = prefix;
|
|
190
|
+
frame._ownerHolder = holder;
|
|
191
|
+
if (placeRow !== null) placeRow(frame, parent, null, site);
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const index = holder._ownerChildren || 0;
|
|
195
|
+
holder._ownerChildren = index + 1;
|
|
196
|
+
frame._ownerPath = formatId(prefix, index);
|
|
197
|
+
if (placeRow !== null) placeRow(frame, parent, holder, site);
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The owner path of the component frame running now, or undefined outside one. */
|
|
202
|
+
export function _currentOwnerPath() {
|
|
203
|
+
const frame = getCurrentComponent();
|
|
204
|
+
return frame ? frame._ownerPath : undefined;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The owner key for the next keyless resource created in `frame` (or at the root). */
|
|
208
|
+
export function _nextOwnerKey(frame, root) {
|
|
209
|
+
const holder = holderOf(frame || null, root);
|
|
210
|
+
const index = holder._ownerResources || 0;
|
|
211
|
+
holder._ownerResources = index + 1;
|
|
212
|
+
return `${frame ? frame._ownerPath || '' : ''}:${index}`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// --- Keyed rows ---
|
|
216
|
+
//
|
|
217
|
+
// A useActionState of a server action has an identity its <Form> posts, and
|
|
218
|
+
// a form posted without JavaScript comes back to the instance of that
|
|
219
|
+
// identity alone. A render that answers the post is another render, of data
|
|
220
|
+
// that may have changed since (a teammate added a record above), so inside a
|
|
221
|
+
// keyed list the identity follows the row's key, as React's key path names
|
|
222
|
+
// an element: the keys of the keyed rows around the instance, outermost
|
|
223
|
+
// first, each with its list's place in the component that renders it and
|
|
224
|
+
// how many rows of that list had the same key before it, then the steps from
|
|
225
|
+
// the innermost row down to the instance's component, and its place among
|
|
226
|
+
// that component's. Outside keyed lists the steps start at the root.
|
|
227
|
+
//
|
|
228
|
+
// A keyed row is a member of an array (a reactive child's, a component's, a
|
|
229
|
+
// keyed list's rows) or of an element's children whose key is a string or a
|
|
230
|
+
// finite number: a `.map()` row with a `key`, a <For key> row, a vnode with
|
|
231
|
+
// a key. A key of any other kind (an object, a boolean, NaN) has no stable
|
|
232
|
+
// form from one render to the next, so its row is not a keyed row and the
|
|
233
|
+
// components in it count by position. The renderers enter each row as they
|
|
234
|
+
// reach it (_enterRow, _leaveRow), on the server and while hydrating alike,
|
|
235
|
+
// on the object that counts the components of the frame rendering it.
|
|
236
|
+
//
|
|
237
|
+
// A component's step, like a list's place, is where it stands, never how many
|
|
238
|
+
// components or lists came before it, so a component or a list that is in one
|
|
239
|
+
// render and not in the next moves no other: the child slots from the
|
|
240
|
+
// innermost row (or the frame's output) down to it, as the vnode renderers
|
|
241
|
+
// walk them (each member of an element's children or of an array that can
|
|
242
|
+
// hold a component or a list enters its index there: takesSlot in render.js
|
|
243
|
+
// and in what-server, _enterRow), then the number what-compiler gives the JSX
|
|
244
|
+
// that renders it, from where that is written in its source. A conditional
|
|
245
|
+
// child keeps its
|
|
246
|
+
// slot when it renders nothing, as an h() function child does (h() drops a
|
|
247
|
+
// `false` or `null` child, which so holds no slot). What still shares a place
|
|
248
|
+
// (one JSX site run more than once in a frame: a helper called twice, the
|
|
249
|
+
// rows of an unkeyed compiled list) is told apart by the order it takes the
|
|
250
|
+
// place in; a keyed list takes its place even when it is empty.
|
|
251
|
+
//
|
|
252
|
+
// The page carries no key: a key is written as its digest under the render's
|
|
253
|
+
// salt (_rowKeyDigest, as a keyed list's keys comment writes it), and the
|
|
254
|
+
// identity begins with that salt. The render that answers a post compares
|
|
255
|
+
// under the posted identity's salt, and the hydration of a page computes its
|
|
256
|
+
// identities under the page's, which the server writes into the page's data
|
|
257
|
+
// (what-server actions.js).
|
|
258
|
+
|
|
259
|
+
const isRowKey = (key) => typeof key === 'string' || (typeof key === 'number' && Number.isFinite(key));
|
|
260
|
+
|
|
261
|
+
// Installed with the keyed rows (_installRowKeys): placeInRow, and readRootRow
|
|
262
|
+
// for a hydration root. An app with no server action carries neither.
|
|
263
|
+
/** @type {((frame: any, parent: any, holder: any, site: number | undefined) => void) | null} */
|
|
264
|
+
let placeRow = null;
|
|
265
|
+
/** @type {((root: any, container: any) => void) | null} */
|
|
266
|
+
let rootRow = null;
|
|
267
|
+
|
|
268
|
+
// A frame's place among the keyed rows: `_rowIn`, the innermost keyed row
|
|
269
|
+
// around it, and `_rowPath`, its steps counted from that row; outside keyed
|
|
270
|
+
// rows, `_placePath`, its steps from the root. Its step is its place in the
|
|
271
|
+
// output of the frame rendering it (`holder`), from the start of the keyed
|
|
272
|
+
// row open there or of the output (placeOf), ending at `X`. A built-in the
|
|
273
|
+
// compiler lowers away (no `holder`) takes no step.
|
|
274
|
+
function placeInRow(frame, parent, holder, site) {
|
|
275
|
+
const open = holder === null ? undefined : holder._row;
|
|
276
|
+
const row = rowOf(open);
|
|
277
|
+
// An island's content takes the place the root of the client's hydrate()
|
|
278
|
+
// into the island takes (_currentRowPath): no slot, no compiled number.
|
|
279
|
+
const step = holder === null ? '' : holder._islandHost === true ? placeOf(undefined, holder, undefined, 'X', frame._ownerPath)
|
|
280
|
+
: placeOf(open, row !== undefined ? row : holder, site, 'X', frame._ownerPath);
|
|
281
|
+
if (row !== undefined) {
|
|
282
|
+
frame._rowIn = row;
|
|
283
|
+
frame._rowPath = step;
|
|
284
|
+
} else if (parent && parent._rowIn !== undefined) {
|
|
285
|
+
frame._rowIn = parent._rowIn;
|
|
286
|
+
frame._rowPath = parent._rowPath + step;
|
|
287
|
+
} else {
|
|
288
|
+
frame._placePath = (parent ? parent._placePath || '' : '') + step;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// A frame's steps from the root outside keyed rows (placeInRow), or its
|
|
293
|
+
// owner path where it has none (no server action, an unanchored frame).
|
|
294
|
+
function stepsOf(frame) {
|
|
295
|
+
const path = frame._ownerPath || '';
|
|
296
|
+
return frame._placePath !== undefined && path[0] !== UNANCHORED ? frame._placePath : path;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// An island's root starts from the place its server wrote (_currentRowPath):
|
|
300
|
+
// the keyed rows around it, or outside them its steps from the root.
|
|
301
|
+
function readRootRow(root, container) {
|
|
302
|
+
const row = root._ownerPath[0] !== UNANCHORED ? container.getAttribute('data-what-row') : null;
|
|
303
|
+
if (row === null || !ROW_PATH.test(row)) return;
|
|
304
|
+
if (row.includes('-')) {
|
|
305
|
+
root._rowIn = { written: row };
|
|
306
|
+
root._rowPath = '';
|
|
307
|
+
} else {
|
|
308
|
+
root._placePath = row;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// What an island's data-what-row holds: digests, steps and the row marks
|
|
313
|
+
// between them (a row's lead ends at `-`, which no step writes).
|
|
314
|
+
const ROW_PATH = /^[0-9A-Za-z_.-]+$/;
|
|
315
|
+
|
|
316
|
+
// The innermost keyed row of what is open in a frame's output (`open`: the
|
|
317
|
+
// slot or row entered last, each slot holding the row around it).
|
|
318
|
+
const rowOf = (open) => (open !== undefined && open.slot !== undefined ? open.row : open);
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* @internal A keyed row with key `key` of the list `list` begins in the
|
|
322
|
+
* output of `frame` (undefined: the component running now; null: the root,
|
|
323
|
+
* counted by `root`). `list` is an object the caller makes for one array or
|
|
324
|
+
* one element's children (with `slot`, the number what-compiler gave a list
|
|
325
|
+
* it lowered), and passes for each of its members with a key: the first
|
|
326
|
+
* gives the list its place (Keyed rows above), and each the count of the rows
|
|
327
|
+
* of the same key before it. A caller that enters only some of its rows
|
|
328
|
+
* (compiled markup enters the rows it renders components in) gives the list
|
|
329
|
+
* its rows' keys as `list.keys`, enters it once before the first row with no
|
|
330
|
+
* key, and passes each row's place in `keys` as `index`; a compiled list
|
|
331
|
+
* enters so even when it has no rows, so it takes its place. Returns what
|
|
332
|
+
* _leaveRow puts back once the row is done. A key that is not a row key
|
|
333
|
+
* (above) enters nothing, and the result is what is open there now. A null
|
|
334
|
+
* `list` is a member that is no keyed row: with a number `key`, its index in
|
|
335
|
+
* the element's children or the array being walked, it enters that slot (a
|
|
336
|
+
* member that can hold a component or a list, takesSlot, holds them below it
|
|
337
|
+
* there); with none, it enters nothing.
|
|
338
|
+
*/
|
|
339
|
+
export function _enterRow(frame, root, list, key, index) {
|
|
340
|
+
const holder = holderOf(frame === undefined ? _currentComponent() : frame, root);
|
|
341
|
+
const open = holder._row;
|
|
342
|
+
if (list === null) {
|
|
343
|
+
if (typeof key === 'number') holder._row = { up: open, slot: key, row: rowOf(open) };
|
|
344
|
+
return open;
|
|
345
|
+
}
|
|
346
|
+
const outer = rowOf(open);
|
|
347
|
+
list.place ??= placeOf(open, outer !== undefined ? outer : holder, list.slot, '', list);
|
|
348
|
+
if (!isRowKey(key)) return open;
|
|
349
|
+
let n;
|
|
350
|
+
if (list.keys !== undefined) n = (list.counts ??= keyCounts(list.keys))[/** @type {number} */ (index)];
|
|
351
|
+
else {
|
|
352
|
+
const seen = list.seen ??= new Map();
|
|
353
|
+
n = seen.get(key) || 0;
|
|
354
|
+
seen.set(key, n + 1);
|
|
355
|
+
}
|
|
356
|
+
let up = outer;
|
|
357
|
+
let lead = '';
|
|
358
|
+
if (outer === undefined) {
|
|
359
|
+
up = holder._rowIn !== undefined ? holder._rowIn : null;
|
|
360
|
+
lead = up !== null ? holder._rowPath : stepsOf(holder);
|
|
361
|
+
}
|
|
362
|
+
holder._row = { up, lead, list: list.place, key, n };
|
|
363
|
+
return open;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// A place in `scope` (the innermost row, or the frame): the slots open from
|
|
367
|
+
// its start down to `open`, the compiled `number`, then `end` (`X` ends a
|
|
368
|
+
// component's step; a list's place is followed by its rows' keys). What
|
|
369
|
+
// takes a place another took before it in `scope` adds how many did; `who`
|
|
370
|
+
// taking it again (a component's retry, with its owner path) keeps its count.
|
|
371
|
+
// Each slot and number is formatId's (self-delimiting); `Z`, `Y` and `X`,
|
|
372
|
+
// which begin no step formatId writes, mark the number, the count and the end.
|
|
373
|
+
function placeOf(open, scope, number, end, who) {
|
|
374
|
+
let place = '';
|
|
375
|
+
for (let s = open; s !== undefined && s.slot !== undefined; s = s.up) place = formatId('', s.slot) + place;
|
|
376
|
+
if (number != null) place += 'Z' + formatId('', number);
|
|
377
|
+
place += end;
|
|
378
|
+
const taken = scope._places ??= new Map();
|
|
379
|
+
const seen = taken.get(place);
|
|
380
|
+
let before = 0;
|
|
381
|
+
if (seen === undefined) taken.set(place, [1, who]);
|
|
382
|
+
else if (seen[1] === who) before = seen[0] - 1;
|
|
383
|
+
else {
|
|
384
|
+
before = seen[0]++;
|
|
385
|
+
seen[1] = who;
|
|
386
|
+
}
|
|
387
|
+
return before === 0 ? place : place + 'Y' + formatId('', before);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// For each key of `keys`, how many before it are the same row key. A Map
|
|
391
|
+
// tells keys apart as SameValueZero does: a number from its string form, and
|
|
392
|
+
// 0 from nothing but -0, which is the same row key.
|
|
393
|
+
function keyCounts(keys) {
|
|
394
|
+
const seen = new Map();
|
|
395
|
+
const counts = [];
|
|
396
|
+
for (let i = 0; i < keys.length; i++) {
|
|
397
|
+
const key = keys[i];
|
|
398
|
+
if (!isRowKey(key)) {
|
|
399
|
+
counts.push(0);
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const n = seen.get(key) || 0;
|
|
403
|
+
seen.set(key, n + 1);
|
|
404
|
+
counts.push(n);
|
|
405
|
+
}
|
|
406
|
+
return counts;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** @internal The row _enterRow entered in `frame`'s output is done: `saved` is what it returned. */
|
|
410
|
+
export function _leaveRow(frame, root, saved) {
|
|
411
|
+
holderOf(frame === undefined ? _currentComponent() : frame, root)._row = saved;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* @internal An app defines a server action: hydration enters keyed rows from
|
|
416
|
+
* now on, as the server renderers always do, so its useActionState keys are
|
|
417
|
+
* the server's (what-server action()).
|
|
418
|
+
*/
|
|
419
|
+
export function _installRowKeys() {
|
|
420
|
+
placeRow = placeInRow;
|
|
421
|
+
rootRow = readRootRow;
|
|
422
|
+
_installRowEntry(_enterRow, _leaveRow);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** @internal Whether keyed rows are installed (_installRowKeys): the server renderers enter slots (_enterRow) only then. */
|
|
426
|
+
export function _tracksRows() {
|
|
427
|
+
return placeRow !== null;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// `row` and the rows around it, under `salt`. An island's root starts from
|
|
431
|
+
// the path its server wrote (_hydrationRoot). A list's place ends at `.`,
|
|
432
|
+
// which no step writes, before the key's fixed-length digest.
|
|
433
|
+
function rowPath(row, salt) {
|
|
434
|
+
let path = '';
|
|
435
|
+
for (let r = row; r !== null; r = r.up) {
|
|
436
|
+
if (r.written !== undefined) return r.written + path;
|
|
437
|
+
path = r.lead + '-' + r.list + '.' + _rowKeyDigest(r.key, salt) + formatId('', r.n) + path;
|
|
438
|
+
}
|
|
439
|
+
return path;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* @internal The component rendering now is an island's wrapper on the
|
|
444
|
+
* server: the client hydrates the island's content as the root of a hydrate()
|
|
445
|
+
* call of its own, so what the wrapper renders takes the place that root's
|
|
446
|
+
* component takes there, with no slot and no compiled number (placeInRow).
|
|
447
|
+
* what-server's Island (through _currentRowPath) and compiled JSX's
|
|
448
|
+
* (components.js _$Island) both are.
|
|
449
|
+
*/
|
|
450
|
+
export function _islandHost() {
|
|
451
|
+
const frame = _currentComponent();
|
|
452
|
+
if (frame !== null) frame._islandHost = true;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* @internal The place of the component rendering now: the keyed rows around
|
|
457
|
+
* it and its steps below the innermost, under `salt`, or outside keyed rows
|
|
458
|
+
* its steps from the root; null when no server action placed it. An island
|
|
459
|
+
* writes it as data-what-row, for the root it hydrates into to start from,
|
|
460
|
+
* and the component asking is that island's wrapper (_islandHost).
|
|
461
|
+
*/
|
|
462
|
+
export function _currentRowPath(salt) {
|
|
463
|
+
const frame = _currentComponent();
|
|
464
|
+
if (frame === null || (frame._rowIn === undefined && frame._placePath === undefined)) return null;
|
|
465
|
+
frame._islandHost = true;
|
|
466
|
+
return frame._rowIn !== undefined ? rowPath(frame._rowIn, salt) + frame._rowPath : frame._placePath;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const SALT = /^[0-9A-Za-z_.]{11}$/;
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* @internal The salt the render running now writes its keyed rows' digests
|
|
473
|
+
* under: one per render context, a fresh one outside a render.
|
|
474
|
+
*/
|
|
475
|
+
export function _renderSalt() {
|
|
476
|
+
const ctx = getServerContext();
|
|
477
|
+
if (ctx === null) return _rowKeySalt();
|
|
478
|
+
let salt = salts.get(ctx);
|
|
479
|
+
if (salt === undefined) salts.set(ctx, salt = _rowKeySalt());
|
|
480
|
+
return salt;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const salts = new WeakMap();
|
|
484
|
+
|
|
485
|
+
/** @internal The salt an action-state key (_actionStateKey) was made under, or null for one made under none. */
|
|
486
|
+
export function _actionKeySalt(key) {
|
|
487
|
+
if (typeof key !== 'string' || key.charCodeAt(11) !== 47) return null;
|
|
488
|
+
const salt = key.slice(0, 11);
|
|
489
|
+
return SALT.test(salt) ? salt : null;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @internal The identity of the server action's useActionState being made
|
|
494
|
+
* now (Keyed rows above), as a function of the salt it is written under:
|
|
495
|
+
* `key(salt)`. `key.salted` says whether it has keyed rows in it, and so
|
|
496
|
+
* whether the salt matters; a key without one is its component's owner path
|
|
497
|
+
* and its place among that component's. The same on the server and while
|
|
498
|
+
* hydrating what the server rendered; under a salt that is not one, a key no
|
|
499
|
+
* server render makes. Null in any other client render, or outside a
|
|
500
|
+
* component.
|
|
501
|
+
*/
|
|
502
|
+
export function _actionStateKey() {
|
|
503
|
+
const frame = _currentComponent();
|
|
504
|
+
if (frame === null || frame._ownerPath === undefined || !(isServerRender() || isHydrating())) return null;
|
|
505
|
+
const holder = holderOf(frame, null);
|
|
506
|
+
const index = holder._ownerActions || 0;
|
|
507
|
+
holder._ownerActions = index + 1;
|
|
508
|
+
const positional = `${stepsOf(frame)}:${index}`;
|
|
509
|
+
const row = frame._rowIn;
|
|
510
|
+
if (row === undefined || positional[0] === UNANCHORED) {
|
|
511
|
+
const key = () => positional;
|
|
512
|
+
key.salted = false;
|
|
513
|
+
return key;
|
|
514
|
+
}
|
|
515
|
+
const below = `${frame._rowPath}:${index}`;
|
|
516
|
+
const key = (salt) => (typeof salt === 'string' && SALT.test(salt) ? `${salt}/${rowPath(row, salt)}${below}` : UNANCHORED + positional);
|
|
517
|
+
key.salted = true;
|
|
518
|
+
return key;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// --- Adopting keyless values while hydrating ---
|
|
522
|
+
//
|
|
523
|
+
// The client numbers its component contexts the way the server numbered its
|
|
524
|
+
// frames, and a keyless resource looks its own owner key up. Nothing is taken
|
|
525
|
+
// by position, so a component the other side does not have (a <Portal>'s
|
|
526
|
+
// content, a client-only widget, a branch that differs) can only miss, never
|
|
527
|
+
// hand its value to a neighbour.
|
|
528
|
+
//
|
|
529
|
+
// A component whose output starts with an element the server keyed takes its
|
|
530
|
+
// path from that key (the server put the innermost component's path there,
|
|
531
|
+
// and this component's is the step of it right after its parent's), so it
|
|
532
|
+
// finds its own values wherever the two sides' trees diverged before it: a
|
|
533
|
+
// branch only one side rendered, the children a boundary tried before its
|
|
534
|
+
// fallback. Its parent then counts on from there. A component with no key in
|
|
535
|
+
// front of it is numbered in order, which holds only while the walk has
|
|
536
|
+
// matched the server's structure: once it has not (a mismatch in the
|
|
537
|
+
// structure: an element of another tag, one missing or left over, a key that
|
|
538
|
+
// differs), or where the server rendered nothing at all, it is numbered under
|
|
539
|
+
// a prefix no server key has, so its resources fetch.
|
|
540
|
+
//
|
|
541
|
+
// Numbering starts from the right place only for a hydrate() root that is the
|
|
542
|
+
// server's render root, or that names the server owner path it starts at:
|
|
543
|
+
//
|
|
544
|
+
// - a container whose direct children include the page's hydration data
|
|
545
|
+
// element is where the server's render went (renderDocument writes the
|
|
546
|
+
// payload into <body> after the page, a stream right after its shell), so
|
|
547
|
+
// it counts from the root;
|
|
548
|
+
// - a container with data-what-owner="PATH" (an island) counts from PATH;
|
|
549
|
+
// - any other container (a second hydrate() root covering part of the page)
|
|
550
|
+
// is numbered under a prefix no server key has, so its resources fetch.
|
|
551
|
+
//
|
|
552
|
+
// Position alone cannot tell two components apart when one side renders a
|
|
553
|
+
// component the other does not have in the same place. In development a value
|
|
554
|
+
// is adopted only when the server's component at that owner path has the
|
|
555
|
+
// client's component's name too (`owners`); otherwise the resource fetches and
|
|
556
|
+
// ERR_HYDRATION_OWNER_MISMATCH names both. A production client adopts by owner
|
|
557
|
+
// path alone: its bundle renames functions (minification), so no name could
|
|
558
|
+
// match, and a tree that renders differently on the two sides is the
|
|
559
|
+
// development check's to find, as hydration mismatches are. A resource given a
|
|
560
|
+
// `key` is adopted by that key everywhere.
|
|
561
|
+
|
|
562
|
+
const UNANCHORED = '~';
|
|
563
|
+
const OWNER_PATH = /^[0-9A-Za-z]*$/;
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* The counter for the root-level components of one hydrate() call into
|
|
567
|
+
* `container`: each call counts from the container's anchor again, and work
|
|
568
|
+
* of that call that runs later (a boundary resuming) counts on it.
|
|
569
|
+
* @internal
|
|
570
|
+
*/
|
|
571
|
+
export function _hydrationRoot(container) {
|
|
572
|
+
const root = { _ownerPath: anchorPath(container), _ownerChildren: 0 };
|
|
573
|
+
if (rootRow !== null) rootRow(root, container);
|
|
574
|
+
return root;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function anchorPath(container) {
|
|
578
|
+
if (!container || container.nodeType !== 1) return UNANCHORED;
|
|
579
|
+
const owner = container.getAttribute('data-what-owner');
|
|
580
|
+
if (owner !== null) return OWNER_PATH.test(owner) ? owner : UNANCHORED;
|
|
581
|
+
for (let el = container.lastElementChild; el; el = el.previousElementSibling) {
|
|
582
|
+
if (isDataElement(el)) return '';
|
|
583
|
+
}
|
|
584
|
+
return UNANCHORED;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Give a client component context its owner path while hydrating; outside a
|
|
589
|
+
* hydration it does nothing. The hydration walk calls it once per component
|
|
590
|
+
* context, right after creating it, with its hydrate() call's root counter
|
|
591
|
+
* (_hydrationRoot) and what the server has where the component's output
|
|
592
|
+
* starts: the key of the element there (a string, '' for an element or text
|
|
593
|
+
* with none), or null when the server rendered nothing more there. `ordered`
|
|
594
|
+
* says whether the walk has matched the server's structure so far, and `site`
|
|
595
|
+
* is the compiled JSX's number (_assignOwnerPath). Returns 0
|
|
596
|
+
* for a built-in that takes no step (see _assignOwnerPath), 2 when the
|
|
597
|
+
* server's key there cannot be this component's (it does not extend the
|
|
598
|
+
* parent's path), else 1.
|
|
599
|
+
*
|
|
600
|
+
* The parent is the nearest component owner above it (boundaries, effects
|
|
601
|
+
* and list rows are owners without a `Component` and are skipped). A
|
|
602
|
+
* component built fresh while hydrating (a compiled component, a mismatch's
|
|
603
|
+
* client render) has no owner path, so nothing under it is numbered from the
|
|
604
|
+
* server's: it fetches.
|
|
605
|
+
*/
|
|
606
|
+
export function _assignHydrationOwnerPath(ctx, root, key, ordered, site) {
|
|
607
|
+
if (!isHydrating()) return 1;
|
|
608
|
+
let parent = ctx.owner;
|
|
609
|
+
while (parent && parent.Component === undefined) parent = parent.owner;
|
|
610
|
+
if (parent && parent._ownerPath === undefined) {
|
|
611
|
+
ctx._ownerPath = UNANCHORED;
|
|
612
|
+
return 1;
|
|
613
|
+
}
|
|
614
|
+
const frame = parent || root;
|
|
615
|
+
if (!_assignOwnerPath(ctx, frame, null, site)) return 0;
|
|
616
|
+
const prefix = frame._ownerPath;
|
|
617
|
+
// Under a prefix no server key has, nothing is the server's.
|
|
618
|
+
if (prefix[0] === UNANCHORED) return 1;
|
|
619
|
+
const step = key && key.startsWith(prefix) ? stepAt(key, prefix.length) : null;
|
|
620
|
+
if (step !== null) {
|
|
621
|
+
ctx._ownerPath = key.slice(0, step.end);
|
|
622
|
+
holderOf(frame, null)._ownerChildren = step.index + 1;
|
|
623
|
+
return 1;
|
|
624
|
+
}
|
|
625
|
+
if (key !== '' || !ordered) ctx._ownerPath = UNANCHORED + ctx._ownerPath;
|
|
626
|
+
return key ? 2 : 1;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function componentName(frame) {
|
|
630
|
+
return (frame.Component && frame.Component.name) || '';
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* While hydrating: the server's result for the next keyless resource of
|
|
635
|
+
* `frame`, `{ value }` or `{ error }`, or undefined when there is none to
|
|
636
|
+
* adopt. Every keyless resource calls this, adopted or not, so the next one in
|
|
637
|
+
* the same component gets the next key.
|
|
638
|
+
*/
|
|
639
|
+
export function _adoptOwnedValue(frame) {
|
|
640
|
+
if (!frame || frame._ownerPath === undefined || !isHydrating()) return undefined;
|
|
641
|
+
const key = _nextOwnerKey(frame, null);
|
|
642
|
+
const data = __readHydrationData();
|
|
643
|
+
if (!data) return undefined;
|
|
644
|
+
const failed = !!data.ownedErrors && Object.hasOwn(data.ownedErrors, key);
|
|
645
|
+
if (!failed && !(data.owned && Object.hasOwn(data.owned, key))) return undefined;
|
|
646
|
+
if (__DEV__) {
|
|
647
|
+
const path = frame._ownerPath;
|
|
648
|
+
const server = data.owners && Object.hasOwn(data.owners, path) ? data.owners[path] : null;
|
|
649
|
+
const client = componentName(frame);
|
|
650
|
+
if (server !== client) {
|
|
651
|
+
report('ERR_HYDRATION_OWNER_MISMATCH', { data: { path, server: String(server), client } });
|
|
652
|
+
return undefined;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return failed ? { error: data.ownedErrors[key] } : { value: data.owned[key] };
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// --- Async values in the channel ---
|
|
659
|
+
//
|
|
660
|
+
// An async value (computed(), createResource) asks where it starts from when it
|
|
661
|
+
// is made. Only a server render or a hydration can answer anything but
|
|
662
|
+
// "nowhere", so the answer is installed by the first of either to start
|
|
663
|
+
// (server-context.js, render.js hydrate()): an app that does neither does not
|
|
664
|
+
// carry it. What starts a value from the server's (reactive.js _installSeedOps)
|
|
665
|
+
// is installed by the first seed a hydration hands out, so an app that
|
|
666
|
+
// hydrates and makes no async value does not carry that either.
|
|
667
|
+
|
|
668
|
+
/** @type {typeof seedFor | null} */
|
|
669
|
+
let seeder = null;
|
|
670
|
+
|
|
671
|
+
/** @internal A hydration is starting: async values take part in the channel. */
|
|
672
|
+
export function _installSeeding() {
|
|
673
|
+
seeder = seedFor;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** @internal A server render is starting: async values take part in the channel, through its resource store. */
|
|
677
|
+
export function _installServerSeeding() {
|
|
678
|
+
seeder = seedFor;
|
|
679
|
+
_installStoreSeedOps();
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* @internal Where an async value made now starts from (see seedFor), or null.
|
|
684
|
+
* @param {unknown} key
|
|
685
|
+
* @param {boolean} track whether an adopted value's function runs once to learn what it reads
|
|
686
|
+
*/
|
|
687
|
+
export function _seedFor(key, track) {
|
|
688
|
+
if (seeder !== null) return seeder(key, track);
|
|
689
|
+
// A server render another copy of what-core runs (this one is a library's
|
|
690
|
+
// own engine) refuses the value: the store of a render that started says
|
|
691
|
+
// so (server-context.js `refuse`).
|
|
692
|
+
/** @type {any} */ (globalThis)[_serverStoreKey]?.refuse?.();
|
|
693
|
+
return null;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// In a server render: the render's resource store, under `key` or, without
|
|
697
|
+
// one, the owner key of the component being rendered, so a retried render
|
|
698
|
+
// finds the run, and the client finds the value. While hydrating: the value
|
|
699
|
+
// (or error) the server rendered with, under the same owner key. Otherwise
|
|
700
|
+
// nothing. Every keyless value made in a component consumes one owner key on
|
|
701
|
+
// both sides, so the numbering matches.
|
|
702
|
+
function seedFor(key, track) {
|
|
703
|
+
const keyed = key != null;
|
|
704
|
+
if (isServerRender()) {
|
|
705
|
+
const ctx = getServerContext();
|
|
706
|
+
if (ctx === null) return null;
|
|
707
|
+
const frame = _currentComponent();
|
|
708
|
+
return {
|
|
709
|
+
store: keyed ? ctx.resources : ctx.owned,
|
|
710
|
+
key: keyed ? key : _nextOwnerKey(frame, ctx),
|
|
711
|
+
owner: keyed ? undefined : (frame && frame.Component && frame.Component.name) || '',
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
if (keyed || !isHydrating()) return null;
|
|
715
|
+
const taken = _adoptOwnedValue(_currentComponent());
|
|
716
|
+
if (taken === undefined) return null;
|
|
717
|
+
_installSeedOps();
|
|
718
|
+
return 'error' in taken ? { error: _errorFromRecord(taken.error), track } : { value: taken.value, track };
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* @internal While hydrating: the error the server's <ErrorBoundary> rendered
|
|
723
|
+
* by `frame` showed its fallback for, when that was a value that failed
|
|
724
|
+
* (`caught`), or undefined.
|
|
725
|
+
*/
|
|
726
|
+
export function _caughtError(frame) {
|
|
727
|
+
if (!frame || typeof frame._ownerPath !== 'string' || frame._ownerPath[0] === UNANCHORED) return undefined;
|
|
728
|
+
const record = lookup('caught', frame._ownerPath);
|
|
729
|
+
return record === undefined ? undefined : _errorFromRecord(record);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/** @internal A server error record as the Error the client exposes. */
|
|
733
|
+
export function _errorFromRecord(record) {
|
|
734
|
+
const error = new Error(record && record.message ? record.message : 'Server error');
|
|
735
|
+
if (record && record.code) /** @type {any} */ (error).code = record.code;
|
|
736
|
+
return error;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
// --- Streamed boundaries ---
|
|
740
|
+
|
|
741
|
+
function endMarker(placeholder) {
|
|
742
|
+
const end = placeholder.id;
|
|
743
|
+
for (let node = placeholder.nextSibling; node; node = node.nextSibling) {
|
|
744
|
+
if (node.nodeType === 8 && node.data === end) return node;
|
|
745
|
+
}
|
|
746
|
+
return null;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
function swapBoundary(placeholder, content, end) {
|
|
750
|
+
for (let node = placeholder.nextSibling; node !== end;) {
|
|
751
|
+
const next = node.nextSibling;
|
|
752
|
+
node.remove();
|
|
753
|
+
node = next;
|
|
754
|
+
}
|
|
755
|
+
end.remove();
|
|
756
|
+
placeholder.replaceWith(content.content);
|
|
757
|
+
content.remove();
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Swap in the content of the streamed boundary `placeholder` stands for when
|
|
762
|
+
* the content is on the page but its swap has not run: a page whose inline
|
|
763
|
+
* scripts did not run, or hydrate() reaching the boundary first. Returns
|
|
764
|
+
* whether it did.
|
|
765
|
+
*/
|
|
766
|
+
export function _swapArrived(placeholder) {
|
|
767
|
+
const id = placeholder.id.slice(3);
|
|
768
|
+
if (!BOUNDARY_ID.test(id)) return false;
|
|
769
|
+
const content = document.querySelector(`template[id="${id}"]`);
|
|
770
|
+
const end = content && endMarker(placeholder);
|
|
771
|
+
if (!end) return false;
|
|
772
|
+
swapBoundary(placeholder, content, end);
|
|
32
773
|
const data = __readHydrationData();
|
|
33
|
-
|
|
774
|
+
if (data && data.head && Object.hasOwn(data.head, id)) applyLateHead(data.head[id]);
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const SAFE_HEAD_ATTR = /^[a-zA-Z_:][a-zA-Z0-9:._-]*$/;
|
|
779
|
+
|
|
780
|
+
// What what-server's boundary ids are made of (stream.js), and so what may go
|
|
781
|
+
// into a selector unescaped.
|
|
782
|
+
const BOUNDARY_ID = /^[A-Za-z][\w-]*$/;
|
|
783
|
+
|
|
784
|
+
function headElement(tag, attrs) {
|
|
785
|
+
const el = document.createElement(tag);
|
|
786
|
+
for (const key in attrs) {
|
|
787
|
+
const value = attrs[key];
|
|
788
|
+
if (value == null || value === false) continue;
|
|
789
|
+
const name = key === 'httpEquiv' ? 'http-equiv' : key;
|
|
790
|
+
if (SAFE_HEAD_ATTR.test(name) && !/^on./i.test(name)) el.setAttribute(name, String(value));
|
|
791
|
+
}
|
|
792
|
+
return el;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function putHeadElement(el, matches) {
|
|
796
|
+
const head = document.head;
|
|
797
|
+
if (matches) {
|
|
798
|
+
for (let node = head.firstChild; node; node = node.nextSibling) {
|
|
799
|
+
if (node.nodeType === 1 && /** @type {Element} */ (node).tagName === el.tagName && matches(node)) {
|
|
800
|
+
node.replaceWith(el);
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
head.appendChild(el);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Apply the <Head> tags a streamed boundary set after the shell: the title,
|
|
810
|
+
* and each meta or link, replacing the head element with the same dedup key as
|
|
811
|
+
* head.js uses. Kept in step with HEAD_FUNCTION in what-server's stream.js.
|
|
812
|
+
*/
|
|
813
|
+
function applyLateHead(head) {
|
|
814
|
+
if (head.title != null) document.title = head.title;
|
|
815
|
+
for (const attrs of head.meta || []) {
|
|
816
|
+
const by = attrs.name ? 'name' : attrs.property ? 'property' : attrs.httpEquiv ? 'http-equiv' : null;
|
|
817
|
+
const value = String(attrs.name || attrs.property || attrs.httpEquiv);
|
|
818
|
+
putHeadElement(headElement('meta', attrs), by && ((node) => node.getAttribute(by) === value));
|
|
819
|
+
}
|
|
820
|
+
for (const attrs of head.link || []) {
|
|
821
|
+
const href = attrs.href == null ? null : String(attrs.href);
|
|
822
|
+
putHeadElement(headElement('link', attrs),
|
|
823
|
+
(node) => node.getAttribute('rel') === String(attrs.rel) && node.getAttribute('href') === href);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Settle every streamed boundary on the page, the work the inline scripts do
|
|
829
|
+
* on a normal stream. A page streamed with what-server's `noScripts` option
|
|
830
|
+
* carries no inline scripts, so its client entry calls this, before hydrating
|
|
831
|
+
* or after; on any streamed page it is safe to call once the document has
|
|
832
|
+
* loaded.
|
|
833
|
+
*
|
|
834
|
+
* A boundary whose content has arrived is swapped into place, and the <Head>
|
|
835
|
+
* tags it set are applied. A placeholder with no content (the boundary failed,
|
|
836
|
+
* or the stream was cut short) is removed with its end marker, and the
|
|
837
|
+
* boundary's opening marker becomes <!--b!-->: the fallback stays as if the
|
|
838
|
+
* server had rendered it, so hydration claims it as a fallback and the client
|
|
839
|
+
* renders the boundary itself. A boundary that hydrated while it was waiting
|
|
840
|
+
* hydrates its content now (render.js _resumeBoundary). A placeholder is a
|
|
841
|
+
* <template id="pl-ID"> followed by its `<!--pl-ID-->` marker, and its content
|
|
842
|
+
* a <template id="ID">: anything else on the page is left alone. Returns how
|
|
843
|
+
* many boundaries it swapped.
|
|
844
|
+
*/
|
|
845
|
+
export function applyStreamedBoundaries() {
|
|
846
|
+
if (typeof document === 'undefined') return 0;
|
|
847
|
+
const data = __readHydrationData() || {};
|
|
848
|
+
let count = 0;
|
|
849
|
+
for (let progress = true; progress;) {
|
|
850
|
+
progress = false;
|
|
851
|
+
for (const placeholder of document.querySelectorAll('template[id^="pl-"]')) {
|
|
852
|
+
const id = placeholder.id.slice(3);
|
|
853
|
+
if (!BOUNDARY_ID.test(id)) continue;
|
|
854
|
+
const content = document.querySelector(`template[id="${id}"]`);
|
|
855
|
+
if (!content) continue;
|
|
856
|
+
const end = endMarker(placeholder);
|
|
857
|
+
if (!end) continue;
|
|
858
|
+
swapBoundary(placeholder, content, end);
|
|
859
|
+
if (data.head && Object.hasOwn(data.head, id)) applyLateHead(data.head[id]);
|
|
860
|
+
_resumeBoundary(id);
|
|
861
|
+
count++;
|
|
862
|
+
progress = true;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
for (const placeholder of document.querySelectorAll('template[id^="pl-"]')) {
|
|
866
|
+
const id = placeholder.id.slice(3);
|
|
867
|
+
if (!BOUNDARY_ID.test(id)) continue;
|
|
868
|
+
const end = endMarker(placeholder);
|
|
869
|
+
if (!end) continue;
|
|
870
|
+
const open = placeholder.previousSibling;
|
|
871
|
+
if (open !== null && open.nodeType === 8 && /** @type {Comment} */ (open).data === 'b') /** @type {Comment} */ (open).data = 'b!';
|
|
872
|
+
end.remove();
|
|
873
|
+
placeholder.remove();
|
|
874
|
+
_resumeBoundary(id);
|
|
875
|
+
}
|
|
876
|
+
return count;
|
|
34
877
|
}
|