fix: Don't destroy boundary contents on error unless the boundary is an error boundary (#16746)

* fix: Don't destroy contents of boundaries on errors if they're not error boundaries

* changeset

* test

* prettier

* prettier

* simplify test

* oops

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/16744/head
Elliott Johnson 4 weeks ago committed by GitHub
parent 68d27f1c4f
commit f09f25e6a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't destroy contents of `svelte:boundary` unless the boundary is an error boundary

@ -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 {

@ -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,
`
<button>throw</button>
<p>some content</p>
`
);
}
});

@ -0,0 +1,19 @@
<script lang="ts">
let should_throw = $state(false);
function throw_error() {
throw new Error('oops');
}
</script>
<button onclick={() => should_throw = true}>
throw
</button>
<svelte:boundary>
<p>some content</p>
{#if should_throw}
{throw_error()}
{/if}
</svelte:boundary>
Loading…
Cancel
Save