Skip to content

Commit 365f37d

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix(web): spread applies attributes before children, matching ssrElement (#3741) (#3757)
A dynamic() / <Dynamic> element whose prop getter mints a hydration id (a ternary over a signal compiles to a condition memo inside the getter) and whose children include an element hydrated with a key miss: the client's runtime spread() inserted `children` before its attribute effect read the getters, while the server's ssrElement read attributes first. Both draw on the enclosing component's id counter, so the memo and the child element swapped ids and the server's child was never claimed. spread() now creates its attribute effect before the children insert -- the order a compiled element already uses (its spread/attribute effects precede its children inserts). ssrElement defers reading a `children` prop until after every attribute (and the trailing compiled attribute thunk), so a `children` key listed before an id-minting attribute agrees too. Side effect on the client: a ref on a dynamic() element now runs before its children are inserted, as on a compiled element. Size: page: base +59 B brotli (+1 B minified), layout only; frozen cap raised 46.20 -> 46.24 KB with a Size-Exception note. Fixes #3741 Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 41be120 commit 365f37d

8 files changed

Lines changed: 101 additions & 29 deletions

File tree

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+
Fix a hydration key miss on a `dynamic()` / `<Dynamic>` element whose prop getter mints a hydration id (a conditional expression over a signal compiles to a condition memo) and whose children include an element (#3741). The client's runtime `spread` inserted `children` before running its attribute effect, while the server's `ssrElement` read attributes first, so the memo and the child element took each other's ids and the child was rebuilt instead of claimed. `spread` now applies attributes before inserting children — the order a compiled element already uses — and `ssrElement` reads a `children` prop after every attribute regardless of key order. Observable on the client: a `ref` on a `dynamic()` element now runs before its children are inserted, as it does on a compiled element.

‎packages/web/src/client.ts‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,10 +1013,9 @@ export function spread(node, props, skipChildren, skip, name) {
10131013
if (r !== prevProps.ref && (typeof r === "function" || Array.isArray(r))) ref(() => r, node);
10141014
assign(node, newProps, true, prevProps, true);
10151015
};
1016-
// The children inserts below are transparent by design (see
1017-
// `unscopedByDesign`); the mark spans their synchronous first compute.
1018-
if ("_SOLID_DEV_") unscopedByDesign = node;
10191016
if (Array.isArray(props)) {
1017+
effect(() => collectSources({}, props, undefined, skip), apply);
1018+
if ("_SOLID_DEV_") unscopedByDesign = node;
10201019
if (!skipChildren && !(skip !== undefined && skip("children")))
10211020
insert(node, () => {
10221021
for (let i = props.length - 1; i >= 0; i--) {
@@ -1025,31 +1024,8 @@ export function spread(node, props, skipChildren, skip, name) {
10251024
}
10261025
});
10271026
if ("_SOLID_DEV_") unscopedByDesign = null;
1028-
effect(() => collectSources({}, props, undefined, skip), apply);
10291027
return prevProps;
10301028
}
1031-
if (!skipChildren && !(skip !== undefined && skip("children"))) {
1032-
if (typeof props !== "function" && props != null && hasStaticKeys(props)) {
1033-
// A plain object's key set can't change reactively — nor can a
1034-
// merge/omit view's over plain objects, and its descriptor trap tells
1035-
// the truth about the owning leaf: no `children` key means nothing to
1036-
// insert, a data property inserts its value with no effect, only a
1037-
// getter needs the tracking scope. So `<Tag {...omit(props, "as")}>`
1038-
// with static children costs no children effect either.
1039-
const desc = Object.getOwnPropertyDescriptor(props, "children");
1040-
if (desc !== undefined) {
1041-
if (desc.get === undefined) insert(node, desc.value);
1042-
else insert(node, () => props.children);
1043-
}
1044-
} else
1045-
insert(node, () => {
1046-
const source = resolveSource(props);
1047-
return source != null && entryHas(source, "children")
1048-
? entryGet(source, "children")
1049-
: undefined;
1050-
});
1051-
}
1052-
if ("_SOLID_DEV_") unscopedByDesign = null;
10531029
effect(() => {
10541030
const source = resolveSource(props);
10551031
const newProps = {};
@@ -1078,6 +1054,29 @@ export function spread(node, props, skipChildren, skip, name) {
10781054
}
10791055
return newProps;
10801056
}, apply);
1057+
if ("_SOLID_DEV_") unscopedByDesign = node;
1058+
if (!skipChildren && !(skip !== undefined && skip("children"))) {
1059+
if (typeof props !== "function" && props != null && hasStaticKeys(props)) {
1060+
// A plain object's key set can't change reactively — nor can a
1061+
// merge/omit view's over plain objects, and its descriptor trap tells
1062+
// the truth about the owning leaf: no `children` key means nothing to
1063+
// insert, a data property inserts its value with no effect, only a
1064+
// getter needs the tracking scope. So `<Tag {...omit(props, "as")}>`
1065+
// with static children costs no children effect either.
1066+
const desc = Object.getOwnPropertyDescriptor(props, "children");
1067+
if (desc !== undefined) {
1068+
if (desc.get === undefined) insert(node, desc.value);
1069+
else insert(node, () => props.children);
1070+
}
1071+
} else
1072+
insert(node, () => {
1073+
const source = resolveSource(props);
1074+
return source != null && entryHas(source, "children")
1075+
? entryGet(source, "children")
1076+
: undefined;
1077+
});
1078+
}
1079+
if ("_SOLID_DEV_") unscopedByDesign = null;
10811080
return prevProps;
10821081
}
10831082

‎packages/web/src/server.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4422,6 +4422,10 @@ export function ssrElement(tag, props, children, needsId, skip, attrs, claims) {
44224422
// (see spreadBehaviorMarkers). Never touched outside a server component's
44234423
// render.
44244424
let behaviors = null;
4425+
// The source owning a `children` prop, read after every attribute: the
4426+
// client's `spread` inserts children after its attribute effect, and both
4427+
// may mint hydration ids on the enclosing counter (#3741).
4428+
let childOwner = null;
44254429
// One walk over one prop body: the outer loop runs once for a single props
44264430
// object and once per source otherwise. With several sources every
44274431
// source's key list is taken once up front, and "a later source owns this
@@ -4481,8 +4485,10 @@ export function ssrElement(tag, props, children, needsId, skip, attrs, claims) {
44814485
continue;
44824486
}
44834487
if (ChildProperties.has(prop)) {
4484-
if (children === undefined && !skipChildren)
4485-
children = info.raw || prop === "innerHTML" ? props[prop] : escape(props[prop]);
4488+
if (children === undefined && childOwner === null && !skipChildren) {
4489+
if (prop === "children") childOwner = props;
4490+
else children = info.raw || prop === "innerHTML" ? props[prop] : escape(props[prop]);
4491+
}
44864492
continue;
44874493
}
44884494
const value = props[prop];
@@ -4576,6 +4582,7 @@ export function ssrElement(tag, props, children, needsId, skip, attrs, claims) {
45764582
// The hydration key is unquoted, so a void element needs the space before
45774583
// `/>` or the slash becomes part of the key's value.
45784584
if (skipChildren) return { t: result + " />" };
4585+
if (childOwner !== null) children = info.raw ? childOwner.children : escape(childOwner.children);
45794586
if (typeof children === "function") children = children();
45804587
// The content most elements end up with — one string (a text child, escaped
45814588
// above or by the compiler), a number, nothing, or one finished node — joins
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "dynamic-children-key-before-prop-memo",
3+
"shell": "<button _hk=20 aria-label=\"b 0\"><i _hk=22>icon</i></button>",
4+
"rest": ""
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "dynamic-prop-memo-before-child",
3+
"shell": "<button _hk=20 aria-label=\"b 0\"><i _hk=22>icon</i>0</button>",
4+
"rest": ""
5+
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,32 @@ function LoadingOnBoundary() {
21872187
);
21882188
}
21892189

2190+
// solidjs/solid#3741: a dynamic() element whose prop getter mints a condition
2191+
// memo (a ternary over a signal) and whose children include an element. Both
2192+
// the memo and the `<i>` take ids from the component's counter, so the two
2193+
// sides must read `children` and the attribute getters in the same order.
2194+
let setDynamicLabelCount!: (v: number) => void;
2195+
function DynamicPropMemoBeforeChild() {
2196+
const [n, set] = createSignal(0);
2197+
setDynamicLabelCount = set;
2198+
const Comp = dynamic(() => "button");
2199+
return (
2200+
<Comp aria-label={n() ? `a ${n()}` : `b ${n()}`}>
2201+
<i>icon</i>
2202+
{n()}
2203+
</Comp>
2204+
);
2205+
}
2206+
// Same, with `children` ahead of the attribute in key order: the server must
2207+
// still read it after every attribute.
2208+
let setDynamicLabelCountFirst!: (v: number) => void;
2209+
function DynamicChildrenKeyBeforePropMemo() {
2210+
const [n, set] = createSignal(0);
2211+
setDynamicLabelCountFirst = set;
2212+
const Comp = dynamic(() => "button");
2213+
return <Comp children={<i>icon</i>} aria-label={n() ? `a ${n()}` : `b ${n()}`} />;
2214+
}
2215+
21902216
export const scenarios: Scenario[] = [
21912217
{
21922218
name: "polymorphic-chain",
@@ -3025,5 +3051,22 @@ export const scenarios: Scenario[] = [
30253051
expectedText: "shelllate#lobby",
30263052
serverText: "shell wait late #lobby",
30273053
stableSelector: "span, b"
3054+
},
3055+
{
3056+
name: "dynamic-prop-memo-before-child",
3057+
App: DynamicPropMemoBeforeChild,
3058+
expectedText: "icon0",
3059+
update: () => setDynamicLabelCount(1),
3060+
expectedTextAfterUpdate: "icon1",
3061+
stableSelector: "button, i",
3062+
adoptAll: true
3063+
},
3064+
{
3065+
name: "dynamic-children-key-before-prop-memo",
3066+
App: DynamicChildrenKeyBeforePropMemo,
3067+
expectedText: "icon",
3068+
update: () => setDynamicLabelCountFirst(1),
3069+
stableSelector: "button, i",
3070+
adoptAll: true
30283071
}
30293072
];

‎scripts/size/floor-caps.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"signals: core floor (createSignal/Memo/Effect/Root/flush)": "9.51 KB",
33
"app: render + one signal (the simple-app floor)": "12.05 KB",
44
"app: hydrating (no stores) with Show/For/Loading/Errored/lazy": "19.69 KB",
5-
"page: base server components (hydrating + dynamic + frames + sf reference)": "46.20 KB",
5+
"page: base server components (hydrating + dynamic + frames + sf reference)": "46.24 KB",
66
"page: live server components (base + live/GET + action + isPending/latest)": "50.45 KB",
77
"server: floor (getRequestEvent + isServer)": "1.34 KB",
88
"server: renderToString (the server-render floor)": "20.38 KB"

‎scripts/size/scenarios.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,14 @@ module.exports = [
30983098
// above is gone (no heap refusal, no `_manualWriteTime`, no
30993099
// `deleteFromHeap` in the setter). Reverts the #3740 raise. The cap is
31003100
// frozen again at 46.20 KB.
3101+
// Size-Exception (#3741, 2026-10-02): 46.20 -> 46.24 KB, measured at
3102+
// 46,240 B against `next` @ 98d35b9bc's 46,181 (+59 B; +1 B minified).
3103+
// `spread` runs its attribute effect before its children insert (the
3104+
// two blocks swap places; no code is added), so a prop getter's
3105+
// hydration id lands where the server's attribute walk takes it. Brotli
3106+
// layout only: three equivalent encodings measured +59..+129 B here and
3107+
// -21..+13 B on the live page; this is the smallest. Pending maintainer
3108+
// review.
31013109
limit: floorCaps["page: base server components (hydrating + dynamic + frames + sf reference)"],
31023110
alias: pageAlias
31033111
},

0 commit comments

Comments
 (0)