I have a critcal CSS process in place that prevents a flash-of-unstyled-content (FOUC) on above-the-fold content of a page.
I'm stuck on 'defer unused CSS' point that's being highlighted by Google PageSpeed insights (lighthouse) and/or Chrome's Performance Audit.
I've gone through other articles but they do not work.
To summarize I've tried so far.
- loadCSS()
- A script that uses requestAnimationFrame
Ref: CSS delivery optimization: How to defer css loading?
If I delay loading the script via setTimeout
by a fixed time of 3 seconds the 'defer unused CSS' issue goes away.
3 seconds is what's needed for Google PageSpeed Insights test (mobile) as they are slower devices but 3 seconds is a lot for desktops which generally has more processing power (note, not always true, hence excluding user-agent based logic).
So the question boils down to how do I delay loading the CSS by the least amount of time irrespective of the device type or specs.
Feel free to throw any rough ideas, I'll try them out and report back, if your idea works, we'll update the code and mark your answer has the chosen one.
Next on my list to try is requestAnimationFrame
+ small fixed delay.