Skip to content

Commit ec37f1c

Browse files
feat(web): ssrElement accepts an array of prop sources and a skip predicate
`ssrElement(tag, [a, b, c], children, needsId, skip)` serializes straight from the sources with the exact output of `ssrElement(tag, merge(a, b, c), ...)`: later sources win per key, attributes land in merged order, only the winning source's getter is read (once), and `skip(key)` drops a key from every source without reading it. Libraries and compilers spreading several sources onto an element no longer build an intermediate merged object that is walked once and discarded. The array — or a thunk yielding it — is resolved after the hydration key is taken, so a source getter that renders a child cannot shift the element's own id. A function source is a plain thunk called once: no memo, no hydration ids, matching the client `spread` array form. Nullish sources are empty. The single-object and thunk forms are unchanged. Measured on the yak-bench SSR suite with next-yak's runtime calling this instead of its hand-written serializer: elements with dynamic props go from 2.1-2.9x slower than the hand-written path to 1.1-1.2x; suite geomean 2.43x -> 1.69x. Co-authored-by: Cursor <[email protected]>
1 parent 14ded24 commit ec37f1c

6 files changed

Lines changed: 467 additions & 66 deletions

File tree

‎.changeset/ssr-element-sources.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
`ssrElement` accepts an array of prop sources and an optional `skip` predicate: `ssrElement(tag, [a, b, c], children, needsId, skip)`. The array form serializes straight from the sources with the exact output of `ssrElement(tag, merge(a, b, c), ...)` — later sources win per key, attributes land in merged order, only the winning source's getter is read (once), and `skip(key)` drops a key from every source without reading it — so libraries and compilers spreading several sources no longer have to build an intermediate merged object that is walked once and discarded. The array (or a thunk yielding it) is resolved after the hydration key is taken; a function source is a plain thunk called once that creates no memo and consumes no hydration ids. Nullish sources are empty. The single-object form is unchanged.

‎packages/web/src/server-mock.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@ export function ssr(template: string[] | string, ...nodes: any[]): { t: string }
338338
*/
339339
export function ssrElement(
340340
name: string,
341-
props: any,
341+
props: any | any[] | (() => any | any[]),
342342
children: any,
343-
needsId: boolean
343+
needsId: boolean,
344+
skip?: (key: string) => boolean
344345
): { t: string } {}
345346
/**
346347
* Compiler primitive — serializes a class value (string, object map, or

‎packages/web/src/server.ts‎

Lines changed: 102 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,90 +3621,128 @@ export function ssrStyleProperty(name, value) {
36213621
}
36223622
export function ssrElement(
36233623
name: string,
3624-
props: any,
3624+
props: any | any[] | (() => any | any[]),
36253625
children: any,
3626-
needsId: boolean
3626+
needsId: boolean,
3627+
skip?: (key: string) => boolean
36273628
): { t: string };
36283629

36293630
// review with new ssr
3630-
export function ssrElement(tag, props, children, needsId) {
3631+
export function ssrElement(tag, props, children, needsId, skip) {
36313632
// The hydration key must be allocated before the props thunk runs: dynamic
36323633
// props (`mergeProps(() => ...)`) create a memo, which consumes a child id.
36333634
// The client claims the element (getNextElement) before applying the spread,
36343635
// so the server must allocate in the same order or the element's own id
36353636
// shifts by one and it is left unclaimed on hydration.
36363637
const hk = needsId ? ssrHydrationKey() : "";
36373638
if (typeof props === "function") props = props();
3638-
// A nullish source (static or resolved) is an empty spread (#3297).
3639-
if (props == null) props = {};
3639+
// `props` is now one props object or an ARRAY of prop sources (either may
3640+
// arrive through the thunk above, which runs after the key is taken). The
3641+
// array form serializes straight from the sources as if they had been
3642+
// merged (`ssrElement(tag, merge(...sources), ...)`): later sources win per
3643+
// key, and the attribute order is the merged one — every key at the
3644+
// position of the LAST source that carries it. It exists so libraries and
3645+
// the compilers can spread several sources without building an
3646+
// intermediate object that only gets walked once. Unlike
3647+
// `mergeProps(() => x)`, a function source here is a plain thunk: it is
3648+
// called once and creates no memo, so the array form allocates no
3649+
// hydration ids of its own — the client `spread` array form follows the
3650+
// same rule. A nullish source is an empty source.
3651+
let sources = null;
3652+
if (Array.isArray(props)) {
3653+
sources = props;
3654+
for (let i = 0; i < sources.length; i++) {
3655+
if (typeof sources[i] === "function") {
3656+
// Resolve into a copy: the caller's array stays as passed.
3657+
if (sources === props) sources = sources.slice();
3658+
sources[i] = sources[i]();
3659+
}
3660+
}
3661+
} else if (props == null) {
3662+
// A nullish source (static or resolved) is an empty spread (#3297).
3663+
props = {};
3664+
}
36403665
const skipChildren = VOID_ELEMENTS.test(tag);
3641-
const keys = Object.keys(props);
36423666
// Each emitted attribute carries its own leading space (the hydration key
36433667
// already does), so skipped props leave no stray whitespace behind:
36443668
// `<li _hk=0>` rather than `<li _hk=0 >` (#3382).
36453669
let result = `<${tag}${hk}`;
3646-
for (let i = 0; i < keys.length; i++) {
3647-
const prop = keys[i];
3648-
// Every branch reads `props[prop]` itself, and only when it will use it.
3649-
// On a spread these are compiled getters: `children` builds the child
3650-
// element and consumes hydration ids as it goes. Reading it more than
3651-
// once — or at all when JSX children already own the slot — burns ids
3652-
// the client never allocates, and every element after it hydrates
3653-
// against the wrong node (#3313). Keep the general read below the two
3654-
// early-outs.
3655-
//
3656-
// The compiler moves static textarea values into children, but an
3657-
// element with a spread is serialized here instead. Keep the runtime
3658-
// path equivalent: textarea value/defaultValue are its text content,
3659-
// never HTML attributes (#3286).
3660-
if (tag === "textarea" && (prop === "value" || prop === "defaultValue")) {
3670+
// One walk over one prop body: the outer loop runs once for a single props
3671+
// object and once per source otherwise.
3672+
const last = sources === null ? 0 : sources.length - 1;
3673+
for (let s = 0; s <= last; s++) {
3674+
if (sources !== null && (props = sources[s]) == null) continue;
3675+
const keys = Object.keys(props);
3676+
nextKey: for (let i = 0; i < keys.length; i++) {
3677+
const prop = keys[i];
3678+
if (skip !== undefined && skip(prop)) continue;
3679+
// A later source that has the key (`in`, so merge/omit proxies answer
3680+
// through their traps) owns it; this source's getter stays unread.
3681+
for (let j = s + 1; j <= last; j++) {
3682+
const later = sources[j];
3683+
if (later != null && prop in later) continue nextKey;
3684+
}
3685+
// Every branch reads `props[prop]` itself, and only when it will use it.
3686+
// On a spread these are compiled getters: `children` builds the child
3687+
// element and consumes hydration ids as it goes. Reading it more than
3688+
// once — or at all when JSX children already own the slot — burns ids
3689+
// the client never allocates, and every element after it hydrates
3690+
// against the wrong node (#3313). Keep the general read below the two
3691+
// early-outs.
3692+
//
3693+
// The compiler moves static textarea values into children, but an
3694+
// element with a spread is serialized here instead. Keep the runtime
3695+
// path equivalent: textarea value/defaultValue are its text content,
3696+
// never HTML attributes (#3286).
3697+
if (tag === "textarea" && (prop === "value" || prop === "defaultValue")) {
3698+
const value = props[prop];
3699+
if (value !== null) children = escape(value);
3700+
continue;
3701+
}
3702+
if (ChildProperties.has(prop)) {
3703+
if (children === undefined && !skipChildren)
3704+
children =
3705+
tag === "script" || tag === "style" || prop === "innerHTML"
3706+
? props[prop]
3707+
: escape(props[prop]);
3708+
continue;
3709+
}
36613710
const value = props[prop];
3662-
if (value !== null) children = escape(value);
3663-
continue;
3664-
}
3665-
if (ChildProperties.has(prop)) {
3666-
if (children === undefined && !skipChildren)
3667-
children =
3668-
tag === "script" || tag === "style" || prop === "innerHTML"
3669-
? props[prop]
3670-
: escape(props[prop]);
3671-
continue;
3672-
}
3673-
const value = props[prop];
3674-
// Nullish is "not set" for every attribute, `style`/`class` included —
3675-
// the client removes the attribute for `undefined`, and emitting
3676-
// `style=""` here made the server disagree with it (#3382).
3677-
if (
3678-
value == undefined ||
3679-
prop === "ref" ||
3680-
prop.slice(0, 2) === "on" ||
3681-
prop.slice(0, 5) === "prop:"
3682-
) {
3683-
// Behavior claims ride NAMED ref/on* positions only — the compiler
3684-
// can't see through a spread, so a claim-carrying stub landing here
3685-
// silently drops. Say so where the author can act on it.
3711+
// Nullish is "not set" for every attribute, `style`/`class` included —
3712+
// the client removes the attribute for `undefined`, and emitting
3713+
// `style=""` here made the server disagree with it (#3382).
36863714
if (
3687-
"_SOLID_DEV_" &&
3688-
typeof value === "function" &&
3689-
value[CLAIM_PROP] !== undefined &&
3690-
(prop === "ref" || prop.slice(0, 2) === "on")
3715+
value == undefined ||
3716+
prop === "ref" ||
3717+
prop.slice(0, 2) === "on" ||
3718+
prop.slice(0, 5) === "prop:"
36913719
) {
3692-
console.warn(
3693-
`A spread on a server-rendered <${tag}> carries \`${prop}\` from client props — ` +
3694-
`spreads don't participate in behavior claims, so this drops. ` +
3695-
`Write the position out: \`${prop}={props.${String(value[CLAIM_PROP])}}\`.`
3696-
);
3720+
// Behavior claims ride NAMED ref/on* positions only — the compiler
3721+
// can't see through a spread, so a claim-carrying stub landing here
3722+
// silently drops. Say so where the author can act on it.
3723+
if (
3724+
"_SOLID_DEV_" &&
3725+
typeof value === "function" &&
3726+
value[CLAIM_PROP] !== undefined &&
3727+
(prop === "ref" || prop.slice(0, 2) === "on")
3728+
) {
3729+
console.warn(
3730+
`A spread on a server-rendered <${tag}> carries \`${prop}\` from client props — ` +
3731+
`spreads don't participate in behavior claims, so this drops. ` +
3732+
`Write the position out: \`${prop}={props.${String(value[CLAIM_PROP])}}\`.`
3733+
);
3734+
}
3735+
continue;
3736+
} else if (prop === "style") {
3737+
result += ` style="${ssrStyle(value)}"`;
3738+
} else if (prop === "class") {
3739+
result += ` class="${ssrClassName(value)}"`;
3740+
} else if (typeof value === "boolean") {
3741+
if (!value) continue;
3742+
result += ` ${escape(prop)}`;
3743+
} else {
3744+
result += value === "" ? ` ${escape(prop)}` : ` ${escape(prop)}="${escape(value, true)}"`;
36973745
}
3698-
continue;
3699-
} else if (prop === "style") {
3700-
result += ` style="${ssrStyle(value)}"`;
3701-
} else if (prop === "class") {
3702-
result += ` class="${ssrClassName(value)}"`;
3703-
} else if (typeof value === "boolean") {
3704-
if (!value) continue;
3705-
result += ` ${escape(prop)}`;
3706-
} else {
3707-
result += value === "" ? ` ${escape(prop)}` : ` ${escape(prop)}="${escape(value, true)}"`;
37083746
}
37093747
}
37103748

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "ssr-element-sources",
3+
"shell": "<div _hk=1><!--$--><span _hk=0 class=\"src\" data-a=\"1\" title=\"new\" id=\"srcs\">srcs</span><!--/--><b>tail</b></div>",
4+
"rest": ""
5+
}

‎packages/web/test/harness/scenarios.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
httpHeader,
4242
clientOnly,
4343
isServer,
44+
ssrElement,
4445
type JSX
4546
} from "@solidjs/web";
4647

@@ -1661,6 +1662,33 @@ function MixedMemoResults() {
16611662
return <div>{items.map(f => createMemo(f))}</div>;
16621663
}
16631664

1665+
// ---------------------------------------------------------------------------
1666+
// ssrElement's multi-source form: the server serializes `<span>` straight from
1667+
// two prop sources (no merged intermediate object) while the client compiles
1668+
// the equivalent double spread. The array form must allocate exactly the ids
1669+
// the compiled spread does — the span's own key and nothing for the sources —
1670+
// so the span AND the sibling after it hydrate against the server nodes.
1671+
let setSourcesLabel!: (v: string) => void;
1672+
function SpreadSources() {
1673+
const [label, set] = createSignal("tail");
1674+
setSourcesLabel = set;
1675+
const a = { class: "src", title: "old", "data-a": "1" };
1676+
const b = { title: "new", id: "srcs" };
1677+
const el = isServer ? (
1678+
(ssrElement("span", [a, b], "srcs", true) as unknown as JSX.Element)
1679+
) : (
1680+
<span {...a} {...b}>
1681+
srcs
1682+
</span>
1683+
);
1684+
return (
1685+
<div>
1686+
{el}
1687+
<b>{label()}</b>
1688+
</div>
1689+
);
1690+
}
1691+
16641692
export const scenarios: Scenario[] = [
16651693
{
16661694
name: "text-hole",
@@ -2249,5 +2277,14 @@ export const scenarios: Scenario[] = [
22492277
update: () => setSepMixed("C"),
22502278
expectedTextAfterUpdate: "a1Cd25",
22512279
stableSelector: "div, b, i"
2280+
},
2281+
{
2282+
name: "ssr-element-sources",
2283+
App: SpreadSources,
2284+
expectedText: "srcstail",
2285+
adoptAll: true,
2286+
update: () => setSourcesLabel("TAIL"),
2287+
expectedTextAfterUpdate: "srcsTAIL",
2288+
stableSelector: "div, span, b"
22522289
}
22532290
];

0 commit comments

Comments
 (0)