diff --git a/.changeset/tasty-trainers-sell.md b/.changeset/tasty-trainers-sell.md new file mode 100644 index 0000000000..4a2dd09fa6 --- /dev/null +++ b/.changeset/tasty-trainers-sell.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't destroy contents of `svelte:boundary` unless the boundary is an error boundary diff --git a/packages/svelte/src/internal/client/dom/blocks/boundary.js b/packages/svelte/src/internal/client/dom/blocks/boundary.js index b7f1803782..d4582024f7 100644 --- a/packages/svelte/src/internal/client/dom/blocks/boundary.js +++ b/packages/svelte/src/internal/client/dom/blocks/boundary.js @@ -285,6 +285,12 @@ export class Boundary { var onerror = this.#props.onerror; let failed = this.#props.failed; + // If we have nothing to capture the error, or if we hit an error while + // rendering the fallback, re-throw for another boundary to handle + if (this.#is_creating_fallback || (!onerror && !failed)) { + throw error; + } + if (this.#main_effect) { destroy_effect(this.#main_effect); this.#main_effect = null; @@ -346,12 +352,6 @@ export class Boundary { } }; - // If we have nothing to capture the error, or if we hit an error while - // rendering the fallback, re-throw for another boundary to handle - if (this.#is_creating_fallback || (!onerror && !failed)) { - throw error; - } - var previous_reaction = active_reaction; try { diff --git a/packages/svelte/tests/runtime-runes/samples/non-error-boundary-preserve-on-error/_config.js b/packages/svelte/tests/runtime-runes/samples/non-error-boundary-preserve-on-error/_config.js new file mode 100644 index 0000000000..7654cf1360 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/non-error-boundary-preserve-on-error/_config.js @@ -0,0 +1,20 @@ +import { test } from '../../test'; +import { flushSync } from 'svelte'; + +export default test({ + test({ assert, target }) { + const [button] = target.querySelectorAll('button'); + + assert.throws(() => { + flushSync(() => button.click()); + }, /oops/); + + assert.htmlEqual( + target.innerHTML, + ` + +

some content

+ ` + ); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/non-error-boundary-preserve-on-error/main.svelte b/packages/svelte/tests/runtime-runes/samples/non-error-boundary-preserve-on-error/main.svelte new file mode 100644 index 0000000000..1c3f062b43 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/non-error-boundary-preserve-on-error/main.svelte @@ -0,0 +1,19 @@ + + + + + +

some content

+ + {#if should_throw} + {throw_error()} + {/if} +