Skip to content

Commit cc15ffb

Browse files
committed
fix hydration
1 parent f3fc443 commit cc15ffb

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
pause_effect,
88
resume_effect
99
} from '../../reactivity/effects.js';
10+
import { hydrate_node, hydrating } from '../hydration.js';
1011
import { create_text, should_defer_append } from '../operations.js';
1112

1213
/**
@@ -18,7 +19,7 @@ import { create_text, should_defer_append } from '../operations.js';
1819
*/
1920
export class BranchManager {
2021
/** @type {TemplateNode} */
21-
#anchor;
22+
anchor;
2223

2324
/** @type {Map<Batch, Key>} */
2425
#batches = new Map();
@@ -34,7 +35,7 @@ export class BranchManager {
3435
* @param {TemplateNode} anchor
3536
*/
3637
constructor(anchor) {
37-
this.#anchor = anchor;
38+
this.anchor = anchor;
3839
}
3940

4041
#commit = () => {
@@ -58,7 +59,7 @@ export class BranchManager {
5859
/** @type {TemplateNode} */ (offscreen.fragment.lastChild).remove();
5960

6061
// ...and append the fragment
61-
this.#anchor.before(offscreen.fragment);
62+
this.anchor.before(offscreen.fragment);
6263
}
6364

6465
this.#batches.delete(batch);
@@ -132,6 +133,10 @@ export class BranchManager {
132133

133134
batch.add_callback(this.#commit);
134135
} else {
136+
if (hydrating) {
137+
this.anchor = hydrate_node;
138+
}
139+
135140
this.#commit();
136141
}
137142
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ export function if_block(node, fn, elseif = false) {
2929

3030
var anchor = node;
3131

32-
/** @type {typeof UNINITIALIZED | boolean | null} */
33-
var condition = UNINITIALIZED;
34-
3532
var flags = elseif ? EFFECT_TRANSPARENT : 0;
3633

3734
var has_branch = false;
@@ -44,12 +41,9 @@ export function if_block(node, fn, elseif = false) {
4441
var branches = new BranchManager(anchor);
4542

4643
const update_branch = (
47-
/** @type {boolean | null} */ new_condition,
44+
/** @type {boolean} */ condition,
4845
/** @type {null | ((anchor: Node) => void)} */ fn
4946
) => {
50-
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
51-
let mismatch = false;
52-
5347
if (hydrating) {
5448
const is_else = read_hydration_instruction(anchor) === HYDRATION_START_ELSE;
5549

@@ -59,24 +53,24 @@ export function if_block(node, fn, elseif = false) {
5953
anchor = skip_nodes();
6054

6155
set_hydrate_node(anchor);
56+
branches.anchor = anchor;
57+
6258
set_hydrating(false);
63-
mismatch = true;
59+
branches.ensure(condition, fn ?? noop);
60+
set_hydrating(true);
61+
62+
return;
6463
}
6564
}
6665

67-
branches.ensure(new_condition, fn ?? noop);
68-
69-
if (mismatch) {
70-
// continue in hydration mode
71-
set_hydrating(true);
72-
}
66+
branches.ensure(condition, fn ?? noop);
7367
};
7468

7569
block(() => {
7670
has_branch = false;
7771
fn(set_branch);
7872
if (!has_branch) {
79-
update_branch(null, null);
73+
update_branch(false, null);
8074
}
8175
}, flags);
8276

0 commit comments

Comments
 (0)