Skip to content

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

@timephy

Description

@timephy

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions