Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-trainers-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: don't destroy contents of `svelte:boundary` unless the boundary is an error boundary
12 changes: 6 additions & 6 deletions packages/svelte/src/internal/client/dom/blocks/boundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -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