Skip to content

Commit 78506c0

Browse files
committed
fix: handle sole empty expression tags
When there's only a single expression tag and its value evaluates to the empty string, special handling is needed to create and insert a text node fixes #10426
1 parent 0e011ad commit 78506c0

File tree

7 files changed

+29
-3
lines changed

7 files changed

+29
-3
lines changed

.changeset/silent-apes-report.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: handle sole empty expression tags

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function child_frag(node, is_text) {
201201
return text;
202202
}
203203

204-
if (first_node !== null) {
204+
if (first_node) {
205205
return capture_fragment_from_node(first_node);
206206
}
207207

packages/svelte/src/internal/client/render.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,22 @@ const space_template = template(' ', false);
212212
const comment_template = template('<!>', true);
213213

214214
/**
215-
* @param {null | Text | Comment | Element} anchor
215+
* @param {Text | Comment | Element | null} anchor
216216
*/
217217
/*#__NO_SIDE_EFFECTS__*/
218218
export function space(anchor) {
219-
return open(anchor, true, space_template);
219+
/** @type {any} */
220+
var node = open(anchor, true, space_template);
221+
// if an {expression} is empty during SSR, there might be no
222+
// text node to hydrate (or a anchor comment is falsely detected instead)
223+
// — we must therefore create one
224+
if (current_hydration_fragment !== null && node?.nodeType !== 3) {
225+
node = document.createTextNode('');
226+
// @ts-ignore in this case the anchor should always be a comment,
227+
// if not something more fundamental is wrong and throwing here is better to bail out early
228+
anchor.parentElement.insertBefore(node, anchor.nextSibling);
229+
}
230+
return node;
220231
}
221232

222233
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--ssr:0-->x<!--ssr:0-->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--ssr:0--><!--ssr:0-->
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let x = typeof window === 'undefined' ? '' : 'x'
3+
</script>
4+
5+
{x}

0 commit comments

Comments
 (0)