Fix/@sveltejs/svelte/16114
#16263
Closing issue
Describe the bug
Not sure if this is a bug or intended. According to this breaking change, if the state caused an $effect to rerun, Svelte should provide the previous value of the state in the teardown function. This work for $state currently, but not for $derived, though it does work if $derived is reassigned.
Reproduction
Logs
System Info
Svelte playground (5.33.18)
Chrome 137.0.7151.56
Severity
annoyance
Pull request
Fixes: #16114
When a $state or a $derived is directly set by the user, we properly add it to the old_values Map.
However when a derived is updated as a result of the check_dirtiness check during flush_queued_effects, we do not save the old value, hence the value read in a teardown function of a $effect is not the old value but the new one, this is incosistent with the "old value behavior" that was recently introduced.
In this PR i save the old value when the $derived is updated, so that it will be used by teardown functions of $effects.
However while working on this i've noticed that there's an edge case when working with child components.
While passing a prop in certain conditions (either a legacy bindable or a prop accessed via $.prop because it has a setter somewhere in the code) the original $state is wrapped in a derived. When the child component is destroyed the teardown functions might read $.prop values that did not got update because thay were not read (perhaps in the template). This means that is possible that during the execution of the teardown the values of the derived are either null because they were never initialized because nothing ever read from it, or they are outdated. I've opened a separate issue for this: #16262.
This bug is present both before and after the changes of this PR.
Before submitting the PR, please make sure you do the following
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- Prefix your PR title with
feat:,fix:,chore:, ordocs:. - This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
- If this PR changes code within
packages/svelte/src, add a changeset (npx changeset).
Tests and linting
- Run the tests with
pnpm testand lint the project withpnpm lint
Info
🦋 Changeset detected
Latest commit: b213de2
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| svelte | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
pnpm add https://pkg.pr.new/svelte@16263Thank you. Unfortunately this isn't the right fix, since it depends on when the derived was last calculated — you can see that :
$effect teardown: 1 * 1 = 1
$effect teardown: 1 * 1 = null
$effect teardown: 2 * 2 = 4
$effect teardown: 3 * 3 = 4
$effect teardown: 4 * 4 = 16
$effect teardown: 5 * 5 = 16
$effect teardown: 6 * 6 = 36
$effect teardown: 7 * 7 = 36
In fact I think this is just #16262 in a slightly different form.
There's a mechanism in #15844 that would fix this — batch_deriveds — so what I think I'll do is separate that from the async logic to make it usable here.
I guess i can close this, thanks for fixing it!
Add tests for props old values in onDestroy hooks to prevent regressions
• Jun 29, 2025, 3:46 PM