The Hydration Mismatch
The page looks right for half a second — then the greeting, the cart badge and the offer date all change. Why?
لم يُترجم هذا التحدي إلى العربية بعد. أنت تقرأ النسخة الإنجليزية.
المهمة:Find why server HTML and client rendering disagree, and make the first client render match the server while still showing personal data.
Hollow Basket's Angular storefront is server-rendered for speed and SEO. The header reads the user's name and cart from localStorage and formats dates with the local clock — on the server and again in the browser. Users see a flicker, the console reports hydration errors and layout shift is high.
المهمة: Find why server HTML and client rendering disagree, and make the first client render match the server while still showing personal data.
أهداف التعلّم
- Understand hydration: the client must reproduce the server's HTML first.
- Read browser-only state after hydration.
- Make dates and time zones deterministic.
المحاكاةمحاكاة
readonly name = signal<string | null>(null); // same initial value on server and clientreadonly cartCount = signal<number | null>(null);constructor() { afterNextRender(() => { // browser only, after hydration this.name.set(localStorage.getItem('name')); this.cartCount.set(JSON.parse(localStorage.getItem('cart') ?? '[]').length); });}// dates: format with an explicit time zone, or render them on the client tooreadonly offerEnds = formatDate(OFFER_END, 'mediumDate', 'en', 'UTC');