8000 Private `$derived` class fields break SSR · Issue #15960 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

Private $derived class fields break SSR #15960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huntabyte opened this issue May 19, 2025 · 0 comments · Fixed by #15964
Closed

Private $derived class fields break SSR #15960

huntabyte opened this issue May 19, 2025 · 0 comments · Fixed by #15964

Comments

@huntabyte
Copy link
Member
huntabyte commented May 19, 2025

Describe the bug

On initial load/SSR, private class fields that use $derived break with the following:
TypeError: Cannot read properties of undefined (reading '<var name>')

Introduced in svelte@5.31.0

Reproduction

<script lang="ts">
    class SomeState {
        opts: { baz: boolean };
        #foo = $derived.by(() => this.opts.baz);

        constructor(opts: { baz: boolean }) {
            this.opts = opts;
        }
    }

    new SomeState({ baz: true });
</script>

Stackblitz: https://stackblitz.com/github/AdrianGonz97/dependent-on-private-derived-repro?file=README.md
GitHub Repo: https://github.com/AdrianGonz97/dependent-on-private-derived-repro

Logs

System Info

(StackBlitz)


  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    svelte: ^5.0.0 => 5.31.1

Severity

blocking an upgrade

@huntabyte huntabyte changed the title Depending on private $derived in classes breaks on SSR Private $derived class fields break SSR May 20, 2025
dummdidumm added a commit that referenced this issue May 20, 2025
Fixes a regression introduced in #15820: deriveds need to be lazily called on the server, too, since they can close over variables only later defined

Fixes #15960
n00ki added a commit to n00ki/sveltekit-omakase that referenced this issue May 22, 2025
Rich-Harris pushed a commit that referenced this issue May 22, 2025
* fix: make deriveds on the server lazy again

Fixes a regression introduced in #15820: deriveds need to be lazily called on the server, too, since they can close over variables only later defined

Fixes #15960

* fix: handle basic assignment of deriveds on the server

* fix: use `build_assignment_value` for deriveds assignments

* use once

* allow writing to public deriveds on server

---------

Co-authored-by: paoloricciuti <ricciutipaolo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant
0