Skip to content

Commit ba7c20b

Browse files
committed
remove <!--ssr:if:true--> comments
1 parent b6fab1c commit ba7c20b

File tree

4 files changed

+13
-27
lines changed

4 files changed

+13
-27
lines changed

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { binding_properties } from '../../bindings.js';
2626
import { regex_starts_with_newline, regex_whitespaces_strict } from '../../patterns.js';
2727
import { DOMBooleanAttributes, HYDRATION_END, HYDRATION_START } from '../../../../constants.js';
2828
import { sanitize_template_string } from '../../../utils/sanitize_template_string.js';
29+
import { BLOCK_CLOSE } from '../../../../internal/server/hydration.js';
2930

3031
export const block_open = t_string(`<!--${HYDRATION_START}-->`);
3132
export const block_close = t_string(`<!--${HYDRATION_END}-->`);
@@ -1522,32 +1523,23 @@ const template_visitors = {
15221523
const state = context.state;
15231524
state.template.push(block_open);
15241525

1525-
// Insert ssr:if:true/false anchors in addition to the other anchors so that
1526-
// the if block can catch hydration mismatches (false on the server, true on the client and vice versa)
1527-
// and continue hydration without having to re-render everything from scratch.
1528-
15291526
const consequent = create_block(node, node.consequent.nodes, context);
1530-
consequent.unshift(
1531-
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal('<!ssr:if:true>')))
1532-
);
1527+
const alternate = node.alternate ? create_block(node, node.alternate.nodes, context) : [];
15331528

1534-
const alternate = node.alternate
1535-
? /** @type {import('estree').BlockStatement} */ (context.visit(node.alternate))
1536-
: b.block([]);
1537-
alternate.body.unshift(
1538-
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal('<!ssr:if:false>')))
1529+
consequent.push(b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(BLOCK_CLOSE))));
1530+
alternate.push(
1531+
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(`<!--${HYDRATION_END}!-->`)))
15391532
);
15401533

15411534
state.template.push(
15421535
t_statement(
15431536
b.if(
15441537
/** @type {import('estree').Expression} */ (context.visit(node.test)),
1545-
b.block(/** @type {import('estree').Statement[]} */ (consequent)),
1546-
alternate
1538+
b.block(consequent),
1539+
b.block(alternate)
15471540
)
15481541
)
15491542
);
1550-
state.template.push(block_close);
15511543
},
15521544
AwaitBlock(node, context) {
15531545
const state = context.state;

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IS_ELSEIF } from '../../constants.js';
22
import { hydrate_nodes, hydrating, set_hydrating } from '../hydration.js';
33
import { remove } from '../reconciler.js';
44
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
5+
import { HYDRATION_END } from '../../../../constants.js';
56

67
/**
78
* @param {Comment} anchor
@@ -34,21 +35,14 @@ export function if_block(
3435
let mismatch = false;
3536

3637
if (hydrating) {
37-
const comment_text = /** @type {Comment} */ (hydrate_nodes?.[0])?.data;
38+
const is_else = anchor.data === `${HYDRATION_END}!`;
3839

39-
if (
40-
!comment_text ||
41-
(comment_text === 'ssr:if:true' && !condition) ||
42-
(comment_text === 'ssr:if:false' && condition)
43-
) {
40+
if (condition === is_else) {
4441
// Hydration mismatch: remove everything inside the anchor and start fresh.
45-
// This could happen using when `{#if browser} .. {/if}` in SvelteKit.
42+
// This could happen with `{#if browser}...{/if}`, for example
4643
remove(hydrate_nodes);
4744
set_hydrating(false);
4845
mismatch = true;
49-
} else {
50-
// Remove the ssr:if comment node or else it will confuse the subsequent hydration algorithm
51-
hydrate_nodes.shift();
5246
}
5347
}
5448

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function hydrate_anchor(node) {
5353

5454
if (data === HYDRATION_START) {
5555
depth += 1;
56-
} else if (data === HYDRATION_END) {
56+
} else if (data[0] === HYDRATION_END) {
5757
if (depth === 0) {
5858
hydrate_nodes = /** @type {import('#client').TemplateNode[]} */ (nodes);
5959
return current;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<!-- unrelated comment -->
2-
<!--[--><!--[--><!--ssr:if:true-->hello<!--]--><!--]-->
2+
<!--[--><!--[-->hello<!--]--><!--]-->

0 commit comments

Comments
 (0)