“Eliminate render-blocking CSS in above-the-fold content”

前端 未结 8 1544
我在风中等你
我在风中等你 2020-11-30 18:01

I\'ve been using Google PageSpeed insights to try and improve my site\'s performance, and so far it\'s proven extremely successful. Things like deferring scripts worked beau

相关标签:
8条回答
  • 2020-11-30 18:22

    How I got a 99/100 on Google Page Speed (for mobile)

    TLDR: Compress and embed your entire css script between your <style></style> tags.


    I've been chasing down that elusive 100/100 score for about a week now. Like you, the last remaining item was was eliminating "render-blocking css for above the fold content."

    Surely there is an easy solve?? Nope. I tried out Filament group's loadCSS solution. Too much .js for my liking.

    What about async attributes for css (like js)? They don't exist.

    I was ready to give up. Then it dawned on me. If linking the script was blocking the render, what if I instead embedded my entire css in the head instead. That way there was nothing to block.

    It seemed absolutely WRONG to embed 1263 lines of CSS in my style tag. But I gave it a whirl. I compressed it (and prefixed it) first using:

    postcss -u autoprefixer --autoprefixer.browsers 'last 2 versions' -u cssnano --cssnano.autoprefixer false *.css -d min/ See the NPM postcss package.

    Now it was just one LONG line of space-less css. I plopped the css in <style>your;great-wall-of-china-long;css;here</style> tags on my home page. Then I re-analyzed with page speed insights.

    I went from 90/100 to 99/100 on mobile!!!

    This goes against everything in me (and probably you). But it SOLVED the problem. I'm just using it on my home page for now and including the compressed css programmatically via a PHP include.

    YMMV (your mileage may vary) pending on the length of your css. Google may ding you for too much above the fold content. But don't assume; test!

    Notes

    1. I'm only doing this on my home page for now so people get a FAST render on my most important page.

    2. Your css won't get cached. I'm not too worried though. The second they hit another page on my site, the .css will get cached (see Note 1).

    0 讨论(0)
  • 2020-11-30 18:28

    The 2019 optimal solution for this is HTTP/2 Server Push.

    You do not need any hacky javascript solutions or inline styles. However, you do need a server that supports HTTP 2.0 (any modern server version will), which itself requires your server to run SSL. However, with Let's Encrypt there's no reason not to be using SSL anyway.

    My site https://r.je/ has a 100/100 score for both mobile and desktop.

    The reason for these errors is that the browser gets the HTML, then has to wait for the CSS to be downloaded before the page can be rendered. Using HTTP2 you can send both the HTML and the CSS at the same time.

    You can use HTTP/2 push by setting the Link header.

    Apache example (.htaccess):

    Header add Link "</style.css>; as=style; rel=preload, </font.css>; as=style; rel=preload"
    

    For NGINX you can add the header to your location tag in the server configuration:

    location = / {
        add_header Link "</style.css>; as=style; rel=preload, </font.css>; as=style; rel=preload";
    }
    

    With this header set, the browser receives the HTML and CSS at the same time which stops the CSS from blocking rendering.

    You will want to tweak it so that the CSS is only sent on the first request, but the Link header is the most complete and least hacky solution to "Eliminate Render Blocking Javascript and CSS"

    For a detailed discussion, take a look at my post here: Eliminate Render Blocking CSS using HTTP/2 Push

    0 讨论(0)
提交回复
热议问题