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
Hydration removes and add nodes again when there are HTML comments just before them #3767
Comments
It's more of a mismatch between the virtual tree and what's in the DOM tree because the comments were injected outside of Made a PR that would address this over here: #3771 That said I'm not sure if we should merge it as the use case seems to be a bit niche. I'd love to know more about where the comments are coming from in your scenario. It seems to be related to how WordPress' editor's HTML is generated. Can you share more information on that? |
Thanks for the explanation, and thanks for looking into this, Marvin. If this is actually the expected behavior in Preact, then we may need to look for an alternative solution. For context, this is not specifically related to WordPress, but to the partial hydration technique we are trying to implement, which:
The main benefit of that technique (versus the Astro-like island approach) is that interisland communication works out of the box (context, suspense, error boundaries) and it can do client-side transitions. In a way, it's similar to RSC, but lightweight and built on top of regular HTML. The initial HTML can be generated by any technology, not Actually, WordPress discourages using HTML comments as they are usually removed by optimization plugins or CDNs (Cloudflare and the like), but WordPress developers still add them sometimes. The ideal solution would be to add the HTML comments to the static virtual DOM and have a 1:1 match. But Preact is not prepared to deal with comment vnodes (the diffing fails). Now that I know that this is the expected behavior and not a bug, we'll try to deal with it ourselves. Maybe removing the comments during the DOM -> vDOM phase. |
@luisherranz Don't get me wrong, I'm happy to add support for this if there is a strong reason to do so. And it might look like I need that feature for streaming hydration anyway as the pending areas need to be marked with HTML comments. |
Oh, interesting (and exciting) 🙂 What's your idea about that part? (for example, is the idea to keep |
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! |
Describe the bug
Preact unnecessarily removes and adds the DOM nodes that it finds after HTML comments during hydration.
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:
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.
The text was updated successfully, but these errors were encountered: