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

  1. Symptom. A white page with its HTML and CSS already loaded is waiting on something else.
  2. Build the DOM. The parser turns HTML tokens, in order, into the DOM tree.
  3. Build the CSSOM. CSS is render-blocking: nothing paints until the CSSOM is built.
  4. First paint. DOM + CSSOM → render tree → layout → paint.
  5. Parser blocked. A plain script in the head stops parsing until it has downloaded and run.
  6. Defer. defer downloads in parallel and runs after parsing, before DOMContentLoaded.
  7. Async. async runs as soon as it arrives; DOMContentLoaded does not wait for it.
  8. Agent bug. Third-party scripts in the head get defer or async.
  9. Recap. Tokens → DOM + CSSOM → render tree → layout → paint.

Sources

Play the lesson: Critical rendering path