Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

script element with async attribute still block browser render?

I use cuzillion tool build a page:

<head>
  <script async></script>
</head>
<body>
   <img />
   <img />
   <img /> 
</body>  

there is only one script element in head, with async attribute and 2 second delay, 3 second to execute.

but the page load timeline in Chrome is:

enter image description here

when the script executing, it still block the browser render process?

But why? it shouldn't execute asynchronously?

However it doesn't block the parser:

enter image description here

like image 259
hh54188 Avatar asked Mar 10 '14 07:03

hh54188


1 Answers

The execution of any script always blocks parsing, rendering, and execution of other scripts in the same tab. The attribute async does not change that.

The only thing async does is tell the browser that the script should be fetched (assuming it's a remote file) without blocking those activities.

After the script is downloaded, the script starts executing at the next available opportunity (that is, right after the current script, if any, finishes running; a new script won't, of course, interrupt a running script). Once that happens, your rendering is blocked. So, with a fast web server, downloading happens so fast that async makes no difference at all.

If you don't want your scripts to pause rendering, use defer attribute instead of async. That will delay the execution of the script until after the document is fully loaded.

More on this here.

like image 73
max Avatar answered Oct 30 '22 11:10

max