Skip to content

Commit 63484af

Browse files
authored
fix: ensure last empty text node correctly hydrates (#14425)
* fix: ensure last empty text node correctly hydrates * fix: ensure last empty text node correctly hydrates
1 parent 37e6c7f commit 63484af

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

.changeset/short-chicken-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure last empty text node correctly hydrates

packages/svelte/src/internal/client/dom/operations.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,31 @@ export function first_child(fragment, is_text) {
154154
*/
155155
export function sibling(node, count = 1, is_text = false) {
156156
let next_sibling = hydrating ? hydrate_node : node;
157+
var last_sibling;
157158

158159
while (count--) {
160+
last_sibling = next_sibling;
159161
next_sibling = /** @type {TemplateNode} */ (get_next_sibling(next_sibling));
160162
}
161163

162164
if (!hydrating) {
163165
return next_sibling;
164166
}
165167

166-
var type = next_sibling.nodeType;
168+
var type = next_sibling?.nodeType;
167169

168170
// if a sibling {expression} is empty during SSR, there might be no
169171
// text node to hydrate — we must therefore create one
170172
if (is_text && type !== 3) {
171173
var text = create_text();
172-
next_sibling?.before(text);
174+
// If the next sibling is `null` and we're handling text then it's because
175+
// the SSR content was empty for the text, so we need to generate a new text
176+
// node and insert it after the last sibling
177+
if (next_sibling === null) {
178+
last_sibling?.after(text);
179+
} else {
180+
next_sibling.before(text);
181+
}
173182
set_hydrate_node(text);
174183
return text;
175184
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--[--><span><span></span></span><!--]-->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let { name, remaining } = $props();
3+
</script>
4+
5+
<span>
6+
<span>{name}</span>{remaining >= 2 ? ',' : ''}
7+
</span>

0 commit comments

Comments
 (0)