←back to thread

Critical CSS

(critical-css-extractor.kigo.studio)
234 points stevenpotts | 1 comments | | HN request time: 0s | source
Show context
lelandfe ◴[] No.43901905[source]
Nice one. Would be cool if this also handled responsiveness. The need to dedupe responsive critical styles has made me resort to manually editing all critical stylesheets I've ever made.

I also see that this brings in CSS variable definitions (sorry, ~custom properties~) and things like that. Since critical CSS's size matters so much, it might be worth giving an option to compile all of that down.

> Place your original non-critical CSS <link> tags just before the closing </body> tag

I don't recommend doing this: you still want the CSS downloaded urgently, critical CSS is a façade. Moving to the end of body means the good stuff is discovered late, and thus downloaded late (and will block render if the preloader[0] discovers it).

These days I'd recommend:

    <link rel="preload" href="styles.css" as="style" onload="this.rel='stylesheet'">

    <noscript><link rel="stylesheet" href="styles.css"></noscript>
[0] https://web.dev/articles/preload-scanner
replies(5): >>43903028 #>>43903093 #>>43904053 #>>43908668 #>>43928038 #
youngtaff ◴[] No.43908668[source]
I wouldn’t use the JS hack to load CSS…

When the stylesheet loads and is applied to the CSSOM it’s going to trigger layout and style calculations for the elements it’s applied to maybe even the whole page

Browsers are pretty eager at fetching stylesheets even those at the bottom of the page

replies(1): >>43909116 #
lelandfe ◴[] No.43909116[source]
That stylesheet application was going to happen anyway, the difference now is that FCP will occur before it.

> Browsers are pretty eager at fetching stylesheets even those at the bottom of the page

Browsers begin fetching resources as they discover them. For a big enough document, that will mean low placed resources will suffer.

replies(1): >>43913461 #
youngtaff ◴[] No.43913461{3}[source]
Sure that work is going to happen but often what you see is multiple stylesheets loaded using the async hack which results in multiple style and layout calculations as the browser can coalesce them because it doesn’t know that they’re stylesheets or when they will arrive

The whole philosophy of critical styles philosophy being those about the fold is a mistake in my view

Far better to adopt approaches like those recommended by Andy Bell that dramatically reduces stylesheet size

And do critical styles “correctly” i.e. load those that are needed to render the initial page and load the ones that rely on interactions separately

replies(1): >>43928401 #
1. stevenpotts ◴[] No.43928401{4}[source]
There are definitely better ways, however, for some scenarios and time constraint, this tool was really useful, e.g., I used a template, since the budget was tight and it had 10,000 of lines of CSS and I had to do something quickly to improve UX and Lighthouse results, the template had bootstrap, revolution, etc.