The short answer

#

Both defer and async let the browser download a script while it keeps parsing the HTML. The difference is when the script runs. A defer script waits until the document has been parsed and runs in the order it appears. An async script runs as soon as it has downloaded, in whatever order the downloads finish. Use defer for your own application code and async for independent scripts such as analytics.


_10
<head>
_10
<!-- your app: runs in order, after parsing -->
_10
<script defer src="/js/vendor.js"></script>
_10
<script defer src="/js/app.js"></script>
_10
_10
<!-- independent: runs whenever it is ready -->
_10
<script async src="https://example.com/analytics.js"></script>
_10
</head>

Why it happens

#

A plain script tag with a src is parser-blocking. When the browser reaches it, it stops building the page, downloads the file, runs it and only then carries on. That is why scripts used to go at the end of the body. Scripts block because they can call document.write or read the DOM that has been built so far, and the browser must respect that.

The two attributes tell the browser the script does not need that guarantee:

DownloadsRunsOrder
no attributeblocks parsingimmediatelydocument order
deferin parallelafter parsing, before DOMContentLoadeddocument order
asyncin parallelas soon as downloaded, pausing the parserfirst to arrive

So defer behaves like putting the script at the end of the body, but the download starts much earlier because the browser sees the tag in the head. async is the fastest to run, but you cannot rely on the DOM being complete or on another script having run first.

How to fix it

#

Use defer for scripts that depend on each other or on the DOM

If app.js uses a library loaded by vendor.js, or queries elements on the page, defer keeps the order and guarantees the document is parsed. You do not need a DOMContentLoaded listener inside a deferred script; the DOM is already there.


_10
<script defer src="/js/vendor.js"></script>
_10
<script defer src="/js/app.js"></script>

Use async for scripts that stand alone

Analytics, ads and error reporting do not depend on your code and nothing depends on them. async lets them start as early as possible.


_10
<script async src="https://example.com/analytics.js"></script>

Know that modules are deferred by default

A script with type="module" is deferred automatically, so adding defer does nothing. You can add async to a module script to run it as soon as it and its imports are ready.


_10
<script type="module" src="/js/main.js"></script>
_10
<script type="module" async src="/js/widget.js"></script>

Scripts added with JavaScript are async

A script element created with document.createElement and appended to the page behaves like async. Set script.async = false if you need several dynamically added scripts to run in order.


_10
const script = document.createElement('script');
_10
script.src = '/js/chart.js';
_10
script.async = false;
_10
document.head.append(script);

Common pitfalls

#
  • Adding defer or async to an inline script. They only apply to scripts with a src, except async on inline module scripts.
  • Using async for code that touches the DOM. It may run before the elements it needs exist.
  • Using async on scripts that depend on each other. The order changes depending on network speed, so the bug comes and goes.
  • Setting both attributes. Modern browsers treat the script as async; defer is only a fallback for very old ones.
  • Listening for DOMContentLoaded inside an async script. If the script arrives late, the event may already have fired and the listener never runs.
#
file under:
HTML

quick question

Was this resource helpful?