Skip to content

Commit e45d91b

Browse files
committed
remove <!--ssr:each_else--> comments
1 parent ba7c20b commit e45d91b

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,24 +1500,28 @@ const template_visitors = {
15001500
b.update('++', index, false),
15011501
b.block(each)
15021502
);
1503+
1504+
const close = b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(BLOCK_CLOSE)));
1505+
15031506
if (node.fallback) {
1504-
const fallback_stmts = create_block(node, node.fallback.nodes, context);
1505-
fallback_stmts.unshift(
1506-
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal('<!ssr:each_else>')))
1507+
const fallback = create_block(node, node.fallback.nodes, context);
1508+
1509+
fallback.push(
1510+
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(`<!--${HYDRATION_END}!-->`)))
15071511
);
1512+
15081513
state.template.push(
15091514
t_statement(
15101515
b.if(
15111516
b.binary('!==', b.member(array_id, b.id('length')), b.literal(0)),
1512-
for_loop,
1513-
b.block(fallback_stmts)
1517+
b.block([for_loop, close]),
1518+
b.block(fallback)
15141519
)
15151520
)
15161521
);
15171522
} else {
1518-
state.template.push(t_statement(for_loop));
1523+
state.template.push(t_statement(for_loop), t_statement(close));
15191524
}
1520-
state.template.push(block_close);
15211525
},
15221526
IfBlock(node, context) {
15231527
const state = context.state;

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
EACH_IS_STRICT_EQUALS,
66
EACH_ITEM_REACTIVE,
77
EACH_KEYED,
8+
HYDRATION_END,
89
HYDRATION_START
910
} from '../../../../constants.js';
1011
import { hydrate_anchor, hydrate_nodes, hydrating, set_hydrating } from '../hydration.js';
@@ -97,16 +98,13 @@ function each(anchor, flags, get_collection, get_key, render_fn, fallback_fn, re
9798
let mismatch = false;
9899

99100
if (hydrating) {
100-
var is_else = /** @type {Comment} */ (hydrate_nodes?.[0])?.data === 'ssr:each_else';
101+
var is_else = /** @type {Comment} */ (anchor).data === `${HYDRATION_END}!`;
101102

102103
if (is_else !== (length === 0)) {
103104
// hydration mismatch — remove the server-rendered DOM and start over
104105
remove(hydrate_nodes);
105106
set_hydrating(false);
106107
mismatch = true;
107-
} else if (is_else) {
108-
// Remove the each_else comment node or else it will confuse the subsequent hydration algorithm
109-
/** @type {import('#client').TemplateNode[]} */ (hydrate_nodes).shift();
110108
}
111109
}
112110

packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export default function Each_string_template($$payload, $$props) {
1717
$$payload.out += "<!--]-->";
1818
}
1919

20-
$$payload.out += `<!--]-->`;
20+
$$payload.out += "<!--]-->";
2121
$.pop();
2222
}

0 commit comments

Comments
 (0)