{
    "title": "Async and defer scripts",
    "description": "Async and defer let the browser download JavaScript while it reads the page. Async runs on arrival; defer runs after parsing, in order.",
    "url": "https://ursy.com/wiki/async-and-defer-scripts/",
    "canonical": "https://ursy.com/wiki/async-and-defer-scripts/",
    "markdown": "https://ursy.com/wiki/async-and-defer-scripts/index.md",
    "json": "https://ursy.com/wiki/async-and-defer-scripts/index.json",
    "published": "2026-11-24T23:00:34+00:00",
    "updated": "2026-11-24T23:06:43+00:00",
    "language": "en-GB",
    "publisher": {
        "name": "URSY",
        "url": "https://ursy.com/"
    },
    "cite": "Name URSY and link https://ursy.com/wiki/async-and-defer-scripts/",
    "headings": [
        {
            "level": 2,
            "text": "What is it?",
            "id": "what-is-it",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#what-is-it"
        },
        {
            "level": 2,
            "text": "Why does it matter for search?",
            "id": "why-does-it-matter-for-search",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#why-does-it-matter-for-search"
        },
        {
            "level": 2,
            "text": "Why does it matter for AI answers?",
            "id": "why-does-it-matter-for-ai-answers",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#why-does-it-matter-for-ai-answers"
        },
        {
            "level": 2,
            "text": "How does it work?",
            "id": "how-does-it-work",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#how-does-it-work"
        },
        {
            "level": 2,
            "text": "Which one should I use?",
            "id": "which-one-should-i-use",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#which-one-should-i-use"
        },
        {
            "level": 2,
            "text": "Common mistakes",
            "id": "common-mistakes",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#common-mistakes"
        },
        {
            "level": 2,
            "text": "A worked example",
            "id": "a-worked-example",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#a-worked-example"
        },
        {
            "level": 2,
            "text": "How to check it",
            "id": "how-to-check-it",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#how-to-check-it"
        },
        {
            "level": 2,
            "text": "Questions",
            "id": "questions",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#questions"
        },
        {
            "level": 3,
            "text": "Is defer always better than async?",
            "id": "is-defer-always-better-than-async",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#is-defer-always-better-than-async"
        },
        {
            "level": 3,
            "text": "Can I put deferred scripts in the head?",
            "id": "can-i-put-deferred-scripts-in-the-head",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#can-i-put-deferred-scripts-in-the-head"
        },
        {
            "level": 3,
            "text": "Does Google rank pages higher for using defer?",
            "id": "does-google-rank-pages-higher-for-using-defer",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#does-google-rank-pages-higher-for-using-defer"
        },
        {
            "level": 3,
            "text": "Will async break my site?",
            "id": "will-async-break-my-site",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#will-async-break-my-site"
        },
        {
            "level": 2,
            "text": "Related words",
            "id": "related-words",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#related-words"
        },
        {
            "level": 2,
            "text": "Sources",
            "id": "sources",
            "url": "https://ursy.com/wiki/async-and-defer-scripts/#sources"
        }
    ],
    "links": [
        {
            "text": "HTML",
            "url": "https://ursy.com/wiki/html/",
            "internal": true
        },
        {
            "text": "DOM",
            "url": "https://ursy.com/wiki/dom-document-object-model/",
            "internal": true
        },
        {
            "text": "script element reference",
            "url": "https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script",
            "internal": false
        },
        {
            "text": "Google",
            "url": "https://ursy.com/wiki/google/",
            "internal": true
        },
        {
            "text": "Core Web Vitals",
            "url": "https://ursy.com/wiki/core-web-vitals/",
            "internal": true
        },
        {
            "text": "Largest Contentful Paint",
            "url": "https://ursy.com/wiki/largest-contentful-paint/",
            "internal": true
        },
        {
            "text": "Interaction to Next Paint",
            "url": "https://ursy.com/wiki/interaction-to-next-paint/",
            "internal": true
        },
        {
            "text": "First Input Delay",
            "url": "https://ursy.com/wiki/first-input-delay/",
            "internal": true
        },
        {
            "text": "Lighthouse guide to render-blocking resources",
            "url": "https://developer.chrome.com/docs/lighthouse/performance/render-blocking-resources",
            "internal": false
        },
        {
            "text": "PageSpeed Insights",
            "url": "https://ursy.com/wiki/google-pagespeed-insights/",
            "internal": true
        },
        {
            "text": "Lighthouse",
            "url": "https://ursy.com/wiki/google-lighthouse/",
            "internal": true
        },
        {
            "text": "JavaScript SEO basics",
            "url": "https://developers.google.com/search/docs/crawling-indexing/javascript/javascript-seo-basics",
            "internal": false
        },
        {
            "text": "Googlebot",
            "url": "https://ursy.com/wiki/googlebot/",
            "internal": true
        },
        {
            "text": "AI crawlers",
            "url": "https://ursy.com/wiki/ai-crawler/",
            "internal": true
        },
        {
            "text": "bots",
            "url": "https://ursy.com/wiki/bot-web-robot/",
            "internal": true
        },
        {
            "text": "server-side rendering",
            "url": "https://ursy.com/wiki/server-side-rendering/",
            "internal": true
        },
        {
            "text": "JavaScript SEO",
            "url": "https://ursy.com/wiki/javascript-seo/",
            "internal": true
        },
        {
            "text": "HTML Living Standard",
            "url": "https://html.spec.whatwg.org/multipage/scripting.html",
            "internal": false
        },
        {
            "text": "analytics",
            "url": "https://ursy.com/wiki/web-analytics/",
            "internal": true
        },
        {
            "text": "Google Tag Manager",
            "url": "https://ursy.com/wiki/google-tag-manager/",
            "internal": true
        },
        {
            "text": "critical rendering path",
            "url": "https://ursy.com/wiki/critical-rendering-path/",
            "internal": true
        },
        {
            "text": "CSS",
            "url": "https://ursy.com/wiki/css/",
            "internal": true
        },
        {
            "text": "cookie banner",
            "url": "https://ursy.com/wiki/cookie-banner/",
            "internal": true
        },
        {
            "text": "PageSpeed",
            "url": "https://ursy.com/wiki/page-speed/",
            "internal": true
        },
        {
            "text": "Core Web Vitals Checker",
            "url": "https://ursy.com/core-web-vitals-checker/",
            "internal": true
        },
        {
            "text": "Page Speed Checker",
            "url": "https://ursy.com/page-speed-checker/",
            "internal": true
        },
        {
            "text": "AI Readiness Checker",
            "url": "https://ursy.com/ai-readiness-checker/",
            "internal": true
        },
        {
            "text": "JavaScript SEO Checker",
            "url": "https://ursy.com/javascript-seo-checker/",
            "internal": true
        },
        {
            "text": "caching",
            "url": "https://ursy.com/wiki/caching/",
            "internal": true
        },
        {
            "text": "HTTP Header Checker",
            "url": "https://ursy.com/http-header-checker/",
            "internal": true
        },
        {
            "text": "JavaScript Minifier",
            "url": "https://ursy.com/webmaster-tools/javascript-minifier/",
            "internal": true
        },
        {
            "text": "minification",
            "url": "https://ursy.com/wiki/minification/",
            "internal": true
        },
        {
            "text": "Core Web Vitals Calculator",
            "url": "https://ursy.com/webmaster-tools/core-web-vitals-calculator/",
            "internal": true
        },
        {
            "text": "Real user speed",
            "url": "https://ursy.com/pro/real-user-speed/",
            "internal": true
        },
        {
            "text": "Asset diet report",
            "url": "https://ursy.com/pro/asset-diet-report/",
            "internal": true
        },
        {
            "text": "JavaScript rendering check",
            "url": "https://ursy.com/pro/javascript-rendering-check/",
            "internal": true
        },
        {
            "text": "staging site",
            "url": "https://ursy.com/wiki/staging-site/",
            "internal": true
        },
        {
            "text": "Rendering",
            "url": "https://ursy.com/wiki/rendering/",
            "internal": true
        },
        {
            "text": "Lazy loading",
            "url": "https://ursy.com/wiki/lazy-loading/",
            "internal": true
        },
        {
            "text": "Why your website is slow",
            "url": "https://ursy.com/wiki/why-your-website-is-slow/",
            "internal": true
        }
    ],
    "words": 1189,
    "text": "Async and defer are two attributes you add to a script tag so the browser downloads the JavaScript file while it keeps reading the page, instead of stopping everything to fetch and run it. Async runs the file the moment it arrives. Defer waits until the whole page has been parsed and keeps your scripts in order. What is it? A browser builds a page by reading the HTML from top to bottom and turning it into the DOM. A plain <script src> tag in the head makes it stop, fetch the file, run it, and only then carry on. Nothing below that tag can be shown in the meantime. The two attributes change that pause: async: the file is fetched in parallel with parsing and run as soon as it is ready. MDN’s script element reference warns there is no promise about the order several async files will run in. defer: the file is fetched in parallel and run after the document has been parsed, just before the DOMContentLoaded event. Deferred files run in the order they appear in the page. <script src=\"app.js\"></script> stops the parser <script async src=\"stats.js\"></script> runs when it lands <script defer src=\"menu.js\"></script> runs after parsing, in order Why does it matter for search? Speed is part of how people judge a page, and Google measures it through Core Web Vitals. A blocking script in the head delays the first paint, which can push back Largest Contentful Paint. A heavy script that runs while a visitor taps a button can hurt Interaction to Next Paint, the measure that replaced First Input Delay. Google’s Lighthouse guide to render-blocking resources flags any script that sits in the head without defer or async. That is the warning you see in PageSpeed Insights and Lighthouse. Crawlers care too. Google’s JavaScript SEO basics explain that Googlebot crawls a page, queues it, then renders it in a headless Chromium before it indexes the result. Async and defer do not hide content from that render. They just stop your scripts from holding up the visible page. Why does it matter for AI answers? Many AI crawlers fetch the raw HTML and do not run JavaScript at all. Google’s own guide says that not all bots can run it, which is why it still calls server-side rendering or pre-rendering a great idea. Async and defer do not fix that. They only change when a script runs in a browser. If your prices, headings or answers only appear after a script runs, an AI system may never see them. Put the words in the HTML, and use scripts for extras. Our JavaScript SEO entry covers this in more depth. How does it work? The HTML Living Standard sets the rules. In short: Tag Download Runs Order kept? No attribute Parser stops At once Yes async Alongside parsing As soon as it arrives No defer Alongside parsing After parsing ends Yes type=”module” Alongside parsing After parsing ends Yes type=”module” async Alongside parsing As soon as it arrives No Four details trip people up. Module scripts are deferred by default, so adding defer to them does nothing. Neither attribute has any effect on an inline script with no src. If both are present, the browser acts as if only async is set; the standard lets you add both so very old browsers fall back to defer. And a deferred script holds back DOMContentLoaded until it has run. Which one should I use? defer for your own code that touches the page: menus, sliders, forms, anything that needs the DOM or another script first. async for independent files that depend on nothing and nothing depends on them, such as some analytics counters or a Google Tag Manager container. Inline, small and in the head, for the few lines the first view truly needs. The Lighthouse guide suggests this for critical code. Remove what is not used. The same guide says unused code should go. Fewer bytes beat any attribute. Common mistakes Async on scripts that depend on each other. A plugin set to async can run before the library it needs and throw an error. Defer on an inline script. It is ignored, so the script still blocks. Calling a deferred function from inline code higher up. The function does not exist yet when the inline code runs. Hiding core text behind scripts. Fast for people, empty for bots that read only HTML. Forgetting CSS. Stylesheets in the head also block the first paint; see the critical rendering path and CSS. Deferring a cookie banner script so late that tracking tags fire before consent is read. A worked example A small online shop has 4 files in its head: a 90 KB library, a slider that needs it, a chat widget and an analytics tag. All 4 block. The page stays blank until every file has downloaded and run. Library and slider get defer. They still run in order, after parsing. The analytics tag gets async, since nothing waits for it. The chat widget is moved to load after the visitor scrolls or clicks. Product names, prices and stock stay in the HTML, so crawlers read them without running anything. The head no longer holds up the first paint, and the render-blocking warning goes. Measure before and after rather than trusting a rule of thumb. How to check it View the page source and look at every <script> in the head. Note which have src, async, defer or type=”module”. Run Lighthouse or PageSpeed Insights and read the render-blocking list. Soon our Core Web Vitals Checker will explain each blocking file in plain words, with the Page Speed Checker (also coming soon) beside it. Check what a bot sees without scripts. Our AI Readiness Checker reads the raw HTML; the JavaScript SEO Checker will compare raw and rendered text. Check caching and compression of your script files with the HTTP Header Checker. Shrink files with our JavaScript Minifier; see minification. Work out pass or fail from your own numbers with the Core Web Vitals Calculator. In URSY Pro: Real user speed, the Asset diet report and the JavaScript rendering check. Questions Is defer always better than async? No. Defer is the safer default for code that needs the page or other scripts. Async suits small, independent files. Can I put deferred scripts in the head? Yes. That is the usual place. The browser finds them early, downloads them alongside parsing and runs them at the end. Does Google rank pages higher for using defer? Not directly. It can make the page faster, and speed is one part of page experience. Good content still matters far more. Will async break my site? It can, if one script relies on another. Test on a staging site first. Related words Critical rendering path JavaScript SEO Rendering Lazy loading Why your website is slow Sources MDN: The script element, checked 24 November 2026 WHATWG HTML Living Standard: Scripting, checked 24 November 2026 Chrome for Developers: Eliminate render-blocking resources, checked 24 November 2026 Google Search Central: Understand JavaScript SEO basics, checked 24 November 2026 Updated 24 November 2026 by URSY.",
    "source": "https://ursy.com/wiki/async-and-defer-scripts/"
}