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
-
Parse the HTML into the tree of elements. تحليل HTML إلى شجرة العناصر.
-
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. تحليل CSS إلى مجموعة قواعد مقابلة، ومعرفة أي قاعدة تفوز لكل عنصر — وهو التتالي من دورة CSS، يحدث هنا.
-
Lay out — decide where every box goes and how big it is. This is what the box model was describing. التخطيط — تحديد مكان كل صندوق وحجمه. وهذا ما كان نموذج الصندوق يصفه.
-
Paint — draw the pixels: colours, text, borders, shadows. الطلاء — رسم البكسلات: الألوان والنصوص والحدود والظلال.
-
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 head | Effect | Why |
|---|---|---|
| A stylesheet | Blocks painting | Painting before the rules arrive would show unstyled text |
| A script, plain | Blocks parsing | It might rewrite the page, so parsing stops until it runs |
| A script, defer | Does not block | Runs after parsing, in order |
| A script, async | Does not block | Runs whenever it arrives — order not guaranteed |
| An image | Does not block | Layout 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.
defer.width وheight تجعل خطوة التخطيط تخمّن. فلا تُحجز المساحة إلا حين تصل الصورة أخيرًا، فيقفز كل ما تحتها إلى أسفل — عادةً في اللحظة التي كان القارئ يوشك أن ينقر فيها شيئًا. وإعطاء السمتين، حتى حين تعيد CSS تحجيم الصورة بعدها، يتيح للتخطيط أن يحجز المساحة الصحيحة من أول مرة.<img src="photo.jpg" width="600" height="400" alt="A photo">
Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. Why does a stylesheet in the head delay the first paint?
The browser would rather wait a moment than show unstyled text and then redraw it. That is why the stylesheet belongs early, where it arrives soonest.
2. The page content jumps down as it loads. Most likely cause?
Layout can only reserve space it knows about. The dimensions let it get the page right the first time, before the image itself arrives.
3. Which step turns the rules and the tree into positions and sizes?
Layout is where every box gets its place and its size — and it is the expensive step, which is why changing a width costs more than changing a colour.
Score / النتيجة: 0 / 3
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 تغييران محددان يبدأ بهما لصفحة بطيئة