GuideCoreUpdated in v15

Server-side rendering

Prerender and hydrate an ngwr app without mismatches.

Hydration and event replay

ngwr needs nothing extra; turn on Angular's hydration as usual.

import { provideClientHydration, withEventReplay } from '@angular/platform-browser';

bootstrapApplication(AppComponent, {
  providers: [
    provideZonelessChangeDetection(),
    provideClientHydration(withEventReplay()),
  ],
});

Incremental hydration

On by default in Angular 22; add a hydrate trigger to a heavy block whose JavaScript can wait.

<!-- Server-rendered as usual; only the JavaScript waits. -->
@defer (hydrate on viewport) {
  <section class="marketing">
    <wr-count-up [to]="12000" trigger="visible" />
  </section>
}

Write the <tbody> yourself

Rows placed straight in a <table> get an implicit <tbody> from the browser, and hydration fails with NG0501.

<!-- Wrong: hydration fails with NG0501 -->
<table>
  @for (row of rows(); track row.id) {
    <tr>…</tr>
  }
</table>

<!-- Right -->
<table>
  <tbody>
    @for (row of rows(); track row.id) {
      <tr>…</tr>
    }
  </tbody>
</table>

Reflow in CSS, not in a template branch

A breakpoint signal is false on the server, so reflow with the responsive modifiers instead.

<!-- Right: one DOM on both sides -->
<wr-table responsive [columns]="cols" [items]="rows" />
<wr-pagination responsive [(page)]="page" [total]="total()" />

<!-- Wrong: the server always renders the narrow branch -->
@if (isMd()) {
  <aside class="filters">…</aside>
}

What the server can answer

APIOn the server
WrPlatform.isBrowser / isServerfalse / true
WrPlatform.userAgentnull
WrPlatform.prefersDark()false
WrTheme.resolved()'light' with the default config
WrStorage.get(key, fallback)the fallback
WrHaptics.supportedfalse
WrTour.start(steps)does nothing

Overlays are not no-ops on the server

WrToast.show() and WrDialog.open() also run on the server and ship the overlay in the HTML, so call them from afterNextRender.

import { afterNextRender, inject } from '@angular/core';
import { WrToast } from 'ngwr/toast';

export class Welcome {
  private readonly toast = inject(WrToast);

  constructor() {
    afterNextRender(() => this.toast.show({ message: 'Welcome back' }));
  }
}

A virtualized table on the server

Pass viewportHeight as a number and pin rowHeight, so the server's first window matches the client's.

<wr-table
  virtualScroll
  [viewportHeight]="400"
  [rowHeight]="45"
  [columns]="cols"
  [items]="rows"
/>

The theme on a prerendered page

auto needs nothing; for a saved light or dark choice, add the pre-paint script to <head>.

import { wrThemePrePaintScript } from 'ngwr/theme';

// In <head>, above every stylesheet.
const html = template.replace('<!--theme-->', `<script>${wrThemePrePaintScript()}</script>`);

See also