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

前端 未结 8 1543
我在风中等你
我在风中等你 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:05

    Hi For jQuery You can only use like this

    Use async and type="text/javascript" only

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

    A related question has been asked before: What is “above-the-fold content” in Google Pagespeed?

    Firstly you have to notice that this is all about 'mobile pages'.
    So when I interpreted your question and screenshot correctly, then this is not for your site!

    On the contrary - doing some of the things advised by Google in their guidelines will things make worse than better for 'normal' websites.
    And not everything that comes from Google is the "holy grail" just because it comes from Google. And they themselves are not a good role model if you have a look at their HTML markup.

    The best advice I could give you is:

    • Set width and height on replaced elements in your CSS, so that the browser can layout the elements and doesn't have to wait for the replaced content!

    Additionally why do you use different CSS files, rather than just one?
    The additional request is worse than the small amount of data volume. And after the first request the CSS file is cached anyway.

    The things one should always take care of are:

    • reduce the number of requests as much as possible
    • keep your overall page weight as low as possible

    And don't puzzle your brain about how to get 100% of Google's PageSpeed Insights tool ...! ;-)

    Addition 1: Here is the page on which Google shows us, what they recommend for Optimize CSS Delivery.

    As said before, I don't think that this is neither realistic nor that it makes sense for a "normal" website! Because mainly when you have a responsive web design it is most certain that you use media queries and other layout styles. So if you are not gonna load your CSS first and in a blocking manner you'll get a FOUT (Flash Of Unstyled Text). I really do not believe that this is "better" than at least some more milliseconds to render the page!

    Imho Google is starting a new "hype" (when I have a look at all the question about it here on Stackoverflow) ...!

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

    I too have struggled with this new pagespeed metric.

    Although I have found no practical way to get my score back up to %100 there are a few things I have found helpful.

    Combining all css into one file helped a lot. All my sites are back up to %95 - %98.

    The only other thing I could think of was to inline all the necessary css (which appears to be most of it - at least for my pages) on the first page to get the sweet high score. Although it may help your speed score this will probably make your page load slower though.

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

    Please have a look on the following page https://varvy.com/pagespeed/render-blocking-css.html . This helped me to get rid of "Render Blocking CSS". I used the following code in order to remove "Render Blocking CSS". Now in google page speed insight I am not getting issue related with render blocking css.

    <!-- loadCSS -->
    <script src="https://cdn.rawgit.com/filamentgroup/loadCSS/6b637fe0/src/cssrelpreload.js"></script>
    <script src="https://cdn.rawgit.com/filamentgroup/loadCSS/6b637fe0/src/loadCSS.js"></script>
    <script src="https://cdn.rawgit.com/filamentgroup/loadCSS/6b637fe0/src/onloadCSS.js"></script>
    <script>
          /*!
          loadCSS: load a CSS file asynchronously.
          */
          function loadCSS(href){
            var ss = window.document.createElement('link'),
                ref = window.document.getElementsByTagName('head')[0];
    
            ss.rel = 'stylesheet';
            ss.href = href;
    
            // temporarily, set media to something non-matching to ensure it'll
            // fetch without blocking render
            ss.media = 'only x';
    
            ref.parentNode.insertBefore(ss, ref);
    
            setTimeout( function(){
              // set media back to `all` so that the stylesheet applies once it loads
              ss.media = 'all';
            },0);
          }
          loadCSS('styles.css');
        </script>
        <noscript>
          <!-- Let's not assume anything -->
          <link rel="stylesheet" href="styles.css">
        </noscript>
    
    0 讨论(0)
  • 2020-11-30 18:16

    Few tips that may help:

    • I came across this article in CSS optimization yesterday: CSS profiling for ... optimization
      A lot of useful info on CSS and what CSS causes the most performance drains.

    • I saw the following presentation on jQueryUK on "hidden secrets" in Googe Chrome (Canary) Dev Tools: DevTools Can do that. Check out the sections on Time to First Paint, repaints and costly CSS.

    • Also, if you are using a loader like requireJS you could have a look at one of the CSS loader plugins, called require-CSS, which uses CSSO - a optimzer that also does structural optimization, eg. merging blocks with identical properties. I used it a few times and it can save quite a lot of CSS from case to case.

    Off the question: I second @Enzino in creating a sprite for all the small icons you are loading. The file sizes are so small it does not really warrant a server roundtrip for each icon. Also keep in mind the total number of concurrent http requests are browser can do. So requests for a larger number of small icons are "render-blocking" as well. Although an empty page compare to yours, I like how duckduckgo loads for example.

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

    Consider using a package to automatically generate inline styles from your css files. A good one is Grunt Critical or Critical css for Laravel.

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