Skip to content

fix: don't eagerly execute deriveds on resume #16150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 14, 2025
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/poor-pumpkins-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: don't eagerly execute deriveds on resume
13 changes: 5 additions & 8 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ export function render_effect(fn) {
/**
* @param {(...expressions: any) => void | (() => void)} fn
* @param {Array<() => any>} thunks
* @param {<T>(fn: () => T) => Derived<T>} d
* @returns {Effect}
*/
export function template_effect(fn, thunks = [], d = derived) {
Expand Down Expand Up @@ -598,15 +599,11 @@ function resume_children(effect, local) {
if ((effect.f & INERT) === 0) return;
effect.f ^= INERT;

// Ensure the effect is marked as clean again so that any dirty child
// effects can schedule themselves for execution
if ((effect.f & CLEAN) === 0) {
effect.f ^= CLEAN;
}

// If a dependency of this effect changed while it was paused,
// schedule the effect to update
if (check_dirtiness(effect)) {
// schedule the effect to update. we don't use `check_dirtiness`
// here because we don't want to eagerly recompute a derived like
// `{#if foo}{foo.bar()}{/if}` if `foo` is now `undefined
if ((effect.f & CLEAN) !== 0) {
set_signal_status(effect, DIRTY);
schedule_effect(effect);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let { value } = $props()

const text = $derived(value.toString())

$effect(() => console.log(text))
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target, logs }) {
const [btn1, btn2] = target.querySelectorAll('button');
const [div] = target.querySelectorAll('div');

flushSync(() => btn1?.click());
assert.htmlEqual(div.innerHTML, '123 123');
assert.equal(div.inert, true);

flushSync(() => btn2?.click());
assert.htmlEqual(div.innerHTML, '');
assert.deepEqual(logs, ['123']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import Component from './Component.svelte';

let outer = $state(true);
let inner = $state(123);

function outro() {
return { duration: 100 };
}
</script>

{#if outer}
<div out:outro>
{#if inner}
{@const text = inner.toString()}
{text} {inner.toString()}
<Component value={inner} />
{/if}
</div>
{/if}

<button onclick={() => { outer = false; inner = undefined; }}>Set both to falsy</button>
<button onclick={() => { outer = true }}>Set outer to truthy</button>