← Back to Angular, page 2

angular-0037 · Hard senior

What is incremental hydration, and how do the hydrate triggers on @defer blocks work?

Incremental hydration lets the server render the whole page but the browser hydrate selected @defer blocks only when a hydrate trigger fires, such as viewport, interaction, or hover. Until then the block stays as static server HTML and its JavaScript is not downloaded, so less script runs at start-up.

Full answer

Normal hydration wakes up the entire page when the application starts. On a long page with comments, a footer, and a recommendation list, most of that work is wasted for a user who only reads the top.

Incremental hydration (stable in Angular 20) hydrates parts of the page later. You enable it with provideClientHydration(withIncrementalHydration()) and mark deferrable blocks with hydrate triggers:

html
@defer (hydrate on viewport) { <app-comments /> }

What happens: on the server the block renders its real content (not the placeholder, which is what a plain @defer does on the server). In the browser Angular leaves that HTML untouched and does not load the component code. When the trigger fires, it downloads the chunk and hydrates the existing DOM. Clicks before that are captured and replayed, because incremental hydration uses event replay.

Hydrate triggers: hydrate on idle, on viewport, on interaction, on hover, on immediate, on timer(ms), hydrate when condition, and hydrate never (the block stays static HTML forever, which suits content with no interactivity).

You can combine hydrate triggers with normal triggers: @defer (on viewport; hydrate on interaction).

Rules to know: the parent block hydrates before its children. Content must be deterministic between server and client. Blocks are only dehydrated when the page was server-rendered. In a client-only run the block behaves like an ordinary @defer.

Trade-off: it lowers JavaScript at start-up (better Interaction to Next Paint and Total Blocking Time), but the first interaction with a dehydrated block waits for download and hydration. Keep primary actions (add to cart) hydrated early, and defer decorative or below-the-fold parts.

typescript
// file: app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideClientHydration, withIncrementalHydration } from '@angular/platform-browser';

export const appConfig: ApplicationConfig = {
  providers: [provideClientHydration(withIncrementalHydration())],
};

// file: comments.component.ts
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-comments',
  template: `
    <button (click)="like()">Like ({{ likes() }})</button>
  `,
})
export class CommentsComponent {
  readonly likes = signal(0);

  like(): void {
    this.likes.update((n) => n + 1);
  }
}

// file: article.component.ts
import { Component, signal } from '@angular/core';
import { CommentsComponent } from './comments.component';

@Component({
  selector: 'app-article',
  imports: [CommentsComponent],
  template: `
    <article>
      <h1>How exams are scheduled</h1>
      <p>Static text that never needs JavaScript.</p>
    </article>

    @defer (on viewport; hydrate on interaction) {
      <app-comments />
    } @placeholder {
      <p>Comments</p>
    }

    @defer (hydrate when showFooterTools()) {
      <app-comments />
    }

    @defer (hydrate never) {
      <footer>Copyright notice (static text only)</footer>
    }
  `,
})
export class ArticleComponent {
  readonly showFooterTools = signal(false);
}

Angular 20.3. Needs the SSR setup from `ng add @angular/ssr`. The comments component is a stand-in.

Key points

  • Enable with `withIncrementalHydration()`. Mark blocks with `@defer (hydrate on ...)`.
  • The server renders the real content; the browser hydrates it only when the trigger fires.
  • Triggers include `idle`, `viewport`, `interaction`, `hover`, `immediate`, `timer`, `when`, and `never`.
  • Parents hydrate before children. Early user events are replayed.

Common mistakes

  • Using `hydrate never` on a block that has buttons or forms, so it never becomes interactive.
  • Deferring the main call-to-action, which delays the first meaningful interaction.
  • Expecting it to work without SSR. Without a server render there is nothing to keep dehydrated.

Follow-ups

  • How is this different from the plain `@defer` placeholder behaviour on the server?
  • What happens to a click inside a dehydrated block?
  • How would you decide which blocks to hydrate on viewport?

Tips

  • Contrast it with full hydration and name the metric it improves (start-up JavaScript, INP, TBT).
  • Mention the trade-off. Senior answers always include one.
  • Say it needs SSR and event replay.

Evaluation rubric

  • Understands dehydrated versus hydrated blocks.
  • Lists hydrate triggers and combinations.
  • Weighs the trade-offs.