The Real Fullstack

The Hydration Mismatch

The page looks right for half a second — then the greeting, the cart badge and the offer date all change. Why?

シニアエンジニア8 分デバッグラボコード課題
angularssrnextjslocalstorage

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション: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.
シミュレーションシミュレーション値
SSR serverCDNbrowser (hydration)
シミュレーションを実行
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');

簡略化

Three symptoms with deterministic fixes. Frameworks differ in how they report mismatches (Angular NG0500, React hydration warnings). 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

関連する課題

参考資料