Lesson 15 / الدرس 15

How a page is built / كيف تُبنى الصفحة

The response arrived and it is a wall of text. What the browser does between that text and the picture on your screen explains most of what makes a site feel fast or slow.

وصلت الاستجابة وهي جدار من النص. وما يفعله المتصفح بين ذلك النص والصورة على شاشتك يفسّر معظم ما يجعل الموقع يبدو سريعًا أو بطيئًا.

The body of the response is HTML — the same text you wrote by hand for a whole course. The browser reads it from the top, and as it reads it builds a tree in memory: this element is inside that one, which is inside the body. That tree is the DOM, and everything afterwards works on the tree rather than on the text.

Five steps from text to picture

  1. Parse the HTML into the tree of elements.
  2. Parse the CSS into a matching set of rules, and work out which rule wins for each element — the cascade from the CSS course, happening here.
  3. Lay out — decide where every box goes and how big it is. This is what the box model was describing.
  4. Paint — draw the pixels: colours, text, borders, shadows.
  5. Composite — stack the painted layers into the final image you see.

The browser does not wait for the whole file before starting — it parses as the bytes arrive, which is why a slow page appears in pieces rather than all at once. But two things interrupt that flow, and they are the reason a page can be blank for a second while it is clearly still loading.

What blocks, and why

In the headEffectWhy
A stylesheetBlocks paintingPainting before the rules arrive would show unstyled text
A script, plainBlocks parsingIt might rewrite the page, so parsing stops until it runs
A script, deferDoes not blockRuns after parsing, in order
A script, asyncDoes not blockRuns whenever it arrives — order not guaranteed
An imageDoes not blockLayout leaves a gap and fills it later

This table is most of web performance advice, compressed. Stylesheets belong in the head so they arrive early; scripts belong at the end of the body, or in the head with defer.

<img src="photo.jpg" width="600" height="400" alt="A photo">
Run it. The two dashed boxes are what layout has to work with before an image arrives: the first was given dimensions and holds its space, the second was not and is flat. Watch where the line of text under each one sits. When the picture finally loads, the second box inflates and that text is shoved down the page — the jump you have felt on news sites.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Why does a stylesheet in the head delay the first paint?

2. The page content jumps down as it loads. Most likely cause?

3. Which step turns the rules and the tree into positions and sizes?

Your task / مهمتك

Draw the five steps as a page, one section each, saying in your own words what goes in and what comes out. Then add a short section listing what blocks and what does not, and finish with the two changes you would make to a slow page first.

ارسم الخطوات الخمس في صفحة، قسم لكل واحدة، وقل بكلماتك ما الذي يدخل وما الذي يخرج. ثم أضف قسمًا قصيرًا يعدّد ما يعطّل وما لا يعطّل، واختم بالتغييرين اللذين ستجريهما على صفحة بطيئة أولًا.

  • All five steps, in order, in your own words الخطوات الخمس كلها بالترتيب وبكلماتك
  • A list of what blocks and what does not قائمة بما يعطّل وما لا يعطّل
  • Two concrete first changes for a slow page تغييران محددان يبدأ بهما لصفحة بطيئة
How do you want to submit? / كيف تريد التسليم؟