Open chin2km opened 7 months ago
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 https://github.com/vitejs/vite/pull/16018#issuecomment-2006385354 and you can see how frameworks implement this.
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?
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 <link />
that references the CSS files directly in index.html
, this helps with the initial HTML response from the server to have the CSS content, see my repo here
@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.
@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. (essentially that's what I've been doing by copying similar code from other frameworks as you can see from my comments there.)
@FreeJ1nG's example is a single client entry/route, so this plugin might be good enough to fix FOUC without manually managing <link>
. Here is an updated example:
https://stackblitz.com/edit/github-eq3fkw?file=vite.config.ts
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:
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