SSR behavior of $derived in module scripts needs clarification or warning.
The issue involves unexpected SSR behavior of `$derived` in module scripts, where values do not update correctly. A reproduction has been provided, and it relates to broader SSR concerns. The maintainer has linked it to an existing issue and pending PR, indicating this may require framework-level changes or warnings.
Exporing a derived directly from either .svelte.ts or .svelte > script module does warn (I guess because there is some reason why it does not work reliably).
I have created a const x = $derived(...) inside a components <script module> more or less by accident, I then used x inside the component markup. This led to a bug in my app, where during SSR the first initialized value was always being used!
I moved the const x = $derived(...) into the normal <script>, this fixed the problem.
However I have created a $derived in script modules before! Why? Because i wanted to use their value in snippets, that I then wanted to export from the script module.
Now I am not sure if this is even safe! It would be highly useful if it was!
Please decide on whether any of this should be safe to do, if what I experience is a bug or intended. Any I would highly recommend to introduce a warning if using a $derived declared inside <script module> is then used inside the component markup.
System:
OS: macOS 15.6.1
CPU: (20) x64 Intel(R) Xeon(R) W-2150B CPU @ 3.00GHz
Memory: 26.08 MB / 64.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 24.5.0 - /usr/local/bin/node
Yarn: 1.22.11 - /usr/local/bin/yarn
npm: 11.5.1 - /usr/local/bin/npm
pnpm: 10.13.1 - /usr/local/bin/pnpm
bun: 1.0.25 - /usr/local/bin/bun
Browsers:
Chrome: 139.0.7258.155
Safari: 18.6
npmPackages:
svelte: 5.37.3 => 5.37.3
annoyance
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!