diff --git a/.changeset/hip-bats-flash.md b/.changeset/hip-bats-flash.md new file mode 100644 index 0000000000..7a005dd370 --- /dev/null +++ b/.changeset/hip-bats-flash.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure a batch exists when a boundary resolves synchronously diff --git a/packages/svelte/src/internal/client/dom/blocks/boundary.js b/packages/svelte/src/internal/client/dom/blocks/boundary.js index 4f655a3695..0ff499f387 100644 --- a/packages/svelte/src/internal/client/dom/blocks/boundary.js +++ b/packages/svelte/src/internal/client/dom/blocks/boundary.js @@ -286,7 +286,7 @@ export class Boundary { this.#pending_effect = null; }); - this.#resolve(/** @type {Batch} */ (current_batch)); + this.#resolve(Batch.ensure()); } }); } @@ -308,7 +308,10 @@ export class Boundary { const pending = /** @type {(anchor: Node) => void} */ (this.#props.pending); this.#pending_effect = branch(() => pending(this.#anchor)); } else { - this.#resolve(/** @type {Batch} */ (current_batch)); + // `current_batch` can be null here — e.g. a `flushSync()` at the top + // level of a component script flushes and clears the batch while + // we are still rendering (#18522) — so ensure one exists + this.#resolve(Batch.ensure()); } } catch (error) { this.error(error); diff --git a/packages/svelte/tests/runtime-runes/samples/flush-sync-component-init/_config.js b/packages/svelte/tests/runtime-runes/samples/flush-sync-component-init/_config.js new file mode 100644 index 0000000000..0054b8993c --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/flush-sync-component-init/_config.js @@ -0,0 +1,6 @@ +import { test } from '../../test'; + +// https://github.com/sveltejs/svelte/issues/18522 +export default test({ + html: `

Hello!

` +}); diff --git a/packages/svelte/tests/runtime-runes/samples/flush-sync-component-init/main.svelte b/packages/svelte/tests/runtime-runes/samples/flush-sync-component-init/main.svelte new file mode 100644 index 0000000000..523d72ae69 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/flush-sync-component-init/main.svelte @@ -0,0 +1,7 @@ + + +

Hello!