Most active commenters

    ←back to thread

    Critical CSS

    (critical-css-extractor.kigo.studio)
    234 points stevenpotts | 13 comments | | HN request time: 1.16s | source | bottom
    1. 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 #
    2. larodi ◴[] No.43903028[source]
    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.
    replies(2): >>43903449 #>>43905469 #
    3. Gabrys1 ◴[] No.43903093[source]
    +1 on responsiveness
    4. todotask2 ◴[] No.43903449[source]
    > HTTP header hints

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

    5. worble ◴[] No.43904053[source]

        <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?
    replies(2): >>43905381 #>>43905956 #
    6. ◴[] No.43905381[source]
    7. ◴[] No.43905469[source]
    8. jy14898 ◴[] No.43905956[source]
    unsafe-hashes is a decent alternative
    9. 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 #
    10. 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 #
    11. 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 #
    12. stevenpotts ◴[] No.43928038[source]
    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
    13. 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.