News for hackers. Stuff that matters.
Re:Critical CSS by lelandfe on Tuesday May 06, 2025 @12:50AM (#43901905)
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
Re:Critical CSS by larodi on Tuesday May 06, 2025 @05:00AM (#43903028)
the prefetch attribute and other HTTP header hints, combined with proper CDN setups does almost the same. and would not require critical CSS to be nonstop rebuilt as the page develops. a properly configured CF is insanely fast.
Re:Critical CSS by todotask2 on Tuesday May 06, 2025 @06:18AM (#43903449)
> HTTP header hints

Assume it's either 103 Early Hints or Resource Hints in HTTP/1.1 and 2.0.

Re:Critical CSS by Gabrys1 on Tuesday May 06, 2025 @05:14AM (#43903093)
+1 on responsiveness
Re:Critical CSS by worble on Tuesday May 06, 2025 @07:53AM (#43904053)

    <link rel="preload" href="styles.css" as="style" onload="this.rel='stylesheet'">
Wouldn't this be blocked by a CSP that doesn't allow unsafe-inline?
Re:Critical CSS by jy14898 on Tuesday May 06, 2025 @11:04AM (#43905956)
unsafe-hashes is a decent alternative
Re:Critical CSS by youngtaff on Tuesday May 06, 2025 @03:19PM (#43908668)
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

Re:Critical CSS by lelandfe on Tuesday May 06, 2025 @04:09PM (#43909116)
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.

Re:Critical CSS by youngtaff on Wednesday May 07, 2025 @04:45AM (#43913461)
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

Re:Critical CSS by stevenpotts on Thursday May 08, 2025 @01:07PM (#43928401)
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.
Re:Critical CSS by stevenpotts on Thursday May 08, 2025 @12:42PM (#43928038)
you are right, I will add this option, probably replace the 'before body' option, the 'DOMCONTENTLOADED' option has worked wonders for me, even tested it on old phones and slower connections, it's good enough for UX and Lighthousr