Skip to content

Commit 8d13921

Browse files
authored
Revert "breaking: avoid flushing queued updates on mount/hydrate" (#12593)
* Revert "breaking: avoid flushing queued updates on mount/hydrate (#12587)" This reverts commit 20b8797. * Update packages/svelte/src/internal/client/render.js
1 parent c548932 commit 8d13921

File tree

7 files changed

+33
-40
lines changed

7 files changed

+33
-40
lines changed

.changeset/slow-gorillas-yawn.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export function set_text(text, value) {
8181
*/
8282
export function mount(component, options) {
8383
const anchor = options.anchor ?? options.target.appendChild(empty());
84-
return _mount(component, { ...options, anchor });
84+
// Don't flush previous effects to ensure order of outer effects stays consistent
85+
return flush_sync(() => _mount(component, { ...options, anchor }), false);
8586
}
8687

8788
/**
@@ -114,35 +115,38 @@ export function hydrate(component, options) {
114115
const previous_hydrate_node = hydrate_node;
115116

116117
try {
117-
var anchor = /** @type {TemplateNode} */ (target.firstChild);
118-
while (
119-
anchor &&
120-
(anchor.nodeType !== 8 || /** @type {Comment} */ (anchor).data !== HYDRATION_START)
121-
) {
122-
anchor = /** @type {TemplateNode} */ (anchor.nextSibling);
123-
}
118+
// Don't flush previous effects to ensure order of outer effects stays consistent
119+
return flush_sync(() => {
120+
var anchor = /** @type {TemplateNode} */ (target.firstChild);
121+
while (
122+
anchor &&
123+
(anchor.nodeType !== 8 || /** @type {Comment} */ (anchor).data !== HYDRATION_START)
124+
) {
125+
anchor = /** @type {TemplateNode} */ (anchor.nextSibling);
126+
}
124127

125-
if (!anchor) {
126-
throw HYDRATION_ERROR;
127-
}
128+
if (!anchor) {
129+
throw HYDRATION_ERROR;
130+
}
128131

129-
set_hydrating(true);
130-
set_hydrate_node(/** @type {Comment} */ (anchor));
131-
hydrate_next();
132+
set_hydrating(true);
133+
set_hydrate_node(/** @type {Comment} */ (anchor));
134+
hydrate_next();
132135

133-
const instance = _mount(component, { ...options, anchor });
136+
const instance = _mount(component, { ...options, anchor });
134137

135-
if (
136-
hydrate_node.nodeType !== 8 ||
137-
/** @type {Comment} */ (hydrate_node).data !== HYDRATION_END
138-
) {
139-
w.hydration_mismatch();
140-
throw HYDRATION_ERROR;
141-
}
138+
if (
139+
hydrate_node.nodeType !== 8 ||
140+
/** @type {Comment} */ (hydrate_node).data !== HYDRATION_END
141+
) {
142+
w.hydration_mismatch();
143+
throw HYDRATION_ERROR;
144+
}
142145

143-
set_hydrating(false);
146+
set_hydrating(false);
144147

145-
return /** @type {Exports} */ (instance);
148+
return instance;
149+
}, false);
146150
} catch (error) {
147151
if (error === HYDRATION_ERROR) {
148152
// TODO it's possible for event listeners to have been added and

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,10 @@ function process_effects(effect, collected_effects) {
671671
* Internal version of `flushSync` with the option to not flush previous effects.
672672
* Returns the result of the passed function, if given.
673673
* @param {() => any} [fn]
674+
* @param {boolean} [flush_previous]
674675
* @returns {any}
675676
*/
676-
export function flush_sync(fn) {
677+
export function flush_sync(fn, flush_previous = true) {
677678
var previous_scheduler_mode = current_scheduler_mode;
678679
var previous_queued_root_effects = current_queued_root_effects;
679680

@@ -687,7 +688,9 @@ export function flush_sync(fn) {
687688
current_queued_root_effects = root_effects;
688689
is_micro_task_queued = false;
689690

690-
flush_queued_root_effects(previous_queued_root_effects);
691+
if (flush_previous) {
692+
flush_queued_root_effects(previous_queued_root_effects);
693+
}
691694

692695
var result = fn?.();
693696

packages/svelte/tests/hydration/test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { suite, assert_ok, type BaseTest } from '../suite.js';
88
import { createClassComponent } from 'svelte/legacy';
99
import { render } from 'svelte/server';
1010
import type { CompileOptions } from '#compiler';
11-
import { flushSync } from 'svelte';
1211

1312
interface HydrationTest extends BaseTest {
1413
load_compiled?: boolean;
@@ -115,7 +114,6 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
115114

116115
if (!override) {
117116
const expected = read(`${cwd}/_expected.html`) ?? rendered.html;
118-
flushSync();
119117
assert.equal(target.innerHTML.trim(), expected.trim());
120118
}
121119

packages/svelte/tests/runtime-browser/driver.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import config from '__CONFIG__';
55
// @ts-expect-error
66
import * as assert from 'assert.js';
77
import { createClassComponent } from 'svelte/legacy';
8-
import { flushSync } from 'svelte';
98

109
/** @param {HTMLElement} target */
1110
export default async function (target) {
@@ -46,8 +45,6 @@ export default async function (target) {
4645
} while (new Date().getTime() <= start + ms);
4746
};
4847

49-
flushSync();
50-
5148
if (config.html) {
5249
assert.htmlEqual(target.innerHTML, config.html);
5350
}

packages/svelte/tests/runtime-runes/samples/hydrate-modified-input-group/_config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { flushSync } from 'svelte';
21
import { test } from '../../test';
32

43
export default test({
@@ -10,7 +9,6 @@ export default test({
109
inputs[1].dispatchEvent(new window.Event('change'));
1110
// Hydration shouldn't reset the value to 1
1211
hydrate();
13-
flushSync();
1412

1513
assert.htmlEqual(
1614
target.innerHTML,

packages/svelte/tests/runtime-runes/samples/hydrate-modified-input/_config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { flushSync } from 'svelte';
21
import { test } from '../../test';
32

43
export default test({
@@ -10,7 +9,6 @@ export default test({
109
input.dispatchEvent(new window.Event('input'));
1110
// Hydration shouldn't reset the value to empty
1211
hydrate();
13-
flushSync();
1412

1513
assert.htmlEqual(target.innerHTML, '<input type="text">\nfoo');
1614
}

0 commit comments

Comments
 (0)