Before the browser can render a single pixel, it has to work through the following steps. It is known as the critical rendering path.
- Constructing the Document Object Model (DOM) from the HTML.
- Constructing the CSS Object Model (CSSOM) from the CSS.
- Applying any JavaScript that alters the DOM or CSSOM.
- Constructing the render tree from the DOM and CSSOM.
- Perform style and layout operations on the page to see what elements fit where.
- Paint the pixels of the elements in memory.
- Composite the pixels if any of them overlap.
- Physically draw all the resulting pixels to screen.
Two kinds of resources block the rendering path.
Render-blocking resources (mainly CSS) let the browser keep parsing the HTML but stop it from rendering anything. A stylesheet’s blocking reach depends on where it sits. A
<link>halfway down the<body>holds up only the part of the page below it. A stylesheet in the<head>comes before everything visible, so it holds up the whole page. That’s why critical-path work is mostly about what’s in your<head>.Parser-blocking resources (synchronous JS in the
<head>) pause HTML parsing and block rendering until they are loaded and evaluated. The browser discovers what else it needs (stylesheets, images, more scripts) by parsing the HTML. A script that pauses the parser therefore also delays the browser from discovering its next tasks. Browsers try to reduce this delay with a preload scanner. The preload scanner reads ahead through the content that has arrived and requests high-priority resources in the background, so they may already be in flight or downloaded when the parser gets to them. This helps, but it doesn’t remove the delay.
What to do about it:
defer: for most scripts. Downloads in the background, runs after the HTML is parsed, in order.async: for independent scripts like analytics. Downloads in the background, runs as soon as it arrives.
The article also shows how to identify these blocking resources with Lighthouse and WebPageTest. It also extends the idea to the critical contentful rendering path: everything needed before the main content (not just the first pixel) appears.