Order your scripts' execution with defer and async, then steer the network with preload, preconnect, and prefetch.
Open this lesson in KodokonA classic <script src> blocks HTML parsing: the browser stops, downloads, executes, then resumes. Two attributes change the game. defer downloads in parallel with parsing and executes scripts in document order, just before DOMContentLoaded: it is the default choice for your application code, which may depend on the DOM and on other scripts. async also downloads in parallel but executes as soon as the file arrives, with no guaranteed order: reserve it for standalone scripts like analytics, which depend on nothing.
<script src="/js/analytics.js" async></script>
<script src="/js/vendor.js" defer></script>
<script src="/js/app.js" defer></script>The browser only discovers some critical resources very late: a font referenced inside a CSS file, an image loaded in JavaScript. <link rel="preload"> fixes that late discovery by declaring the resource right in the <head>, with the mandatory as attribute so the browser applies the right priority and the right cache. Textbook case: webfonts, which are otherwise requested only after the CSS has been downloaded and parsed.
<link rel="preload" as="font" type="font/woff2"
href="/fonts/inter-var.woff2" crossorigin>
<link rel="preload" as="image"
href="/img/hero-1600.jpg" fetchpriority="high">Last family: the low-priority network hints. <link rel="preconnect"> establishes the full connection in advance (DNS, TCP, TLS) to a third-party origin you will definitely use: up to 300 ms saved on the first request. dns-prefetch is its lightweight cousin (DNS resolution only), useful as a fallback for less certain origins. And prefetch downloads, during idle time, a resource meant for the next navigation: the checkout page from the cart, for instance. The trade-off is clear: preload serves the current page, prefetch bets on the next one.
<link rel="preconnect"
href="https://api.example.com">
<link rel="dns-prefetch"
href="https://cdn.example.com">
<link rel="prefetch" href="/checkout.html">