-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes done by setting store value in context of child component is not propagated back to parent during SSR rendering #8282
Comments
This has come up before and is the expected behavior. SSR constructs a string as it goes along. It does not construct an entire document tree and wait for everything to settle and then serialize it. This lets SSR run much faster, but has limitations like the one you've noted here. For this reason, it's recommended generally to have state passed down into components, not up. |
While I do understand that part, how exactly would you solve these cases where using components rather than passing objects gives a much better developer ux? I believe there is something to stop and undo, which is triggered when components causes errors, but I assume that happens at a Layout boundary then, and is not something general, or maybe I'm remembering that part wrong also. Also the svelte docs mentions that using context + stores this way is supported as I read it, if you want some reactivity support. Either way, thank you for the quick answer, I'll do a workaround instead :) |
I know this was closed as not planned, but this drove me nuts today. The docs should at least make a note of this, as passing stores via parent component context is the recommended way to use stores with SSR safely right here: https://kit.svelte.dev/docs/state-management#using-stores-with-context |
Further discussion can be found in this issue sveltejs#8282
Describe the bug
If you have a pair of components, where one is intended to be used in slots of the parent components, and the components communicate by using a store in the context, then updates done to the store by the child component is not propagated back to the parent component during SSR, but only during client side rendering.
Concretely I'm using this to render a semi-dynamic table. When the client is doing the rendering, the table looks like this:
However, when done serverside, the output looks like this:
Once the client rendering kicks in, the table does update and renders correctly. However, it still causes a bit of a "flash" when things are corrected
One important things to note, the issue only occurs if the location of the child element in the DOM is after the read of the store. If the store is read after the child component has initialized, then the issue does not occur. That trick however cannot be used to solve my table case above, since the child component results in some styling that needs to change on the container element, and thus the child can only run after the parent.
Reproduction
The reproduction here is relatively minimal to keep things simple and illustrate the issue.
Stackblitz: https://stackblitz.com/edit/sveltejs-kit-template-default-wanpgy?file=src/lib/ChildComponent.svelte
The essential components are the
ChildComponent
and theParentComponent
. The+page.svelte
only handles "invoking" the components.ParentComponent.svelte:
ChildComponent.svelte:
Logs
No logs here, since this isn't an explicit error.
System Info
Severity
serious, but I can work around it
Additional Information
Recording of the issue:
https://user-images.githubusercontent.com/1499810/209962702-1a83deaa-54a1-4439-9517-729bbb9926eb.mp4
You have to watch pretty carefully, since it only flashes the wrong text for a couple of ms.
The text was updated successfully, but these errors were encountered: