Skip to content

Commit a01e054

Browse files
committed
fix(compiler): run an element's ref after its attributes
Refs were emitted ahead of an element's attribute writes. A ref's own attribute write survived hydration, where setAttribute returns early, but was overwritten on client creation. Refs are now emitted after attributes and spread, before children, in both the Babel plugin and the native compiler. Fixes #3685
1 parent c3adc28 commit a01e054

12 files changed

Lines changed: 51 additions & 35 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@solidjs/babel-plugin": patch
3+
"@solidjs/compiler": patch
4+
---
5+
6+
DOM output: an element's `ref` now runs after its attributes and spread are applied, and still before its children. A ref's own attribute writes now survive client creation the way they already survived hydration (#3685).

‎packages/babel-plugin/src/dom/element.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ function transformAttributes(
497497
hasChildren = path.node.children.length > 0,
498498
hasSpread = attributes.some(attribute => t.isJSXSpreadAttribute(attribute.node)),
499499
config = getConfig(path);
500+
const refs: babelTypes.Statement[] = [];
500501

501502
// preprocess spreads
502503
if (hasSpread) {
@@ -976,7 +977,7 @@ function transformAttributes(
976977
(binding.kind === "const" || binding.kind === "module");
977978
if (!isConstant && t.isLVal(value.expression)) {
978979
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
979-
results.exprs.unshift(
980+
refs.push(
980981
t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]),
981982
t.expressionStatement(
982983
t.conditionalExpression(
@@ -1005,7 +1006,7 @@ function transformAttributes(
10051006
t.isFunction(value.expression) ||
10061007
t.isArrayExpression(value.expression)
10071008
) {
1008-
results.exprs.unshift(
1009+
refs.push(
10091010
t.expressionStatement(
10101011
t.callExpression(
10111012
registerImportMethod(path, "ref", getRendererConfig(path, "dom").moduleName),
@@ -1015,7 +1016,7 @@ function transformAttributes(
10151016
);
10161017
} else {
10171018
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
1018-
results.exprs.unshift(
1019+
refs.push(
10191020
t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]),
10201021
t.expressionStatement(
10211022
t.logicalExpression(
@@ -1245,6 +1246,9 @@ function transformAttributes(
12451246
path.node.children.push(children);
12461247
}
12471248
if (spreadExpr) results.exprs.push(...(Array.isArray(spreadExpr) ? spreadExpr : [spreadExpr]));
1249+
// Refs run after the element's attributes and spread are applied, so a ref's
1250+
// own writes survive client creation the way they survive hydration.
1251+
results.exprs.push(...refs);
12481252

12491253
results.hasHydratableEvent = results.hasHydratableEvent || hasHydratableEvent;
12501254
}

‎packages/babel-plugin/test/__dom_compatible_fixtures__/attributeExpressions/output.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ _$spread(
139139
],
140140
true
141141
);
142-
var _ref$ = link;
143-
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
144142
_$claimElement(_el$3);
145143
_$className(_el$3, {
146144
"ccc ddd": true
147145
});
146+
var _ref$ = link;
147+
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
148148
const template = _el$;
149149
var _el$4 = _tmpl$2(),
150150
_el$5 = _el$4.firstChild,

‎packages/babel-plugin/test/__dom_fixtures__/attributeExpressions/output.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ _$spread(
134134
],
135135
true
136136
);
137-
var _ref$ = link;
138-
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
139137
_$claimElement(_el$3);
140138
_$className(_el$3, {
141139
"ccc ddd": true
142140
});
141+
var _ref$ = link;
142+
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
143143
const template = _el$;
144144
var _el$4 = _tmpl$2(),
145145
_el$5 = _el$4.firstChild,

‎packages/babel-plugin/test/__dom_fixtures__/components/output.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,11 @@ function MyComponent(props) {
531531
let el;
532532
const others = omit(props, "children");
533533
var _el$48 = _tmpl$2();
534+
_$spread(_el$48, others, true);
534535
var _ref$9 = el;
535536
typeof _ref$9 === "function" || Array.isArray(_ref$9)
536537
? _$ref(() => _ref$9, _el$48)
537538
: (el = _el$48);
538-
_$spread(_el$48, others, true);
539539
_$insert(_el$48, () => props.children);
540540
return _el$48;
541541
}

‎packages/babel-plugin/test/__dom_hydratable_fixtures__/attributeExpressions/output.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ _$spread(
132132
],
133133
true
134134
);
135-
var _ref$ = link;
136-
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
137135
_$claimElement(_el$3);
138136
_$className(_el$3, {
139137
"ccc ddd": true
140138
});
139+
var _ref$ = link;
140+
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
141141
_$runHydrationEvents();
142142
const template = _el$;
143143
var _el$4 = _$getNextElement(_tmpl$2),

‎packages/babel-plugin/test/__dynamic_fixtures__/attributeExpressions/output.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ _$spread(
124124
],
125125
true
126126
);
127-
var _ref$ = link;
128-
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
129127
_$claimElement(_el$3);
130128
_$className(_el$3, {
131129
"ccc ddd": true
132130
});
131+
var _ref$ = link;
132+
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => _ref$, _el$3) : (link = _el$3);
133133
const template = _el$;
134134
var _el$4 = _tmpl$2(),
135135
_el$5 = _el$4.firstChild,

‎packages/compiler/__tests__/fixtures/dom-hydratable/attributeExpressions/output.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ _$spread(_el$2, [
110110
}
111111
}
112112
], true);
113+
_$claimElement(_el$3);
114+
_$className(_el$3, { "ccc ddd": true });
113115
var _ref$ = link;
114116
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => {
115117
return _ref$;
116118
}, _el$3) : link = _el$3;
117-
_$claimElement(_el$3);
118-
_$className(_el$3, { "ccc ddd": true });
119119
_$runHydrationEvents();
120120
const template = _el$;
121121
var _el$4 = _$getNextElement(_tmpl$2);

‎packages/compiler/__tests__/fixtures/dom/attributeExpressions/output.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ _$spread(_el$2, [
110110
}
111111
}
112112
], true);
113+
_$claimElement(_el$3);
114+
_$className(_el$3, { "ccc ddd": true });
113115
var _ref$ = link;
114116
typeof _ref$ === "function" || Array.isArray(_ref$) ? _$ref(() => {
115117
return _ref$;
116118
}, _el$3) : link = _el$3;
117-
_$claimElement(_el$3);
118-
_$className(_el$3, { "ccc ddd": true });
119119
const template = _el$;
120120
var _el$4 = _tmpl$2();
121121
var _el$5 = _el$4.firstChild;

‎packages/compiler/__tests__/fixtures/dom/components/output.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ function MyComponent(props) {
354354
let el;
355355
const others = omit(props, "children");
356356
var _el$48 = _tmpl$2();
357+
_$spread(_el$48, others, true);
357358
var _ref$9 = el;
358359
typeof _ref$9 === "function" || Array.isArray(_ref$9) ? _$ref(() => {
359360
return _ref$9;
360361
}, _el$48) : el = _el$48;
361-
_$spread(_el$48, others, true);
362362
_$insert(_el$48, () => {
363363
return props.children;
364364
});

0 commit comments

Comments
 (0)