
هل المتصفح بيرمي الـ HTML اللي جاي من الـ server؟ الـ hydration في Angular
- كاتب موثّق
- Angular 22
محتويات المقال
ngOnInit بـ createElement() وinsertBefore(). حد يحط ngSkipHydration على الـ root component عشان يسكّت الـ error، وفعلًا الـ NG0500 اختفى. بس دلوقتي الصفحة بترمش مرة وهي بتخلص تحميل، لأن الـ attribute ده رجّع بالظبط اللي التيم كان فاكر إن الـ SSR بيعمله على طول: يبني الصفحة من الأول.
الفكرة الغلط الشائعة
console.log اللي في constructor بيظهر مرة في الـ terminal بتاع الـ server ومرة تانية في الـ console بتاع المتصفح. والفكرة دي بتوصف mode حقيقي في Angular. الـ guide بيقول إن من غير الـ hydration، تطبيق Angular اللي عامل SSR "will destroy and re-render the application's DOM"، وده ممكن يعمل رمشة باينة. فنص الحقيقة هنا إن كود الـ components بيشتغل مرتين، بس الـ DOM مش لازم يتبني مرتين.
اللي بيحصل فعلًا
الـ server بيبعت HTML ومعاه خريطة ليه
@if و@for. والصفحة كمان ممكن تشيل داتا، زي responses الـ HttpClient اللي الـ server جابها، عشان المتصفح مايطلبهاش تاني.
المتصفح بيشغّل الـ components بس بياخد الـ DOM الجاهز
ngOnInit بيشتغلوا، والـ signals والـ inputs بتاخد قيمها. الفرق في الخطوة اللي بتعمل الـ elements. وبكلام الـ guide، Angular "tries to match existing DOM elements to the application's structure at runtime and reuses DOM nodes when possible". يعني بياخد الـ <div> اللي جه من الـ server بدل ما يعمل واحد جديد، ويربط عليه الـ listeners. وبعد الـ hydration، فيه خطوة cleanup بتشيل الـ nodes اللي محدش أخدها.
لحد ما يخلص، الصفحة مجرد صورة
provideClientHydration() بيشغّله بشكل افتراضي، فالضغطة البدري على "Add to cart" مابتضيعش.
الاتفاق: نفس الـ DOM في الناحيتين
التعديل المباشر في الـ DOM بالـ native APIs: createElement()وappendChild()، أو إنك تحطinnerHTML، أو تنقل nodes من مكانها.HTML مش valid والمتصفح بيصلّحه وهو بيعمل parse: <table>من غير<tbody>، أو<div>جوه<p>، أو<a>جوه<a>تاني.templates بتتفرع حسب الـ platform، زي @ifمعisPlatformBrowser، فالمتصفح يعمل render لشكل غير اللي الـ server عمله.
import { Component, DOCUMENT, ElementRef, inject, input, OnInit,} from '@angular/core';@Component({ selector: 'app-price', template: `<div class="price">{{ amount() }}</div>`,})export class Price implements OnInit { readonly amount = input.required<string>(); readonly onSale = input(false); private readonly host = inject(ElementRef<HTMLElement>).nativeElement; private readonly doc = inject(DOCUMENT); ngOnInit() { if (!this.onSale()) return; // Angular has no record of this node, so the DOM no longer // matches what the server annotated: hydration fails (NG0500) const badge = this.doc.createElement('span'); badge.textContent = 'Sale'; this.host.insertBefore(badge, this.host.firstChild); }}import { Component, input } from '@angular/core';@Component({ selector: 'app-price', template: ` @if (onSale()) { <span class="badge">Sale</span> } <div class="price">{{ amount() }}</div> `,})export class Price { // Same inputs on the server and in the browser, so the same // DOM: hydration claims the <span> instead of rebuilding it readonly amount = input.required<string>(); readonly onSale = input(false);}@if، والمتصفح بيوصل لنفس الفرع بنفس الـ inputs، والـ hydration بياخد الـ <span> زي أي node تانية. ولو محتاج فعلًا شغل DOM بإيدك، زي chart library، شغّله جوه afterNextRender()، اللي بيشتغل في المتصفح بس بعد الـ render. والـ guide بيدي نفس النصيحة للـ third-party scripts اللي بتلمس الـ DOM.
الـ ngSkipHydration بيرجّع السلوك القديم
ngSkipHydration على الـ host بتاع component بيخلّي Angular يسيب الـ hydration بتاعها هي وكل اللي تحتها. والـ guide بيقول إن الـ component دي "will destroy and re-render itself"، وإنك لو حطيته على الـ root component فانت كده عمليًا قفلت الـ hydration للتطبيق كله. ده حل مؤقت تلجأله في الآخر خالص، والـ guide بيعتبر الـ components اللي محتاجاه bugs لازم تتصلح.
ngSkipHydration حل |
تطبقها إزاي في مشروعك
اتأكد إن الـ hydration شغال المشاريع اللي اتعملت بـ ng new --ssrأوng add @angular/ssrبتناديprovideClientHydration()أصلًا. ولو الـ setup بتاعك custom، اتأكد إنه موجود في الـ providers بتاعة المتصفح وبتاعة الـ server الاتنين.انقل تعديلات الـ DOM للـ templates دوّر في الـ components على createElementوappendChildوinsertBeforeوinnerHTML. حوّل كل واحدة لـ markup في الـ template، أو انقلها جوهafterNextRender()لو فيه library لازم تمسك الـ DOM ده.راجع الـ HTML بتاعك حط <tbody>صريح في كل table، ودوّر على block elements جوه<p>وعلى links جوه links.اعتبر كل ngSkipHydration bug اعمل list بكل استخدام، وشيله أول ما الـ component اللي بيخبّيها تبطّل تعدّل في الـ DOM. وأوعى تحطه على الـ root component خالص.
الخلاصة
الـ hydration بيعيد استخدام الـ DOM nodes بتاعة الـ server، ومن غيره Angular بيهد الصفحة ويعملها render من جديد. كود الـ components بتاعك لسه بيشتغل في المتصفح. اللي بيتشال بس هو بناء الـ DOM. الـ listeners بتتربط لما الـ hydration يخلص، والـ event replay، الشغال افتراضيًا في Angular 22، بيحفظ الضغطات البدري. الـ server والمتصفح لازم يطلّعوا نفس الـ DOM، فعدّله من الـ templates مش من الـ native APIs. الـ ngSkipHydrationمخرج مؤقت بيرجّع الرمشة للـ subtree دي.
التعليقات (0)