Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-comics-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: better handling of empty text node hydration
Original file line number Diff line number Diff line change
Expand Up @@ -1127,15 +1127,15 @@ function create_block(parent, name, nodes, context) {
trimmed.every((node) => node.type === 'Text' || node.type === 'ExpressionTag');

if (use_space_template) {
// special case — we can use `$.space` instead of creating a unique template
// special case — we can use `$.space_frag` instead of creating a unique template
const id = b.id(context.state.scope.generate('text'));

process_children(trimmed, () => id, false, {
...context,
state
});

body.push(b.var(id, b.call('$.space', b.id('$$anchor'))), ...state.init);
body.push(b.var(id, b.call('$.space_frag', b.id('$$anchor'))), ...state.init);
close = b.stmt(b.call('$.close', b.id('$$anchor'), id));
} else {
/** @type {(is_text: boolean) => import('estree').Expression} */
Expand Down Expand Up @@ -1495,7 +1495,7 @@ function process_children(nodes, expression, is_element, { visit, state }) {

state.template.push(' ');

const text_id = get_node_id(expression(true), state, 'text');
const text_id = get_node_id(b.call('$.space', expression(true)), state, 'text');

const singular = b.stmt(
b.call(
Expand Down
20 changes: 18 additions & 2 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const comment_template = template('<!>', true);
* @param {Text | Comment | Element | null} anchor
*/
/*#__NO_SIDE_EFFECTS__*/
export function space(anchor) {
export function space_frag(anchor) {
/** @type {Node | null} */
var node = /** @type {any} */ (open(anchor, true, space_template));
// if an {expression} is empty during SSR, there might be no
Expand All @@ -216,11 +216,27 @@ export function space(anchor) {
node = empty();
// @ts-ignore in this case the anchor should always be a comment,
// if not something more fundamental is wrong and throwing here is better to bail out early
anchor.parentElement.insertBefore(node, anchor);
anchor.before(node);
}
return node;
}

/**
* @param {Text | Comment | Element} anchor
*/
/*#__NO_SIDE_EFFECTS__*/
export function space(anchor) {
// if an {expression} is empty during SSR, there might be no
// text node to hydrate (or an anchor comment is falsely detected instead)
// — we must therefore create one
if (hydrating && anchor.nodeType !== 3) {
const node = empty();
anchor.before(node);
return node;
}
return anchor;
}

/**
* @param {null | Text | Comment | Element} anchor
*/
Expand Down
15 changes: 15 additions & 0 deletions packages/svelte/tests/runtime-runes/samples/state-space/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from '../../test';

export default test({
html: `<button type="button">Update Text</button><div></div>`,

async test({ assert, target }) {
const btn = target.querySelector('button');

await btn?.click();
assert.htmlEqual(
target.innerHTML,
`<button type="button">Update Text</button><div>updated</div>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
let text = $state('');

function update_text() {
text = 'updated';
}
</script>

<button type="button" on:click={update_text}>Update Text</button>

<div>{text}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function Each_string_template($$anchor, $$props) {
1,
($$anchor, thing, $$index) => {
/* Init */
var text = $.space($$anchor);
var text = $.space_frag($$anchor);

/* Update */
$.text_effect(text, () => `${$.stringify($.unwrap(thing))}, `);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function Function_prop_no_getter($$anchor, $$props) {
onmouseenter: () => $.set(count, $.proxy(plusOne($.get(count)))),
children: ($$anchor, $$slotProps) => {
/* Init */
var text = $.space($$anchor);
var text = $.space_frag($$anchor);

/* Update */
$.text_effect(text, () => `clicks: ${$.stringify($.get(count))}`);
Expand All @@ -33,4 +33,4 @@ export default function Function_prop_no_getter($$anchor, $$props) {

$.close_frag($$anchor, fragment);
$.pop();
}
}