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

Open
huntabyte opened this issue May 19, 2025 · 0 comments · May be fixed by #15964
Open

Private $derived class fields break SSR #15960

huntabyte opened this issue May 19, 2025 · 0 comments · May be 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 [email protected]

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
@dummdidumm dummdidumm linked a pull request May 20, 2025 that will close this issue
6 tasks
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