Replies: 4 comments 9 replies
-
Is it possible to get this working in some way with target-gating, such that the SSR functions are only enabled on the server-side (unless some special feature is set to enable them in the browser too) etc? We would still need feature flags or similar I would think, but using Rust's native system in this way would be a little cleaner. I'm not familiar with this part of the Sycamore code though, so I've got no clue if this is possible. I'm just going off the success I've had with target-gating basically the entire Perseus codebase, which has worked very well overall! |
Beta Was this translation helpful? Give feedback.
-
These are not important details. What (i) really miss in sycamore is this: |
Beta Was this translation helpful? Give feedback.
-
How does Dioxus currently do it? I seem to remember they just have an Element that gets returned with no generics |
Beta Was this translation helpful? Give feedback.
-
One thing that would be really nice is to have Sycamore support using |
Beta Was this translation helpful? Give feedback.
-
Ever since SSR has been introduced in Sycamore, one thing that has been bugging me is the
<G: Html>
generics on every single component. This is because the same component code needs to run in two different environments, the first one being the browser and using the DOM, the other being on the server and using a hand-rolled DOM mock specifically designed for SSR.Current solution
The way we've been solving this issue of having two different environments is abstracting them with the
GenericNode
andHtml
traits and making everything generic over them.This means that all the components need to have the
<G: GenericNode>
or<G: Html>
bound on them so that they work with both the real DOM and the mocked DOM on the server.Advantages:
innerHTML
already anyways).Disadvantages:
<G: Html>
bound on functions that don't actually render anything, e.g. to access theG::IS_BROWSER
constant.Alternative 1: Feature flags
This is probably the most obvious solution. The idea is to have a
ssr
feature and adom
(and perhaps adom-hydrate
) feature to choose which rendering backend to use. Depending on which feature is activated, you can either render to a string, render to the DOM, or hydrate the existing DOM.However, there are a few problems with this approach. Cargo feature flags are not designed to be mutually exclusive. What should we do if multiple features are enabled? For example, if both
ssr
anddom
are set, should we dodom
by default? or maybe create a compiler error? In any case, this could get pretty complicated, pretty fast.Advantages:
Disadvantages:
Alternative 2: Env variables
Pretty similar to feature flag but this time using env variables instead. We could have an env variable called
SYCAMORE_WEB_BACKEND
which could have the values ofdom
,ssr
, ordom-hydrate
and fallback todom
if none is set.Advantages:
Disadvantages:
Alternative 3: Custom
--cfg
flagAlso similar to feature flags but using a custom
--cfg
flag passed via the command line torustc
. Essentially the same advantages and disadvantages compared to env variables because we will need to pass the--cfg
via the command line anyways, except that its less unexpected when codegen changes depending on this flag.Alternative 4: Runtime selection
Instead of choosing backend at compile-time, we could do it at runtime. To prevent shipping the SSR code to the browser when it is not needed, we could add feature flags as well, but this would simply activate/deactivate support at runtime rather than error out when multiple of them are activated.
We could, for example, define a data structure like so and implement all the methods of
GenericNode
on it.The issue would be that we will rely on compiler optimizations to strip the ssr code out when both are enabled but only one is used.
Another issue would be how to select which implementation to use at runtime. One simple way of doing this is to just add a new context value to the reactive scope that tells us which backend to use.
Advantages:
Disadvantages:
NodeWrapper
and instead provide their ownView
type instead.54 votes ·
Beta Was this translation helpful? Give feedback.
All reactions