SSR rehydrate
See original GitHub issueThis is a question.
In React 16, when using SSR, in the client-side code, we need to use hydrate
instead of render
so that React only registers event handler. I guess that React does this by comparing the checksum between server-side rendered html and supposed-to-be client-side rendered html.
While playing with Preact and attempting to perform SSR, I notice that at first my component was rendered twice. The rendering code is
// Server code
app.get("/", (req, res) => {
const html = renderToString(<App />)
res.render('index', { html })
})
// Client code
const root = document.getElementById("root")
render(<App />, root)
and the resulting html is
...
<div id="root">
<div>This is my app.</div>
<div>This is my app.</div>
</div>
...
When I change the client code to be
// Client code
const root = document.getElementById("root")
render(<App />, document.body, root)
The rendered html appears to be correct
...
<div id="root">
<div>This is my app.</div>
</div>
...
but I wonder if it was the original html with preact event all hooked up, or if the content was replaced with exactly the same content.
Please help me shed some light on this.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Understanding Hydration in React applications(SSR)
Hydration is used only with server-side rendering (SSR). It is a process of rendering our components and attaching event handlers.
Read more >The Perils of Rehydration - Josh W Comeau
This is known as server-side rendering (SSR). Server-side rendering can be a ... In a rehydration, React assumes that the DOM won't change....
Read more >Hydration and Server-side Rendering - somewhat abstract
During hydration, React works quickly in a virtual DOM to match up the existing content with what the application renders, saving time from ......
Read more >Keeping Server-Side Rendering Cool With React Hydration
The purpose of this article is to share some helpful things to keep in mind to render a seamless experience as a Server-Side...
Read more >SSR Rehydration - RFC
The aim of rehydration is to avoid the client-side performance issues of a naive SSR implementation, reusing original DOM wherever possible. # ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There is no meaningful difference between hydrate() and render() within Preact, since we don’t use checksums.
If you’re seeing a flash of white or similar, check that you’re not rendering something that is initially different from your server-rendered HTML. Also make sure you’re interpreting the meaning of “parent” and “root” correctly:
If you’d like a hydrate method, here’s a simple one:
Sweet. This can be closed then, correct?