Consider using the defer attribute instead of async for script tags [performance]
See original GitHub issue🚀 Feature Proposal
It seems like the most performant way of loading scripts is to include them with defer
instead of async
. At least according to this article
We’d be getting the following:
Instead of:
And when it comes to SSR, it would mean the js would only execute in order once the HTML is fully loaded.
Issue Analytics
- State:
- Created 5 years ago
- Comments:28 (11 by maintainers)
Top Results From Across the Web
Performance impact of async vs defer in 3rd party script tag
1. defer loads the script's text (blocking parsing), but doesn't execute it. Using async defer loads it in parallel to parsing, and then ......
Read more >Scripts: async, defer - The Modern JavaScript Tutorial
The defer attribute tells the browser not to wait for the script. Instead, the browser will continue to process the HTML, build DOM....
Read more >The Defer Attribute Explained - Jon D Jones
The issue with async is that the order in which the scripts are run can not be guaranteed.
Read more >What's the difference between async vs defer attributes
With defer attribute in place, the script file is downloaded in parallel while the HTML document is still parsing. However, even if the...
Read more >Prefer DEFER Over ASYNC - Web Performance Calendar
ASYNC and DEFER are similar in that they allow scripts to load without blocking the HTML parser which means users see page content...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m doing this for now as a temp workaround till they fix it (doesn’t cover any edge cases):
https://github.com/gerardmrk/erogen/blob/master/app/client.web/src/renderer/index.tsx#L220
So - I am pretty sure async is a wrong decision.
Defer
- run scripts, in the right order, after page is fully loadedAsync
- run a script once it is ready. before or after page is fully loadedReact.hydrate
- merge with a existing HTMLThe only variant, when react can
hydrate
is defer. In async mode DOM three could be not complete, by any reason, and the result would be just broken. You might wait for DOM-ready, but it would be equal todefer
, but less semantic and safe.