Hydration removes and add nodes again when there are HTML comments just before them
See original GitHub issue- Check if updating to the latest Preact version resolves the issue
Describe the bug
Preact unnecessarily removes and adds the DOM nodes that it finds after HTML comments during hydration.
<div id="root">
<div>I'm going to be preserved in the DOM</div>
<!-- I'm a comment, so I'll be removed, which is also fine -->
<div>I'll be removed and added back again</div>
</div>
To Reproduce
I reproduced the bug in this repository: https://github.com/luisherranz/preact-comments-hydration-bug (Open with Stackblitz Codeflow)
And this is a branch with a Mutation Observer added to the current hydration tests.
Steps to reproduce the behavior:
- Add a comment to the SSR’ed HTML
- Monitor changes to the DOM
- Hydrate it with Preact
- The nodes after the HTML comment are removed and added again
There is a video with a longer explanation on this other issue.
Expected behavior
Preact should remove the comments, but it should preserve the rest of the DOM nodes.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Initial rendering remove all nodes from server side ... - GitHub
When load the page in the first time or click the "refresh" button in browser, the client side js is going to remove...
Read more >Hydrating text content from Server-Side Rendering | Tan Li Hau
So, when React tries to hydrate the div from SSR, it starts with comparing all the props from the React element and the...
Read more >Understand and solve hydration errors in Vue.js - sum.cumo
As long as only the content of text nodes/attributes is affected, that is, no nodes are added or removed, hydration errors do not...
Read more >Why Efficient Hydration in JavaScript Frameworks is so ...
Most frameworks hydration ready code is larger than their typical client code because ultimately it needs to do both things. It might only...
Read more >How to render a HTML comment in React? - Stack Overflow
For one thing, it doesn't address the core issue; the result is four nodes (a comment node, two element nodes, and then a...
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
I’m going to close this issue as this works as expected, and we will manually remove the HTML comments during our DOM -> vDOM step. I’ll keep an eye on what you release for streaming in case we can adopt it in the future!
Oh, interesting (and exciting) 🙂
What’s your idea about that part? (for example, is the idea to keep
vnode
representations of those HTML comments in the vdom while the rest of the HTML is streamed?)