Sub topic 13
Fetching CSS, scripts, images and fonts
This chapter assumes you know how the browser parses HTML into the DOM and how an HTTP request and response work. It covers what happens when the parser finds a reference to another file.
Every reference is a new request
The HTML response is only the first file. As the parser reads it, each <link>, <script> and <img> points to another URL. The browser fetches each one with its own HTTP request.
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/style.css">
<script src="/app.js" defer></script>
</head>
<body>
<h1>Hello</h1>
<img src="/logo.png" alt="Logo">
</body>
</html>This page triggers three more requests. If they go to the same origin (scheme, host and port), the browser reuses the open connection, so there is no new DNS lookup, TCP handshake or TLS handshake. A different origin, such as a CDN, needs all three again unless you hint with <link rel="preconnect">. The HTTP cache applies to each request, so a cached style.css may never touch the network.
Who blocks whom
Resources differ in how much they delay the page.
| Resource | Blocks HTML parsing | Blocks first paint | Notes |
|---|---|---|---|
| Stylesheet | No | Yes | The browser will not paint without the CSSOM. |
| Plain `<script src>` | Yes | Yes | Parsing pauses until it downloads and runs. |
| `defer` script | No | No | Runs after parsing, in document order. |
| `async` script | No, until it runs | No | Runs as soon as it arrives, in any order. |
| Image | No | No | Loads in the background. |
| Font | No | Depends | Fetched only when text needs it. |
A plain script blocks parsing because it may call document.write or change the DOM. A script can also read styles, so it waits for any pending stylesheets first. That is why a stylesheet followed by a plain script can stall the whole page.
The preload scanner
If the main parser is stuck on a script, the browser does not sit idle. A lightweight preload scanner reads ahead through the raw HTML and starts downloads for stylesheets, scripts and images it finds. This works only for URLs written in the HTML. A resource named in JavaScript or CSS is found later, after that file has been downloaded and run.
Fonts and images
Fonts are discovered late. The browser learns about a font from an @font-face rule in CSS. It downloads the font only after the CSS is parsed and some element on the page uses that font. Until then, the font-display setting decides what you see. With swap, fallback text shows first and is replaced when the font arrives. You can start the download earlier with <link rel="preload" as="font" crossorigin>. Fonts are fetched in CORS mode, so the crossorigin attribute is required even for same-origin files.
Images do not block rendering, but they can move content. Without width and height attributes, the browser does not know the image size until it loads. The layout then shifts when the image arrives. Setting the dimensions reserves the space.
Common mistakes
- Putting a plain
<script>in<head>withoutdefer. The page stays blank until it runs. - Chaining CSS with
@import. Each file is discovered only after the previous one downloads. Use separate<link>tags instead. - Preloading a font without
crossorigin. The browser fetches it twice. - Loading many resources from many origins. Each new origin pays for DNS, TCP and TLS.
Exercise
A page has <script src="/big.js"></script> in <head>, followed by a <link rel="stylesheet">. The page shows nothing for several seconds. Give one change that helps, and say why the stylesheet is also found late.
Show answer
Add defer to the script, so parsing continues while it downloads and it runs after parsing. The stylesheet is found late because the parser is paused on the plain script. The preload scanner may still find it early. Moving the <link> above the script also makes the stylesheet request start sooner.