The page looked fine to me, to QA, and to every logged-out visitor. Then a logged-in user on a slower connection described the header jumping on load — "Welcome" snapping to her name a beat after the page appeared. Once I reproduced it with the right account, the console had no errors and no warnings, just Angular's usual hydration stats line. Angular hadn't complained at all. It had quietly done the only safe thing it could: the server had sent one version of the header, the browser had built a different one, and Angular threw the server's markup away and rebuilt it.
The reason it only happened to some users is the whole lesson. The greeting in the header read the user's name from localStorage and, when it found one, rendered a different branch: "Welcome back, Dana," with a link to her account. On the server, there is no localStorage, so it rendered the generic "Welcome." In the browser, the name was right there, so it took the personalized branch. Same component, two inputs, two different DOMs — and hydration's one firm rule is that the first thing the browser renders must match what the server sent.
A logged-out user had no name to read, so both renders produced "Welcome," and hydration was happy. The bug was hiding behind exactly the state that made the feature worth building.
Hydration is a contract about the first render, not the final one
It helps to be precise about what hydration is doing. The server sends real HTML so the page is meaningful before any JavaScript runs. Then Angular boots in the browser and, instead of rebuilding that DOM, it adopts it — walks the existing nodes and wires up the bindings in place. That adoption only works if the component renders the same structure the server did. A difference in text alone is harmless: Angular keeps the server's node and updates the words in place. A different shape is another matter. When an @if resolves the other way, Angular can't reuse the server's nodes for it, so it destroys that subtree and re-creates it, without an error or a warning. (The hydration guide's wording suggests any mismatch throws; in practice Angular saves its loud hydration error, NG0500, for DOM it can't account for at all, like nodes inserted outside its templates.) That rebuild is a real tax: the server-rendered HTML you paid to produce gets discarded the moment it mattered.
So anything that changes the shape of the first render based on something the server can't see — localStorage, a cookie read in the browser, window, the current time, a random id — is a hydration mismatch waiting for the one user whose value differs from the server's default. The feature works on your machine because your machine has the value. The server never does.
I had been thinking of SSR as "the same app, rendered twice." It's closer to two environments that have agreed to produce an identical first frame, and the agreement breaks the instant my code reads something only one of them has.
Keep the first render neutral; personalize after hydration
The fix is not to abandon the personalization. It's to keep it out of the first render, let the server and client agree on the neutral version, and apply the personal touch after hydration has safely adopted the DOM. Angular gives you a browser-only hook for exactly this — afterNextRender runs only in the browser, only after that first render is committed:
@Component({
selector: 'app-greeting',
imports: [RouterLink],
template: `
@if (name(); as name) {
<p>Welcome back, <a routerLink="/account">{{ name }}</a></p>
} @else {
<p>Welcome</p>
}
`,
})
export class Greeting {
protected readonly name = signal<string | null>(null); // matches the server
constructor() {
afterNextRender(() => {
this.name.set(localStorage.getItem('displayName')); // browser-only, after the first render
});
}
}
The server renders "Welcome." The browser's first render also renders "Welcome," because the signal starts at null — so hydration adopts the DOM cleanly, no mismatch, no teardown. A tick later, afterNextRender reads localStorage and the @if switches branches as an ordinary update. The user still gets "Welcome back, Dana" — as one deliberate update after hydration has claimed the page, not as a mismatch Angular has to quietly repair mid-hydration. The swap itself still happens; if even that is too much, the data has to reach the first render, which is the next fix.
The other fix is worth naming, because it's better when the data exists on the server. If the personalization comes from something the server does know — a session, a logged-in API call — the right move is TransferState: render it personalized on the server, serialize that data into the page, and read it on the client so both renders match from the start. afterNextRender is for the genuinely browser-only values; TransferState is for "the server knew this, don't make the client fetch it again." Reaching for the browser-only hook when the server had the data all along buys a visible swap and a redundant request in place of a first render that could have been right.
You can watch incremental hydration adopt a server-rendered DOM in place — a below-the-fold panel hydrating on scroll without re-rendering — on the performance demo.
SSR Is Two Environments Agreeing on the First Frame
The mental shift that fixed this was giving up "the same app rendered twice" for something stricter: the server and the browser have to produce an identical first frame, and only afterward are they allowed to diverge. Every browser-only value I read during that first render is a small bet that the server happened to guess the same thing — and that bet comes due in front of the one user whose value is different. Keep the first render neutral, personalize after hydration, and the server-rendered HTML you paid for survives to do its job instead of being torn down at the finish line.



