Replies: 1 comment 2 replies
-
Preserving state is difficult because we leverage actual DOM nodes rather than creating a separate virtual DOM with all state/props. The HTML string output from SSR will only contain attributes so if you're able to encode all needed data there, it could work. But any non-attribute bindings will be lost and you need to hydrate them. Even in regular SSR use case we recommend that the same template and data be shipped to the client for hydrating like in the small code snippet here: https://github.com/lit/lit/tree/main/packages/labs/ssr#server-only-templates While serializing template result is another possibility, with a chance of preserving other bindings, you'd need the values to also be serializable. I'm not sure how these would work with things like directives and event bindings certainly aren't serializable as they're functions. There might be interesting optimizations possible if the template is already present in the client and the server could just provide the Partial server render and update is indeed an interesting pursuit but it would require a lot of care and coordination. Also looking at your raw html example, would nested custom elements need to be server rendered with declarative shadow dom? Then |
Beta Was this translation helpful? Give feedback.
-
Hi,
I implemented ssr using lit-ssr in my project and I would like the client to be able to receive server updates in the form of an html template that I can use in my page component's render function as the result of a POST request, similar to Next.js's server actions and revalidatePath.
I tried to send both raw html and pre-rendered html template (the json output of the html function) but i get various errors and issues.
Raw HTML:
existing html in page component
The problem of
unsafeHTML
is that we loose the state contained in the custom elements such as my-post. The element is basically destroyed and recreated, which is absolutely not a possibility for my use case.JSON template:
Instead of sending raw html we can also run
Page.prototype.render()
on the server to basically return the JSON output of the render function.With this technique, I am able to basically the get same result as unsafeHTML
however with the same issue, the state of is not not maintained and the whole html tree is replaced.
Is there any way I could patch the template as if the server update was rendered on the client?
Beta Was this translation helpful? Give feedback.
All reactions