关键渲染路径 是浏览器将 HTML、CSS 和 JS 转换为像素的一系列步骤。理解它可以解释什么阻塞了渲染,以及如何使页面绘制更快。
步骤
text
1. Parse HTML → DOM tree (the document structure)
2. Parse CSS → CSSOM tree (the style rules)
3. DOM + CSSOM → Render tree (visible nodes + their styles)
4. Layout (reflow) → compute each node's geometry (position/size)
5. Paint → fill in pixels (colors, text, images)
6. Composite → combine layers onto the screen
什么阻塞渲染
CSS 会阻塞渲染。 浏览器在 CSSOM 准备好之前不会绘制(以避免无样式内容的闪现)。因此大型/低速 CSS 会延迟首次绘制 — 应该保持关键 CSS 很小,并尽可能将其内联。
同步脚本会阻塞解析。 普通的 会暂停 HTML 解析、下载并执行,然后才继续 — 因为脚本可能会修改 DOM:
