Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
`const x = $derived(...)` in `<script module>` does not reevaluate in SSR | GoodFirstPicks

`const x = $derived(...)` in `<script module>` does not reevaluate in SSR

sveltejs/svelte 8 comments 1mo ago
View on GitHub
mediumopenScope: somewhat clearSkill match: maybeSvelteTypeScript

Why this is a good first issue

SSR behavior of $derived in module scripts needs clarification or warning.

AI Summary

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.

Issue Description

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).

Description

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!

Solution

I moved the const x = $derived(...) into the normal <script>, this fixed the problem.

Thinking

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!

Request

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 Info

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

Severity

annoyance

GitHub Labels

needs discussion

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!

Risk Flags

  • SSR behavior
  • framework-specific knowledge
Loading labels...

Details

Points10 pts
Difficultymedium
Scopesomewhat clear
Skill Matchmaybe
Test Focusedno