Critical rendering path: the whole lesson in words
Whenever the chat widget's server is slow, the whole sale page stays white.
Symptom
An agent added a chat widget to the sale page. Now, whenever the widget's server is slow, the whole page stays white, even though the HTML and the CSS have arrived.
A white page with its HTML and CSS already loaded is waiting on something else.
Predict
Before we explain anything, look at the page's head. The HTML and the CSS are here. What is the page waiting for?
Build the DOM
First, the page as it was before the widget. The parser reads the HTML one token at a time and builds the DOM, a tree of every element.
The parser turns HTML tokens, in order, into the DOM tree.
Build the CSSOM
The link tag names a stylesheet. Its rules become the CSSOM, a second tree. Until it is ready, the browser will not paint: CSS blocks rendering.
CSS is render-blocking: nothing paints until the CSSOM is built.
First paint
The DOM and the CSSOM join into the render tree, which holds only what will be drawn. Then layout and paint. Chrome measured first pixels well under a tenth of a second.
DOM + CSSOM → render tree → layout → paint.
Add the script
Now the agent's widget goes in the head as a plain script tag, and its server answers one second late. When does the heading appear?
Parser blocked
The parser reaches the script and stops. It cannot read another tag until the script has arrived and run. The CSS is ready, but there is no body to paint until then.
A plain script in the head stops parsing until it has downloaded and run.
Defer
Add the defer attribute. The script still downloads, but the parser carries on, and the script runs only after parsing ends, just before DOMContentLoaded. First pixels are back, well under a tenth of a second.
defer downloads in parallel and runs after parsing, before DOMContentLoaded.
Async
Change defer to async. The script still downloads in parallel, but it runs the moment it arrives, even in the middle of parsing, and DOMContentLoaded no longer waits for it.
async runs as soon as it arrives; DOMContentLoaded does not wait for it.
Agent bug
Back to the agent's widget. A plain script in the head stops the parser for as long as the widget's server takes. Mark it defer, or async if nothing on the page depends on it.
Third-party scripts in the head get defer or async.
Recap
Before the first pixel, the browser builds the DOM and the CSSOM, joins them, lays out and paints. CSS holds back the paint; a plain script holds back the parser.
Tokens → DOM + CSSOM → render tree → layout → paint.
The common wrong idea
The browser shows the HTML as soon as it arrives; CSS and scripts load in the background.
What really happens
Nothing paints until the CSSOM is built, and a plain script stops the parser until it has downloaded and run. defer and async take a script off that path.
The one-page cheatsheet for this lesson →
Sources
- web.dev/articles/critical-rendering-path/render-blocking-css
- web.dev/articles/critical-rendering-path/adding-interactivity-with-javascript
- web.dev/articles/critical-rendering-path/render-tree-construction
- developer.mozilla.org/en-US/docs/Web/Performance/Guides/Critical_rendering_path
- developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script
- html.spec.whatwg.org/multipage/scripting.html
- developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event