fix: re-run non-render-bound deriveds on the server#17674
Conversation
🦋 Changeset detectedLatest commit: 9124349 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
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); |
There was a problem hiding this comment.
so this basically means that the value will run over and over on the server? FWIW I think that's fine
There was a problem hiding this comment.
exactly, yeah. the alternative would basically be to implement full reactivity, which seems extravagant and unnecessary
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) |
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>
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...
...
thingwill 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
mainthese 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.
Before submitting the PR, please make sure you do the following
feat:,fix:,chore:, ordocs:.packages/svelte/src, add a changeset (npx changeset).Tests and linting
pnpm testand lint the project withpnpm lint