-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Vite Dev Mode not loading css in react SSR application #16515
Comments
At the moment Vite SSR doesn't handle this out-of-the-box and each framework has been dealing with this on their own. There are a few links in this comment #16018 (comment) and you can see how frameworks implement this. |
Thanks for the reply! Does this mean this would be working out-of-the-box in a future Vite release? |
Hmm, I don't know about Vite team's plan. The comment I linked is just something came up when discussing about future css module change and it's not about the general FOUC issue. From my understanding, I'm not sure if it's ideal or possible for Vite to provide something uniform to SSR users/frameworks since frameworks normally wish to manage when/how to inject CSS on their own (e.g. code splitting with file-system routing etc..). |
What i did to solve it was to create a |
@FreeJ1nG that works if you only have one static file, but in the case where the page you render has css imports throughout the tree, there needs to be a way to query or intercept the imports during ssr so that you can gather them there. |
ah I've been thinking about how such code could fail, and yea, that makes sense :) |
This is not a complete solution for dev SSR FOUC issue, but just to share one approach I've been using, I extracted it to an independent plugin. https://github.com/hi-ogawa/vite-plugins/tree/main/packages/ssr-css To adapt it to your usage, it might be easier to just copy these style collection utilities https://github.com/hi-ogawa/vite-plugins/blob/main/packages/ssr-css/src/collect.ts and directly use it for your SSR handler pipeline. @FreeJ1nG's example is a single client entry/route, so this plugin might be good enough to fix FOUC without manually managing |
Describe the bug
Dev mode in a react server-rendered application, the initial critical css is not inlined into the html.
Styles kicks in only after the react hydration and the vite runtime loaded.
This results in a very bad flickering, especially in big applications since it takes a few seconds before the vite fetches all the required js and is browser ready.
Expectation
The stylesheet with critical css is either:
or
Reproduction
https://github.com/chin2km/vite-react-ssr-dev-mode-critical-css-issue
Steps to reproduce
and see the flickering from the server rendered page goes from a no-styles state to when the styles are loaded.
System Info
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: