-
Notifications
You must be signed in to change notification settings - Fork 75
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
Component hydration #34
Comments
I just learned about this project so taking a stab: In both React and crank components hold state and are impure - in crank that state is managed by the language itself (through generators) rather than reinventing the wheel (through array pushing and popping in hooks or through instance state with "fake classes" in classes). Holding the state as local variables means you can use tooling the language makes available to you instead of rolling something "specific to SSR" kind of how async functions with MobX are easier than Redux because they leverage language tooling. This means SSR is as simple as just calling Taking the code example of an async component:
In order to SSR this - you just run it as is (with isomorphic-fetch) or if you want to cache the result or otherwise treat it differently on the server - you just inject the server parts and mock them on the server side. |
That is, even in regular react SSR is slightly different because components are "less stateful", managing that state in regular JavaScript and just discarding the generators makes things easier. |
@benjamingr |
Oh I see what you're talking about now - that depends on how the stores and state is implemented. I'm not sure if the framework already supports hydration - but hydration would require "cooperation" from the stores in order to "progress the generator automatically" on the client. |
Cross-quoting from Reddit:
—Brian Kim Some responses:
To which he said "excellent points" roughly. I am not sure that "blow it all away" would not work, however, if the SSR'd rendering framework requested an initial holding state for animations and video, and then loaded in the actual animation/video when it threw the explosives... essentially demanding a clear landing site for the app's dynamic components. Bad for certain kind of pages, ofc, but those pages are often the ones which are most dependent on JS in the first place, so the gains of SSR with hydration (providing a static webpage quickly using a dynamic framework, then enabling a swift transition using the app's diffing powers) were already low. |
Blowing away the DOM may work from a technical point of view but is a pretty jarring user experience. It will nearly always lead to white disrupting flash when the DOM is destroyed and build up again. |
To add two more arguments to why I think @marvinhagemeister is right on saying that this will led to a jarring user experience without hydrating:
If the issue goes down to user experience, this is a must that has to be taken into account, otherwise crank will be unlikely to be a choice for anything that needs ssr. To the point from @yurynix of how to forward the generators to the right step, this can be let to be the responsibility of the architecture of the app, and the libraries that hold the serialized state from the server. For example if you are driving both the ssr and client renders based on fetch state, you can:
|
This was implemented in 0.5. |
(First of all, thanks for the great work on making this!)
I'm fascinated by the approach crank takes of modeling component states as local variables. How does it handle when a component instance needs to be discarded and re-created with states intact?
In a hypothetical server-side rendering scenario, the server would create the initial render and send the rendered HTML and component states to the client. The client replicates the initial render by creating new component instances and injecting component states. In both class-style and hook-style React, this is easy because class properties can be re-assigned and useState() acts as a DI mechanism. However, in crank components are impure and may have local variables, so the component states are implicit and harder to track by the framework.
Have you thought about how crank would handle this?
The text was updated successfully, but these errors were encountered: