@verajs/ssr 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/vera/index.js +12 -1
- package/src/vera/serializer.js +54 -3
package/package.json
CHANGED
package/src/vera/index.js
CHANGED
|
@@ -15,7 +15,18 @@ import { installShims, registry, hoistedStyles } from './shim.js';
|
|
|
15
15
|
import { serializeTemplate } from './serializer.js';
|
|
16
16
|
|
|
17
17
|
installShims();
|
|
18
|
-
const { setRenderer } = await import('@verajs/core');
|
|
18
|
+
const { setRenderer, insert } = await import('@verajs/core');
|
|
19
|
+
/**
|
|
20
|
+
* `static styles` moved out of core in 0.2.0 (`@verajs/styles`). Server rendering must still
|
|
21
|
+
* serialize them — the markup a browser produces includes the component's styles — and nothing on
|
|
22
|
+
* the server cares about the bytes, so SSR wires the adopter unconditionally rather than making
|
|
23
|
+
* every caller remember to.
|
|
24
|
+
*
|
|
25
|
+
* `insert` comes from core, not from `@verajs/inserts`, so the registration lands in the map core
|
|
26
|
+
* actually reads. Exactly why `setRenderer` above is taken from core too.
|
|
27
|
+
*/
|
|
28
|
+
const { adoptStyles } = await import('@verajs/styles');
|
|
29
|
+
insert('init', adoptStyles, 50);
|
|
19
30
|
|
|
20
31
|
/** The server renderer: template object in, markup into the (shadow) container shim. */
|
|
21
32
|
setRenderer((template, container) => {
|
package/src/vera/serializer.js
CHANGED
|
@@ -95,7 +95,9 @@ export const serializeTemplate = (template) => {
|
|
|
95
95
|
const value = values[i];
|
|
96
96
|
switch (kinds[i]) {
|
|
97
97
|
case TEXT:
|
|
98
|
-
|
|
98
|
+
/** A spread rewrites the open tag it sits in, so it is folded rather than appended. */
|
|
99
|
+
if (value !== null && typeof value === 'object' && value._$attrs$) out = foldSpread(out, value._$attrs$());
|
|
100
|
+
else out += serializeValue(value);
|
|
99
101
|
break;
|
|
100
102
|
case BOOLEAN:
|
|
101
103
|
if (value) out += ` ${names[i]}=""`;
|
|
@@ -113,13 +115,62 @@ export const serializeTemplate = (template) => {
|
|
|
113
115
|
return out + parts[kinds.length];
|
|
114
116
|
};
|
|
115
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Fold resolved spread bindings into the tag being built, mirroring what the client does.
|
|
120
|
+
*
|
|
121
|
+
* Appending is not enough, and the difference is a correctness bug rather than a nicety.
|
|
122
|
+
* `<input type="text" ${spread({ type: 'number' })}>` appends a second `type`, and an HTML parser
|
|
123
|
+
* keeps the **first** duplicate — so the server would render `type="text"` while the client, where
|
|
124
|
+
* `setAttribute` overwrites, renders `type="number"`. Same template, two answers, and a hydration
|
|
125
|
+
* mismatch between them.
|
|
126
|
+
*
|
|
127
|
+
* So a spread key removes any attribute of that name already written into the open tag before
|
|
128
|
+
* adding its own — including when it adds nothing, because `?disabled: false` and `id: null` both
|
|
129
|
+
* *remove* on the client and must remove here too. Kinds that never touch attributes client-side
|
|
130
|
+
* (events, non-form properties) leave the tag alone.
|
|
131
|
+
*
|
|
132
|
+
* Splitting on the last `<` is safe: attribute values are escaped, so no raw `<` can appear inside
|
|
133
|
+
* one.
|
|
134
|
+
*/
|
|
135
|
+
const foldSpread = (out, entries) => {
|
|
136
|
+
const tagStart = out.lastIndexOf('<');
|
|
137
|
+
let tag = out.slice(tagStart);
|
|
138
|
+
let added = '';
|
|
139
|
+
|
|
140
|
+
for (const [kind, name, value] of entries) {
|
|
141
|
+
const serializes = kind === 'a' || kind === 'b' || (kind === 'p' && FORM_ATTRIBUTES.includes(name));
|
|
142
|
+
if (!serializes) continue;
|
|
143
|
+
|
|
144
|
+
/** Quoted, single-quoted, unquoted, or valueless — whatever the template author wrote. */
|
|
145
|
+
tag = tag.replace(new RegExp(`\\s${name}(=("[^"]*"|'[^']*'|[^\\s>]*))?`, 'i'), '');
|
|
146
|
+
|
|
147
|
+
if (kind === 'b') {
|
|
148
|
+
if (value) added += ` ${name}=""`;
|
|
149
|
+
} else if (value != null && value !== false) {
|
|
150
|
+
added += ` ${name}="${escapeHtml(kind === 'p' && value === true ? '' : serializeValue(value, true))}"`;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The element-position slot already carries the separating space. */
|
|
155
|
+
return out.slice(0, tagStart) + tag + (added ? added.slice(1) : '');
|
|
156
|
+
};
|
|
157
|
+
|
|
116
158
|
const serializeValue = (value, raw = false) => {
|
|
117
159
|
if (value == null || value === false) return '';
|
|
118
160
|
if (Array.isArray(value)) return value.map((entry) => serializeValue(entry, raw)).join('');
|
|
119
161
|
if (typeof value === 'function') return '';
|
|
120
162
|
if (typeof value === 'object') {
|
|
121
|
-
/** Template-shaped (core's html, by shape) recurses
|
|
122
|
-
|
|
163
|
+
/** Template-shaped (core's html, by shape) recurses. */
|
|
164
|
+
if (value.strings) return serializeTemplate(value);
|
|
165
|
+
/**
|
|
166
|
+
* A spread (`@verajs/spread`) at element position. It hands back resolved bindings and this
|
|
167
|
+
* decides what reaches markup: attributes and truthy booleans do, form properties do because
|
|
168
|
+
* hydration reads them back, and events and other properties are client state. Escaping happens
|
|
169
|
+
* here and only here — principle #8 puts it at the render boundary, not at the source.
|
|
170
|
+
*
|
|
171
|
+
* Anything else at element position is a ref, which is a client concern.
|
|
172
|
+
*/
|
|
173
|
+
return '';
|
|
123
174
|
}
|
|
124
175
|
return raw ? String(value) : escapeHtml(value);
|
|
125
176
|
};
|