Part 01 · How the Browser Works
Critical Rendering Path
One CSS file and one script tag can keep the product page blank for seconds. What blocks rendering, and how async, defer and critical CSS unblock it.
⏱ 6 min · Station 3 of 125
The problem
ShopLite's product page has 3 lines of text and a coloured banner. The server sends the HTML in 10 ms. Yet on a slow day the customer stares at a white screen for 1.6 seconds, because of one CSS file and one script tag in <head>.
Nothing is broken, and no error is logged. The page is simply not allowed to paint yet. Knowing what the browser waits for before the first paint is the whole game of fast first loads.
The idea
The critical rendering path is the minimum work the browser must finish before it can paint the first frame: get the HTML, build the DOM up to the visible content, get every render-blocking stylesheet and build the CSSOM, run every parser-blocking script in the way, then style, layout and paint.
Anything on that path delays the first paint by its full download and run time. Anything moved off it (with defer, async, inline critical CSS, or a non-blocking stylesheet) no longer does.
How it works
- Stylesheets block rendering. The browser won't paint content without its CSS, because painting unstyled HTML and then restyling it (a "flash of unstyled content") looks broken. So every
<link rel="stylesheet">in the document must arrive before the first paint. - Plain scripts block the parser. While
<script src>downloads and runs, the parser can't reach the<body>. No body in the DOM means nothing to paint. - Scripts also wait for CSS. A script might ask
getComputedStyle(), so a script that comes after a stylesheet won't run until that stylesheet has loaded. CSS + sync script in<head>means: the slower of the two, and then the script's run time. deferdownloads the script in parallel and runs it after parsing finishes, just beforeDOMContentLoaded. It leaves the critical path.- Inline critical CSS puts the few rules the first screen needs in a
<style>tag. The full stylesheet loads without blocking (themedia="print"trick below, orrel="preload"+ swap), and applies when it arrives. DOMContentLoadedfires when parsing is done and deferred scripts have run. It is not "the page is visible": first paint can come long before it, or after it.
app.css takes 1000 ms and app.js takes 1500 ms. Only change: the script gets the defer attribute. When is the first paint now?
Visual
Three heads compared
| Head | CSS | Script | What gates first paint |
|---|---|---|---|
| blocking | <link rel=stylesheet> | <script src> | CSS and script download + run |
| defer | <link rel=stylesheet> | <script defer> | CSS only |
| critical | inline <style> + non-blocking link | <script defer> | just the HTML |
| (async) | <script async> | doesn't block parsing, but runs whenever it lands, in any order |
async is for scripts that don't depend on anything and nothing depends on them (analytics). defer keeps document order, so it's the default for your own code. type="module" scripts are deferred by default.
See it live
Measured on an Intel i3-10110U laptop (4 threads), 7 GB RAM, Ubuntu 26.04, Chrome 151 headless via Playwright 1.64.1, the lab in Docker on the same machine. No network throttling: the mock API's latency knob holds the CSS for 1000 ms and the JS for 1500 ms.
The blocking version, crp-blocking.html:
<!doctype html>
<html>
<head>
<title>crp blocking</title>
<link rel="stylesheet" href="http://localhost:4000/asset/app.css?latency=1000&kb=60" />
<script src="http://localhost:4000/asset/app.js?latency=1500&kb=150"></script>
</head>
<body>
<section class="hero"><h1>Nova phone Pro 7</h1><p>$499 · 4.6★</p></section>
<p>Free delivery in Dhaka. 30-day returns.</p>
</body>
</html>
crp-defer.html is identical except for <script defer ...>. crp-critical.html inlines the two rules the hero needs and loads the full CSS without blocking:
<!doctype html>
<html>
<head>
<title>crp critical</title>
<style>/* critical CSS: only what the first screen needs */ body{font-family:system-ui;margin:0} .hero{padding:32px;background:#ffcf70}</style>
<link rel="stylesheet" href="http://localhost:4000/asset/app.css?latency=1000&kb=60" media="print" onload="this.media='all'" />
<script defer src="http://localhost:4000/asset/app.js?latency=1500&kb=150"></script>
</head>
<body>
<section class="hero"><h1>Nova phone Pro 7</h1><p>$499 · 4.6★</p></section>
<p>Free delivery in Dhaka. 30-day returns.</p>
</body>
</html>
media="print" tells the browser this stylesheet isn't needed for the screen, so it downloads at low priority without blocking; onload switches it to all once it's here. tools/crp.mjs loads each page three times and prints the median:
// node crp.mjs: the same product page with three different <head>s.
// The CSS file takes 1000 ms to arrive, the JS file 1500 ms. When does the user see something?
import { open, pageTimings, WEB, ms } from "./lib.mjs";
console.log("variant first paint DOMContentLoaded load");
for (const v of ["blocking", "defer", "critical"]) {
const runs = [];
for (let i = 0; i < 3; i++) {
const { browser, page } = await open();
await page.goto(`${WEB}/x/crp-${v}.html`, { waitUntil: "load" });
runs.push(await pageTimings(page));
await browser.close();
}
// median of 3 runs
const med = (k) => runs.map((r) => r[k]).sort((a, b) => a - b)[1];
console.log(`${v.padEnd(10)} ${ms(med("fcp")).padStart(11)} ${ms(med("dcl")).padStart(18)} ${ms(med("load")).padStart(8)}`);
}
variant first paint DOMContentLoaded load
blocking 1592 ms 1542 ms 1544 ms
defer 1104 ms 1540 ms 1541 ms
critical 60 ms 1534 ms 1534 ms
Reading it:
- blocking: first paint at 1592 ms. The parser waited 1500 ms for the script, then the page painted. Notice first paint came after
DOMContentLoaded(1542 ms): the DOM was ready, the frame just hadn't been produced yet. - defer: 1104 ms. The script is off the path; the 1000 ms CSS is now the only thing in the way. Paint came about 100 ms after the CSS arrived.
- critical: 60 ms. Nothing blocks. The hero paints with its inline styles, and the full CSS applies when it lands.
DOMContentLoadedandloadbarely moved (about 1.54 s every time). The same files still download; what changed is only whether the user stares at white while they do. That's why "load time" is a bad metric for how fast a page feels.
| blocking | defer | critical | |
|---|---|---|---|
| First paint | 1592 ms | 1104 ms | 60 ms |
| vs. blocking | -488 ms | -1532 ms (26x sooner) |
In real apps
- Build tools extract critical CSS for you: tools in the Critters/Beasties family find the above-the-fold rules at build time and inline them, and Next.js has an opt-in setting to inline a route's CSS.
- Every first-party script is
deferor a module. Vite's builtindex.htmlusestype="module", so ShopLite's bundle is already deferred. - Third-party tags are loaded
asyncor afterload, often through a tag manager that itself isasync. - Big stores budget the critical path: some teams cap render-blocking CSS at around 14 KB, roughly what fits in the first TCP round trip, so the first paint needs one trip.
What goes wrong in production
- A CSS
@importinside a blocking stylesheet: the second file is only discovered after the first one arrives, doubling the blocking time. - A font, icon or A/B-test stylesheet from another domain in
<head>: its DNS + TCP + TLS is now on the critical path too. - Critical CSS that's too critical: inlining 200 KB of CSS makes every HTML response heavy and uncacheable.
- A/B testing scripts that hide the page (
opacity: 0until the experiment loads) bring back a blocking script under a different name. - Async scripts that depend on each other run in whatever order they arrive, so they work on fast Wi-Fi and break on mobile.
Key takeaway
First paint waits for every render-blocking stylesheet and every parser-blocking script ahead of the content. Defer your scripts, inline the CSS the first screen needs, load the rest without blocking, and judge pages by first paint, not by load.
Practice
The lab has one more page, crp-import.html: the defer version, except app.css now starts with @import url(".../theme.css?latency=800"). Predict its first paint. Then add "import" to the list of variants in crp.mjs and run it.
Answer
The browser only finds theme.css after app.css has arrived (1000 ms), and an imported sheet is render-blocking too. So expect roughly 1000 + 800 = 1800 ms plus a little: later than even the "blocking" version. Our three runs gave 2016, 1920 and 1916 ms. The fix is to list both stylesheets as <link> tags in the HTML, so they download in parallel and the preload scanner can see them, or to bundle them into one file at build time.
Station stamp
Finish the chapter
Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.
Go Pro to earn XP →