Skip to content

$derived not updated on first read if source is async store #15525

@mcous

Description

@mcous

Describe the bug

If a $derived rune derives its value from a asynchronous store (i.e. one that uses StartStopNotifier), its value is not updated on first read.

We ran into this extremely fun issue when upgrading a stores-based state structure to runes. One of the sources to a $derived value is a @tanstack/svelte-query store that updates asynchronously. The $derived value is not accessed directly in the template, but is accessed in a callback triggered on user click. When the callback is triggered, the $derived value is stale and does not recalculate, even though every source value before the $derived is up to date.

In our case, this issue only manifested in the prod build - not the dev build - but I was able to boil it down to a reproduction that occurs in dev, linked below. In our actual code, the rune is part of an object attached to a context, which for some reason is enough to get this working on our dev server.

Reproduction

https://svelte.dev/playground/31db259839d54528a3323b9987bce475?version=5.23.0

System Info

System:
  OS: macOS 14.3.1
  CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 516.24 MB / 32.00 GB
  Shell: 5.9 - /usr/local/bin/zsh
Binaries:
  Node: 22.14.0 - ~/.nvs/default/bin/node
  npm: 10.9.2 - ~/.nvs/default/bin/npm
  pnpm: 9.15.5 - ~/.nvs/default/bin/pnpm
Browsers:
  Chrome: 123.0.6312.86
  Safari: 17.3.1
npmPackages:
  svelte: ^5.20.2 => 5.23.0

Severity

blocking an upgrade

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions