Question presented to candidate: "After enabling SSR our console shows NG0500 hydration errors on some pages. Walk me through how Angular hydration actually matches the server DOM, what counts as a mismatch, and how you would track one down."
What a strong answer should cover:
- The server writes hydration annotations:
nghattributes, the__nghData__map in theng-statescript, comment anchors, and thenghmintegrity marker. - In the browser Angular walks the DOM and claims nodes, checking the element tag or node type at each position.
- Not mismatches: different text values and different
@ifbranches (Angular patches or re-creates them). - Mismatches (
NG0500): invalid HTML repaired by the parser (<tr>without<tbody>), direct DOM manipulation, third-party scripts and browser extensions. Removed comments giveNG0507. - Reading the error's expected vs actual DOM diagram and the component name it points to.
- Fix order: valid HTML, DOM work in
afterNextRender,ngSkipHydrationas the last resort.
Clarifying questions expected:
- "Does it happen on every load, or only for some users (extensions, A/B scripts)?"
- "Is anything between the server and browser rewriting the HTML, such as a CDN optimiser?"
Code / implementation expected: Optional — reproduce one mismatch and fix it.