diff --git a/.changeset/orange-laws-drop.md b/.changeset/orange-laws-drop.md new file mode 100644 index 0000000000..99a594c9e0 --- /dev/null +++ b/.changeset/orange-laws-drop.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: more efficient checking for missing SSR text node diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js index 684d19bb3f..c5b7c29216 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js @@ -30,13 +30,16 @@ export function process_children(nodes, expression, is_element, { visit, state } if (node.type === 'Text') { let prev = expression; - expression = () => b.call('$.sibling', prev(true)); + expression = () => b.call('$.sibling', prev(false)); state.template.push(node.raw); return; } } - const id = get_node_id(expression(true), state, 'text'); + // if this is a standalone `{expression}`, make sure we handle the case where + // no text node was created because the expression was empty during SSR + const needs_hydration_check = sequence.length === 1; + const id = get_node_id(expression(needs_hydration_check), state, 'text'); state.template.push(' '); diff --git a/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js index 416af28608..c6a894ea71 100644 --- a/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js @@ -27,7 +27,7 @@ export default function Bind_component_snippet($$anchor) { } }); - var text_1 = $.sibling(node, true); + var text_1 = $.sibling(node); $.template_effect(() => $.set_text(text_1, ` value: ${$.get(value) ?? ""}`)); $.append($$anchor, fragment); diff --git a/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js b/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js index aa4d22d99a..ef070bf288 100644 --- a/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js +++ b/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js @@ -9,17 +9,17 @@ export default function Main($$anchor) { let y = () => 'test'; var fragment = root(); var div = $.first_child(fragment); - var svg = $.sibling($.sibling(div, true)); - var custom_element = $.sibling($.sibling(svg, true)); - var div_1 = $.sibling($.sibling(custom_element, true)); + var svg = $.sibling($.sibling(div)); + var custom_element = $.sibling($.sibling(svg)); + var div_1 = $.sibling($.sibling(custom_element)); $.template_effect(() => $.set_attribute(div_1, "foobar", y())); - var svg_1 = $.sibling($.sibling(div_1, true)); + var svg_1 = $.sibling($.sibling(div_1)); $.template_effect(() => $.set_attribute(svg_1, "viewBox", y())); - var custom_element_1 = $.sibling($.sibling(svg_1, true)); + var custom_element_1 = $.sibling($.sibling(svg_1)); $.template_effect(() => $.set_custom_element_data(custom_element_1, "fooBar", y())); diff --git a/packages/svelte/tests/snapshot/samples/purity/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/purity/_expected/client/index.svelte.js index 8d865bdd49..8a2daa877f 100644 --- a/packages/svelte/tests/snapshot/samples/purity/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/purity/_expected/client/index.svelte.js @@ -13,11 +13,11 @@ export default function Purity($$anchor) { p.textContent = Math.max(min, Math.min(max, number)); - var p_1 = $.sibling($.sibling(p, true)); + var p_1 = $.sibling($.sibling(p)); p_1.textContent = location.href; - var node = $.sibling($.sibling(p_1, true)); + var node = $.sibling($.sibling(p_1)); Child(node, { prop: encodeURIComponent(value) }); $.append($$anchor, fragment); diff --git a/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js index 0f2d6f4200..8f12fd7913 100644 --- a/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js @@ -18,11 +18,11 @@ export default function State_proxy_literal($$anchor) { $.remove_input_defaults(input); - var input_1 = $.sibling($.sibling(input, true)); + var input_1 = $.sibling($.sibling(input)); $.remove_input_defaults(input_1); - var button = $.sibling($.sibling(input_1, true)); + var button = $.sibling($.sibling(input_1)); button.__click = [reset, str, tpl]; $.bind_value(input, () => $.get(str), ($$value) => $.set(str, $$value));