Why does Google recommend inline CSS?

7

I was optimizing a client site that insists on wanting to get a high score on PageSpeed Insights, and I'm having trouble "removing JavaScript and CSS blocking rendering in content above the edge."

Even after I'm minifying CSS, I'm still charged for loading it before the site. So I asked myself, "What does Google recommend to me? Why does the import CSS at the bottom of the page mean it would not be completely unstructured until the final upload?"

I made the recommendations and Google recommended an inline style sheet loading via <style> !

I am not belittling the giant, but why the PageSpeed Insights recommends something so crude according to current standards? It practically forces the use of a preloader , which in turn slows down slow connections.

One thing to keep in mind: Google's own sites can not even get close to the ideal. A good example is Google Fonts , which gets a mobile note of 67/100.

    
asked by anonymous 21.06.2017 / 20:10

2 answers

5

It's not all, it's just the priority, few key parts of page loading.

Follow the tag fragment :

  

PageSpeed Insights is a tool provided by Google that measures the performance of a page on mobile and desktop devices . It fetches the URL twice, one with a mobile user agent and once with a user agent , generating suggestions to make the fastest one.

Tips, that is, based on Google's opinion , you can do X thing to improve.

BUT, what the "giant" might say is harmful to your site , since it is not aware of how you are developing your site, but this is another question that you as a developer should see, calculate and make the necessary modifications and not simply do X thing because the giant spoke ...

Given this, it's important to look at the Google Developers site, related to CSS, and read carefully what they say (emphasis mine):

  

In-line insertion of small CSS allows the browser to proceed with page rendering.

We recommend incorporating critical CSS inline. Small key parts, to load the styles needed to render the parts needed for your page.

  

In case of a large CSS file, you will need to identify and insert the required CSS in-line to process the contents of the region above the fold.

In other words, it is recommended to insert only what is irrefutable to render your site and if it is a large CSS file, you as a developer should analyze what is important to load / be loaded to avoid rendering block in the contents above the border and make the necessary changes.

Example

If the HTML document looks like this:

<html>
  <head>
    <link rel="stylesheet" href="small.css">
  </head>
  <body>
    <div class="blue">
      Hello, world!
    </div>
  </body>
</html>

And the small.css resource looks like this:

  .yellow {background-color: yellow;}
  .blue {color: blue;}
  .big { font-size: 8em; }
  .bold { font-weight: bold; }

Insert critical inline CSS as follows:

<html>
  <head>
    <style>
      .blue{color:blue;}
    </style>
    </head>
  <body>
    <div class="blue">
      Hello, world!
    </div>
  </body>
</html>
<link rel="stylesheet" href="small.css">

See? You have inserted only what is important, the visible content of the page. In spite of having several CSS rules in small.css , it put the priority on the page, which was class=blue , which the user will see when he / she enters your page.

You can also try to postpone or load blocking features asynchronously.

About your customer

Tell him that this is a tool that generates suggestions and recommendations , which are generally generic to any site, not specific to your pages. Also tell them that it's not Google that approves, but their customers approve. Who wants to have a site that Google approves but no one accesses?

  

This test verifies that the page uses common best practices for performance. A high score is correlated to a fast user experience, but does not guarantee it . - Google

Of course, do not ignore everything that Google says, but do not accept everything blindly strive for balance between Google and your customers.

Note: Particularly I've never seen a 100/100 site.

Just remembering, they are recommendations, they are not rules that should always be followed blindly, they are very generic. Apply them in moderation.

Sources:

21.06.2017 / 21:20
7

It is not Google's recommendation to insert inline CSS in any case, but in a specific case: when the stylesheet is small.

It is true that the page is unstructured if you leave the final CSS, but if the CSS is too large the page may take too long. Why not a middle ground? A little above (critical part) and the rest underneath. Google explicitly says: prioritize visible content :

  

In case of a large CSS file, you will need to identify and insert inline CSS needed to process the contents of the region above the fold and delay loading of the remaining styles until after the content above the fold is processed.

Even when the minimum CSS is too large to do inline, you can use this logic. For example:

<html>
  <head>
    <link rel="stylesheet" href="estilos-usados-acima-da-borda.css">
  </head>
  <body>
    conteúdo
    etc

    <link rel="stylesheet" href="outros-estilos.css">
  </body>
</html>

Remember (to yourself or your client) that indicators like PageSpeed Insights were not meant to be judged. They give great ideas and optimization tips, but they have no way of knowing the particularities of each site.

    
21.06.2017 / 20:57