Critical rendering path in one screen
The one-page summary of the lesson Critical rendering path. It states the answers, so if you want them to stick, play the lesson first: it makes you predict before it explains.
The points
- HTML is parsed token by token into the DOM.
- CSS becomes the CSSOM; CSS is render-blocking.
- DOM + CSSOM → render tree (visible nodes only) → layout → paint.
- A plain <script> stops the parser until it has downloaded and run.
- defer: downloads in parallel, runs after parsing, in order, before DOMContentLoaded.
- async: downloads in parallel, runs as soon as it arrives, in any order.
- Slow CSS delays the first paint, not DOMContentLoaded.
What catches people out
- A script waits for earlier stylesheets before it runs, so slow CSS can delay a plain script too.
- async scripts that depend on each other can run in the wrong order.
The wrong idea, and the right one
Often believed: 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.
Step by step
- Symptom. A white page with its HTML and CSS already loaded is waiting on something else.
- Build the DOM. The parser turns HTML tokens, in order, into the DOM tree.
- Build the CSSOM. CSS is render-blocking: nothing paints until the CSSOM is built.
- First paint. DOM + CSSOM → render tree → layout → paint.
- Parser blocked. A plain script in the head stops parsing until it has downloaded and run.
- Defer. defer downloads in parallel and runs after parsing, before DOMContentLoaded.
- Async. async runs as soon as it arrives; DOMContentLoaded does not wait for it.
- Agent bug. Third-party scripts in the head get defer or async.
- Recap. Tokens → DOM + CSSOM → render tree → layout → paint.
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