Skip to content

fix: re-run non-render-bound deriveds on the server#17674

Merged
Rich-Harris merged 16 commits intomainfrom
thunkify-deriveds
Feb 18, 2026
Merged

fix: re-run non-render-bound deriveds on the server#17674
Rich-Harris merged 16 commits intomainfrom
thunkify-deriveds

Conversation

@Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Feb 11, 2026

I tried to bring #14977 up-to-date but it's slipped too far. Also, I wanted to try a slightly different approach.

In this PR, deriveds are memoized if they're created during render — in other words if you have something like this...

<script>
  let thing = $derived(expensivelyComputeThing());
</script>

...thing will only be computed once. This seems correct since the inputs should never change during render.

For deriveds created outside render, we re-run the derived each time it is accessed, which fixes #14954. This way, there's still some overhead compared to how deriveds work in the browser (where they only recompute when their dependencies have changed), but only in the rare places where it is necessary.

There is one wrinkle: writable deriveds. On main these are just regular old variables, which means they can be written to during render. This PR currently preserves that behaviour, but I'm not sure it's desirable. It prevents the values of non-render-bound deriveds from ever updating, and makes no sense in the context of render-bound deriveds since they shouldn't be changing during render anyway. So my preference would be to disallow writes to deriveds on the server, but I'm not sure if we would need to consider that a breaking change.

Draft because of that question, and also because I think we might be able to tidy up some stuff around class fields.

  • figure out if we can delete some existing code around derived class fields
  • figure out what to do about writable deriveds
  • add a test

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot
Copy link

changeset-bot bot commented Feb 11, 2026

🦋 Changeset detected

Latest commit: 9124349

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris Rich-Harris temporarily deployed to Publish pkg.pr.new (maintainers) February 11, 2026 03:34 — with GitHub Actions Inactive
@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@17674

@Rich-Harris Rich-Harris temporarily deployed to Publish pkg.pr.new (maintainers) February 11, 2026 17:35 — with GitHub Actions Inactive
@Rich-Harris Rich-Harris temporarily deployed to Publish pkg.pr.new (maintainers) February 11, 2026 17:50 — with GitHub Actions Inactive
@Rich-Harris Rich-Harris marked this pull request as ready for review February 11, 2026 17:51
@Rich-Harris Rich-Harris temporarily deployed to Publish pkg.pr.new (maintainers) February 11, 2026 17:52 — with GitHub Actions Inactive
@dummdidumm
Copy link
Member

Draft because of that question

It's no longer in draft, so what was the conclusion?

*/
// deriveds created during render are memoized,
// deriveds created outside (e.g. SvelteKit `page` stuff) are not
const get_value = ssr_context === null ? fn : once(fn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this basically means that the value will run over and over on the server? FWIW I think that's fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, yeah. the alternative would basically be to implement full reactivity, which seems extravagant and unnecessary

@Rich-Harris
Copy link
Member Author

what was the conclusion?

that the path of least resistance was to maintain the existing behaviour, even though you can break things if you try hard enough (by writing to a derived that should later be updated via its dependencies)

@Rich-Harris Rich-Harris merged commit 09c4cb5 into main Feb 18, 2026
21 checks passed
@Rich-Harris Rich-Harris deleted the thunkify-deriveds branch February 18, 2026 20:56
@github-actions github-actions bot mentioned this pull request Feb 18, 2026
Rich-Harris pushed a commit that referenced this pull request Feb 18, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## svelte@5.52.0

### Minor Changes

- feat: support TrustedHTML in `{@html}` expressions
([#17701](#17701))

### Patch Changes

- fix: repair dynamic component truthy/falsy hydration mismatches
([#17737](#17737))

- fix: re-run non-render-bound deriveds on the server
([#17674](#17674))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.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 this pull request may close these issues.

page.params from $app/state not a rune server-side

2 participants