Skip to content
Open
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/chilled-donkeys-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: fixed component lifecycle timing issue in #key blocks. its now properly cleaned up before new instance are created.
13 changes: 8 additions & 5 deletions packages/svelte/src/internal/client/dom/blocks/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function key(node, get_key, render_fn) {
/** @type {V | typeof UNINITIALIZED} */
var key = UNINITIALIZED;

/** @type {Effect} */
/** @type {Effect | null} */
var effect;

/** @type {Effect} */
Expand All @@ -37,10 +37,6 @@ export function key(node, get_key, render_fn) {
var changed = is_runes() ? not_equal : safe_not_equal;

function commit() {
if (effect) {
pause_effect(effect);
}

if (offscreen_fragment !== null) {
// remove the anchor
/** @type {Text} */ (offscreen_fragment.lastChild).remove();
Expand All @@ -54,6 +50,12 @@ export function key(node, get_key, render_fn) {

block(() => {
if (changed(key, (key = get_key()))) {
// Pause and cleanup the old effect before creating a new one
// This ensures proper component lifecycle ordering (destroy -> create -> mount)
if (effect) {
pause_effect(effect);
effect = null;
}
var target = anchor;

var defer = should_defer_append();
Expand All @@ -63,6 +65,7 @@ export function key(node, get_key, render_fn) {
offscreen_fragment.append((target = create_text()));
}

// Create the new effect with the component
pending_effect = branch(() => render_fn(target));

if (defer) {
Expand Down
Loading