Critical Rendering Path — Performance

The browser turns HTML into pixels through the DOM, CSSOM, render tree, layout, paint and composite. CSS holds rendering by default, and a <script> without…

On this lesson's example page, the HTML reaches the phone in 400 ms. But the screen only shows something at 1,350 ms. For almost a second the browser isn't being lazy: it's waiting for something before it's allowed to draw.

The route it takes from receiving HTML to drawing the first pixels on screen is the critical rendering path. It's called critical because everything on this route has to be done before the visitor sees anything. Picture cooking fried rice: the rice, the spices and the wok must be ready before you start, while the crackers can come later. If you wait for the crackers to be fried before you start cooking, the meal is late too. Your job: tell apart what's required and what can come later.

From HTML to pixels

  1. DOM. The browser reads (parses) the HTML from top to bottom and builds the DOM, a tree of every element on the page.
  2. CSSOM. All the CSS is built into the CSSOM, the set of style rules.
  3. Render tree. The DOM and CSSOM are combined. It holds only elements that show, so anything with display: none is dropped.
  4. Layout. The browser works out the position and size of every element.
  5. Paint. The browser colours in the pixels: text, colours, borders, images.
  6. Composite. If the page is split into several layers, the layers are stacked into one final picture.

The first moment the browser draws anything on screen is called the first paint. The sooner the first paint, the sooner the visitor knows the page is alive.

What holds rendering back

A render-blocking resource makes the browser put off drawing the page until that resource is done.

The example page

This is the <head> of the page used in the Lab. The CSS for the top of the page is already written straight into <style>, so non-critical.css only holds styles for parts that aren't visible yet:

<head>
  <title>Landing Page</title>
  <style>/* critical CSS: header and hero */</style>
  <link rel="stylesheet" href="non-critical.css">
  <script src="app.js"></script>
</head>

On a slow phone network, the timing is roughly this (a simulation):

Resource Starts Ends Holds back?
index.html 0 ms 400 ms
non-critical.css (60 KB) 400 ms 1,000 ms rendering
app.js (120 KB) 400 ms 1,100 ms, then runs 150 ms the parser
hero.woff2 (the heading font) 1,300 ms 1,600 ms

The parser stops at app.js from 400 ms until the script finishes running at 1,250 ms, then needs 50 ms to read the rest of the HTML. The first paint only comes at 1,350 ms. The heading font is only found to be needed once the render tree exists, so the heading only shows at 1,600 ms.

One at a time, then all together

Before you look at the table, guess: if you only add defer to the script, where does the first paint move from 1,350 ms? To 500 ms, to 1,050 ms, or does it stay at 1,350 ms?

What changed First paint Hero heading shows
nothing yet 1,350 ms 1,600 ms
defer only 1,050 ms 1,300 ms
media="print" only 1,350 ms 1,600 ms
defer and media="print" 500 ms 750 ms
plus preloading the font 500 ms 700 ms

With defer alone, the first paint moves to 1,050 ms. The parser isn't held anymore, but non-critical.css still holds rendering until 1,000 ms. The other way round, media="print" alone changes nothing, because the script still holds the parser. Only the two together bring the first paint down to 500 ms.

Three fixes in the head

<head>
  <title>Landing Page</title>
  <style>/* critical CSS: header and hero */</style>
  <link rel="preload" href="/fonts/hero.woff2" as="font" type="font/woff2" crossorigin>
  <link rel="stylesheet" href="non-critical.css" media="print" onload="this.media='all'">
  <script src="app.js" defer></script>
</head>

The same works for the page's main image: <link rel="preload" href="hero.webp" as="image"> (images don't need crossorigin). So when a page has a big CSS file and several scripts in <head>, the three most effective steps are: put the critical CSS straight into <style>, give the scripts defer, and preload the biggest image.

⚠️ When do the head tricks backfire?

TL;DR: The browser turns HTML into pixels through the DOM, CSSOM, render tree, layout, paint and composite. CSS holds rendering by default, and a <script> without defer or async holds the parser, so both delay the first paint. Fix the <head> in three steps: defer on the script that has a src, media="print" plus onload="this.media='all'" on CSS that isn't critical, and <link rel="preload" as="font" crossorigin> for an important font. In this example the first paint drops from 1,350 ms to 500 ms, and the hero heading from 1,600 ms to 700 ms.

⚡ From HTML to pixels

Guess first: which step works out the position and size of every element?

Layout. After it, paint colours in the pixels, and composite stacks the layers into one picture.

Meaning: The browser builds the DOM from the HTML and the CSSOM from the CSS, combines them into the render tree, then runs layout, paint and composite. The first moment anything is drawn on screen is called the first paint.

⚡ What holds rendering back

Write:

<link rel="stylesheet" href="non-critical.css">
<script src="app.js"></script>

Meaning: CSS holds rendering back by default. A <script> without defer or async even holds the parser: the browser stops reading the HTML until that script is downloaded and has run. On the example page, the first paint only comes at 1,350 ms.

⚡ Three fixes in the head

Write:

<link rel="preload" href="/fonts/hero.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="non-critical.css" media="print" onload="this.media='all'">
<script src="app.js" defer></script>
Guess first: if only the script gets defer, where does the first paint move?

To 1,050 ms, not 500 ms. The parser isn't held anymore, but non-critical.css still holds rendering until 1,000 ms. Only the two together bring the first paint down to 500 ms.

Meaning: defer runs the script after the HTML has been read in full. media="print" keeps the CSS from holding the screen, then onload switches it to all. A font preload needs crossorigin, or the font is downloaded twice.

What you will learn