fix: transform derived assignments and select function bindings correctly during server-side rendering (#18669)

follow-up to the fix in #18607 - there are a two more bugs related to
writeable deriveds in that area
pull/18721/head
Simon H 4 days ago committed by GitHub
parent 14ec75c5fe
commit 68e7c9b521
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: transform derived assignments and select function bindings correctly during server-side rendering

@ -110,7 +110,7 @@ function build_assignment(operator, left, right, context) {
context.visit(build_assignment_value(operator, left, right))
);
return b.call(binding.node, value);
return b.call(object, value);
}
return null;

@ -324,10 +324,9 @@ export function build_spread_object(element, attributes, context, transform) {
return b.prop('init', b.key(name), value);
} else if (attribute.type === 'BindDirective') {
const name = get_attribute_name(element, attribute);
const expression = /** @type {Expression} */ (context.visit(attribute.expression));
const value =
attribute.expression.type === 'SequenceExpression'
? b.call(attribute.expression.expressions[0])
: /** @type {Expression} */ (context.visit(attribute.expression));
expression.type === 'SequenceExpression' ? b.call(expression.expressions[0]) : expression;
return b.prop('init', b.key(name), value);
}

@ -1,6 +0,0 @@
import { test } from '../../test';
export default test({
ssrHtml: '<p>LATER</p> <input value="LATER">',
html: '<p>LATER</p> <input>'
});

@ -0,0 +1,6 @@
import { test } from '../../test';
export default test({
ssrHtml: '<p>y:y</p> <p>LATER</p> <input value="LATER">',
html: '<p>y:y</p> <p>LATER</p> <input>'
});

@ -1,4 +1,13 @@
<script>
function write(value) {
return foo = value;
}
// a leading comment on the declaration
// that spans more than one line
let foo = $derived('x');
let bar = write('y');
const ctx = {
get later() {
return later;
@ -10,5 +19,6 @@
let later = $derived.by(() => 'LATER');
</script>
<p>{foo}:{bar}</p>
<p>{ctx.later}</p>
<input bind:value={later} />

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
ssrHtml: '<select><option value="a">a</option><option value="b" selected="">b</option></select>',
test({ assert, target }) {
const select = /** @type {HTMLSelectElement} */ (target.querySelector('select'));
assert.equal(select.value, 'b');
}
});

@ -0,0 +1,9 @@
<script>
let source = $state('b');
let value = $derived(source);
</script>
<select bind:value={() => value, (next) => (value = next)}>
<option value="a">a</option>
<option value="b">b</option>
</select>
Loading…
Cancel
Save