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
Style injection conflicts with SSR hydration on document #15765
Comments
Actually I just found I can manually save those injected styles before hydration and inject them after hydration. function hydrate(vnode: VNode) {
let injections: NodeListOf<HTMLStyleElement> | null = null;
if (import.meta.env.DEV) // save before hydration
injections = document.head.querySelectorAll("style[data-vite-dev-id]");
render(vnode, document, document.documentElement);
if (import.meta.env.DEV && injections) // inject after hydration
injections.forEach((element) => document.head.appendChild(element));
} Don't know if there are any side effects, but it seems to work well. |
I'm not sure if this is something Vite can fix. |
Thanks for commenting. Although I think this should be an expected behavior for react/preact as re-mount of html element should remove every other elements that should not exist. In another word, we should not let react to fix it, this should be remix's work when hydrating. I was expecting vite to expose something inside |
I think it cannot be handled by a meta-framework side completely because it seems to affect many examples (facebook/react#24430).
Perhaps Vite can provide a helper for that if it's a general problem among many rendering frameworks (e.g. React, Vue, Svelte). import.meta.hot.runWithoutStyles(() => {
hydrate(<App />, document)
}) |
Describe the bug
tl;dr: vite dev injected
<style>
tags maybe removed by preact's hydration on<html>
element.Actually my SSR framework renders the whole
<html>
tag and I was trying to figure out how to deal with the styles in dev server.Suppose I have a project like this:
And in server side, I just have something like this:
The code above could work for production build, as I can read the manifest file and know which entry requires which style and inject them into the final html code. However, when I was using dev server, I don't know how many styles there in the project, and I can only rely on vite's dynamic style injection. Here comes the problem.
To describe the problem more accurately, let's think it step by step after open browser:
__vite__updateStyle
runs, a style tag injected to headAfter these steps, no styles can get rendered.
Reproduction
n/a
Steps to reproduce
No response
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: