Skip to content

Commit f740ccf

Browse files
committed
feat: upd answer for question N1
1 parent 587191a commit f740ccf

6 files changed

Lines changed: 173 additions & 206 deletions

File tree

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,53 @@
1-
<p>The process of turning HTML, CSS, and JS code into an interactive page on the screen is called the <strong>Critical Rendering Path</strong>. It consists of the following sequential steps:</p>
2-
3-
<h3>1. HTML Parsing and DOM Tree Construction</h3>
4-
<p>As soon as the browser receives the first bytes of an HTML document over the network, the HTML parser starts.</p>
5-
6-
<p>The parser reads the code from top to bottom, converting tags into tokens (start tag, content, end tag), and tokens into nodes.</p>
7-
8-
<p>From these nodes, the <span class="accent">DOM (Document Object Model)</span> is built — a tree-like structure describing the page content.</p>
9-
10-
<p class="info">Important: HTML parsing happens incrementally (as it is downloaded) but can be blocked by synchronous scripts (<code>&lt;script&gt;</code>).</p>
11-
12-
<h3>2. Downloading External Resources and JavaScript Execution</h3>
13-
<p>Upon encountering links to external resources (fonts, images, CSS, JS), the browser sends requests to download them.</p>
14-
15-
<p>JavaScript plays a special role:</p>
16-
17-
<p> - By default, <code>&lt;script&gt;</code> blocks HTML parsing. The browser pauses DOM construction, downloads the script, executes it, and only then continues parsing the HTML.</p>
18-
19-
<p> - <code>defer</code> — the script is downloaded in the background without blocking parsing, and executes strictly after the entire HTML has been parsed, preserving the script execution order.</p>
20-
21-
<p> - <code>async</code> — the script is downloaded in the background and executes immediately upon download, pausing HTML parsing during execution. The execution order is not guaranteed.</p>
22-
23-
<p>Modern browsers use a Preload Scanner — a background process that "looks ahead" of the HTML parser, finds external resources, and starts downloading them in advance.</p>
24-
25-
<h3>3. CSS Parsing and CSSOM Creation</h3>
26-
<p>As soon as a CSS file is downloaded, the browser converts it into the <span class="accent">CSSOM (CSS Object Model)</span> — a tree-like structure describing the styles for DOM nodes.</p>
27-
28-
<p>Unlike HTML, CSS parsing cannot be incremental, as styles declared at the end of the file can override styles from the beginning.</p>
29-
30-
<p>CSS is Render-Blocking. The browser will not start painting the page until the complete CSSOM is built to avoid a "Flash of Unstyled Content" (FOUC).</p>
31-
32-
<h3>4. Render Tree Construction</h3>
33-
<p>The browser combines the DOM and CSSOM into a single Render Tree.</p>
34-
35-
<p>The Render Tree contains only the nodes that will be visually displayed on the screen.</p>
36-
37-
<p>Tags like <code>&lt;head&gt;</code>, <code>&lt;meta&gt;</code>, and <code>&lt;script&gt;</code> are not included in this tree.</p>
38-
39-
<p>Nodes with the <code>display: none</code> property are excluded from the render tree.</p>
40-
41-
<p>Nodes with <code>visibility: hidden</code> or <code>opacity: 0</code> styles remain in the tree, as they still occupy space on the screen (affecting geometry).</p>
42-
43-
<h3>5. Geometry Calculation (Layout / Reflow)</h3>
44-
<p>Having the Render Tree, the browser knows what to paint, but not where.</p>
45-
<p>During the <span class="accent">Layout</span> phase, the browser traverses the render tree from top to bottom and calculates the exact dimensions (width, height) and coordinates of each element on the screen (in pixels) relative to the viewport.</p>
46-
47-
<p><span class="accent">Reflow</span> is the process where the browser recalculates the dimensions and exact positioning of elements on the page. Essentially, this is a re-execution of the Layout step.</p>
48-
49-
<h3>6. Paint</h3>
50-
<p>The browser knows the geometry of all elements. During the <span class="accent">Paint</span> phase, it starts filling in the pixels on the screen: drawing text, colors, borders, shadows, and background images. Painting typically occurs on multiple independent layers.</p>
51-
52-
<h3>7. Compositing</h3>
53-
<p>Since elements might have been painted on different layers (for example, elements with <code>transform</code>, <code>opacity</code>, or <code>z-index</code>), in the final step, the browser overlays these layers on top of each other in the correct order on the Graphics Processing Unit (GPU), forming the final image that the user sees.</p>
54-
55-
<h3>Key Loading Events</h3>
56-
<p><code>DOMContentLoaded</code> — fires when the initial HTML document has been completely loaded and parsed into the DOM, without waiting for stylesheets, images, and subframes to finish loading.</p>
57-
58-
<p><code>load</code> — fires when the page is fully loaded, including all dependent resources (images, styles, etc.).</p>
1+
<h3>Critical Rendering Path</h3>
2+
<p>Turning HTML, CSS, and JS into an interactive page is the <span class="accent">Critical Rendering Path</span>: a chain of sequential steps from bytes to pixels.</p>
3+
4+
<p class="info"><strong>Answer skeleton:</strong> parse HTML → <code>DOM</code>, parse CSS → <code>CSSOM</code>, combine them into a <strong>Render Tree</strong>, then <strong>Layout → Paint → Composite</strong>. HTML is parsed incrementally, but CSS and synchronous JS block this pipeline.</p>
5+
6+
<h3>Parsing HTML → DOM, CSS → CSSOM</h3>
7+
<p>Once the first bytes arrive, the HTML parser reads the code top to bottom, turns tags into tokens and tokens into nodes, from which it builds the <span class="accent">DOM</span> — the tree of the page's content. Parsing happens incrementally, as bytes are downloaded.</p>
8+
9+
<p>In parallel, the browser builds the <span class="accent">CSSOM</span> from CSS — the tree of styles. CSS is <strong>render-blocking</strong>: the browser won't paint the page until the full CSSOM is built — otherwise you'd get FOUC (a flash of unstyled content).</p>
10+
11+
<h3>JavaScript blocks parsing</h3>
12+
<p>By default, <code>&lt;script&gt;</code> stops DOM construction: the browser downloads and executes the script, and only then resumes parsing the HTML.</p>
13+
<ul>
14+
<li><code>defer</code> — downloaded in the background, executed after the HTML is fully parsed, preserving the script order.</li>
15+
<li><code>async</code> — downloaded in the background and executed as soon as it's ready, pausing parsing during execution; order is not guaranteed.</li>
16+
</ul>
17+
18+
<h3>Render Tree → Layout → Paint → Composite</h3>
19+
<p>The DOM and CSSOM are combined into the <span class="accent">Render Tree</span> — only visible nodes make it in (no <code>&lt;head&gt;</code>, <code>&lt;script&gt;</code>, or elements with <code>display: none</code>).</p>
20+
<ul>
21+
<li><strong>Layout (Reflow)</strong> — computing the size and coordinates of each element relative to the viewport.</li>
22+
<li><strong>Paint</strong> — filling in pixels: text, colors, borders, shadows, backgrounds.</li>
23+
<li><strong>Composite</strong> — assembling the layers in the right order on the GPU into the final image.</li>
24+
</ul>
25+
26+
<h3>DOMContentLoaded vs load</h3>
27+
<ul>
28+
<li><code>DOMContentLoaded</code> — the HTML is loaded and parsed into the DOM, without waiting for stylesheets, images, and frames.</li>
29+
<li><code>load</code> — the page is fully loaded with all dependent resources (images, styles, frames).</li>
30+
</ul>
31+
32+
<p class="deep-dive">Deep Dive</p>
33+
34+
<h3>Preload Scanner</h3>
35+
<p>While the main parser is stalled on a synchronous script, the auxiliary <span class="accent">Preload Scanner</span> "runs ahead" through the HTML, finds external resources (CSS, JS, images, fonts), and starts downloading them in advance — so one blocking script doesn't halt the entire load.</p>
36+
37+
<h3>Why CSS is not parsed incrementally</h3>
38+
<p>Unlike HTML, the CSSOM cannot be built in pieces: a rule at the end of the file may override a rule from the beginning (the cascade). So the browser waits for all the CSS before considering the CSSOM ready.</p>
39+
<p class="info info--orange">A synchronous script after a <code>&lt;link&gt;</code> waits for the CSSOM to be built: JS can read computed styles, so it won't run until the CSS is loaded — this is how CSS can indirectly block parsing too.</p>
40+
41+
<h3>What ends up in the Render Tree</h3>
42+
<ul>
43+
<li><code>display: none</code> — the node is excluded from the tree: it isn't rendered at all.</li>
44+
<li><code>visibility: hidden</code> and <code>opacity: 0</code> — stay in the tree: the elements are invisible but still occupy space and affect geometry.</li>
45+
</ul>
46+
47+
<h3>Reflow and Repaint</h3>
48+
<p><span class="accent">Reflow</span> — a repeated Layout: recomputing geometry when sizes or positions change (window resize, node insertion, reading <code>offsetHeight</code>). Expensive, since it can cascade to both descendants and ancestors.</p>
49+
<p><span class="accent">Repaint</span> — redrawing without a geometry change (color, background change). Cheaper than reflow, but still loads the CPU.</p>
50+
<p class="info info--blue">Animating only via <code>transform</code> and <code>opacity</code> changes just the Composite stage, skipping Layout and Paint — so it runs on the GPU and holds 60 FPS (more in the question on animation performance).</p>
51+
52+
<h3>Layers and compositing</h3>
53+
<p>Elements with <code>transform</code>, <code>opacity</code>, <code>will-change</code>, or <code>z-index</code> may be promoted to a separate composite layer. The browser rasterizes layers independently and stitches them on the GPU — this lets it move a layer without repainting the rest of the page.</p>

src/assets/content/eng/questions.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@ export const questions = [
1818
name: 'Critical Rendering Path',
1919
},
2020
{
21-
name: 'How the browser builds the DOM, CSSOM, and Render Tree',
21+
name: 'Parsing HTML → DOM, CSS → CSSOM',
2222
},
2323
{
24-
name: 'How the browser executes JavaScript and how it affects rendering',
24+
name: 'JavaScript blocks parsing (defer / async)',
2525
},
2626
{
27-
name: 'What is Layout / Reflow',
27+
name: 'Render Tree → Layout → Paint → Composite',
2828
},
2929
{
30-
name: 'What is Repaint',
31-
},
32-
{
33-
name: 'Composite',
34-
},
35-
{
36-
name: 'Events DOMContentLoaded and Load - what is the difference?',
30+
name: 'DOMContentLoaded vs load',
3731
}
3832
]
3933
},

0 commit comments

Comments
 (0)