Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Render Blocking CSS

I am trying to understand how CSS is evaluated in a specific setting:

Lets assume I have the following content in my <head> tag:

<html><head>
...
    <link href="reset.css" type="text/css" rel="stylesheet">
    <link href="style.css" type="text/css" rel="stylesheet">
    <link href="autocomplete.css" type="text/css" rel="stylesheet">
</head>
<body>
... html ...
<script type="text/javascript" src="/js/script.js"></script>
</body></html>

Now, let's assume reset.css and style.css contain some rules that immediately affect the above the fold content or the the elements in the HTML. However, autocomplete.css only contains class used that are used later by some JavaScript.

Now, let's further assume the browser already downloaded reset.css and style.css but autocomplete.css is still pending. I am wondering what would happen if the browser would do it encounters the blocking script tag at the end of the page? Obviously, it can render the HTML up to the script tag, but is the execution of the script blocked by the missing autocomplete.css?

Note that the script tag is not a sync.

I've read: https://developers.google.com/web/fundamentals/performance/critical-rendering-path/analyzing-crp

And there it says that the execution of the JavaScript is blocked until the CSSOM is there.

Now: 1. Will the page start rendering even autocomplete.css has not yet arrived? and, 2. Is the execution of the script.js javascript blocked until the autocomplete.css is there?

Note, I am referring to two different things: rendering and script execution.

like image 763
worenga Avatar asked Mar 03 '17 14:03

worenga


People also ask

What are render blocking?

What are render-blocking resources? Render-blocking resources are portions of code in website files, usually CSS and JavaScript, that prevent a web page from loading quickly. These resources take a relatively long time for the browser to process, but may not be necessary for the immediate user experience.

How do I know if I have render blocking resources?

How To Identify Render-Blocking Resources. To identify the critical rendering path and analyze critical resources: Run a test using webpagetest.org and click on the “waterfall” image. Focus on all resources requested and downloaded before the green “Start Render” line.

What is CSS render blocking JavaScript?

What is Render-Blocking JavaScript and CSS? Render blocking JavaScript and CSS are files that prevent a website from displaying a web page before loading these files. Every WordPress site has a theme and plugins that add JavaScript and CSS files to the front-end of your website.


2 Answers

All CSS is render blocking. The only exception from this rule would be CSS that your DOM does not yet know about (loaded async, built/loaded on the fly via javascript).

Until your browser didn't resolve (or thinks it resolved) all CSS by building the CSS Object Model, the page won't render and javascript will not be executed. However, resolve does not necessarily mean load. It can mean two things:

  • load & parse. If it's above 2k lines of code, you're going to notice it when measuring with proper tools. If it's above 10k lines of code, you'll be able to notice it without using any tools. This is regardless if it contains rules concerning elements above the fold or not.
  • not being able to load/parse (due to network problems or invalid rules) - if an error is returned when CSS is loaded/parsed, it will resolve faster and the resulting differences will be hardly noticeable. If the server does not return an error (and just doesn't respond) - that's going to block your CSSOM from building, your page from loading and your scripts from executing.

Resources:

  • W3C's CSSOM spec.
  • MDN's CSSOM resource page
  • What is CSSOM?
like image 94
tao Avatar answered Oct 30 '22 13:10

tao


The latest performance recommendations are as follows:

1) Inline all css into the header that the browser needs to render above-the-fold-content. Content which is visible without scrolling.

2) Add all other css to the bottom of the page or better load it asynchronously with something like this: https://github.com/filamentgroup/loadCSS

like image 35
Sascha Grindau Avatar answered Oct 30 '22 13:10

Sascha Grindau