Engineering 4 min read Updated
Loading scripts with defer, async and modules
In short
Use defer for scripts that need the page and must keep their order, async for independent scripts, and type module for modern code, which is deferred by default.
A script tag in the head can stop the browser from building the page until the file has downloaded and run. Attributes change that behaviour, but each one trades timing for order. Choose between a plain script, async, defer and module scripts by asking whether the script depends on the page or on other scripts.
A plain script blocks the parser
A classic script without attributes blocks the parser. The browser stops reading the HTML, fetches the file, runs it and only then carries on. The MDN reference says that such a script is fetched and executed before the browser continues to parse the page. The web.dev guidance adds that synchronous scripts delay DOM construction and rendering.
defer: after parsing, in order
A deferred script downloads in parallel with parsing and runs after the document has been parsed, before the DOMContentLoaded event fires. Deferred scripts run in the order they appear in the HTML. That makes defer the sensible default for scripts that touch the page or depend on each other, such as a library followed by the code that uses it.
async: as soon as it arrives
An async script also downloads in parallel, but runs as soon as it finishes, which may be while the parser is still working. MDN warns that order is not guaranteed, and web.dev notes that async scripts can interrupt DOM building. Use async only for scripts that stand on their own, such as an independent tag that does not need the page to be complete. If both attributes are present, the browser treats the script as async.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Module scripts are deferred by default
A script with type module is deferred by default, so defer has no effect on it. Module scripts can also use async. Module files fetched from another origin must be served with CORS, as MDN notes. Do not add async or defer to an inline script either, because those attributes have no effect without a src attribute.
Comparing the three behaviours
The table summarises what each attribute does to a classic script with a src attribute.
| Attribute | Download | When it runs | Order kept |
|---|---|---|---|
| None | Blocks the parser | Immediately | Yes, in document order |
| async | In parallel with parsing | As soon as it arrives | No |
| defer | In parallel with parsing | After parsing, before DOMContentLoaded | Yes, in document order |
Order problems rarely show up on a fast development machine. A cached file or a quick connection can make a script finish before the one it depends on, and the page then fails only for some visitors. The safe approach is to keep dependent scripts in defer, which keeps document order, and to use async only where no other script is involved. Test the page on a throttled connection as well, because that is where the timing differs.
A practical checklist
- Add defer to every script that needs the page, and keep the scripts in the order they depend on.
- Use async for an independent script, such as a tag that stands alone, and accept that the order is not guaranteed.
- Use type module for your own modern code, and check that module files from other origins send CORS headers.
- Do not add async or defer to inline scripts, because they have no effect there.
- After the change, measure in the browser performance tools when the page first renders and when the scripts run, and compare with the earlier result.
Third-party tags are often the scripts that cause trouble, so read the real cost of third-party scripts. Blocking scripts also affect when the main content appears, as described in fixing Largest Contentful Paint.
Statements about browser behaviour come from MDN and web.dev. Timing on a real page depends on the browser and the network, so measure before and after any change.
Common questions
- Which attribute should I use by default?
- Use defer for scripts that need the page or each other, because it keeps document order and waits until parsing ends. Use async only for scripts that stand alone.
- Does defer have any effect on module scripts?
- No. Module scripts are deferred by default, so the attribute changes nothing. It matters only for classic scripts.
- Why did an async script run before my other code?
- An async script runs as soon as it arrives, and the order of async scripts is not guaranteed. If one script depends on another, use defer for both, in document order.
Sources
- The Script element, MDN Web Docs. Accessed 24 November 2026
- Efficiently load third-party JavaScript, web.dev. Accessed 24 November 2026
Further reading
-
The real cost of third-party scripts
A third-party script costs your visitors time even when it is small, so load it with care and remove what adds no clear value.
-
Client-side validation that never replaces the server
Use the constraints HTML already provides, add clear custom messages where they help, and check every value again on the server.
-
Progressive enhancement in practice: HTML first
Build the page so the main task works with HTML alone, then add CSS and JavaScript as improvements that never block the baseline.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea