Sub topic 10
Replaying user events captured before hydration
This chapter assumes you know how to enable hydration with provideClientHydration and how server-rendered HTML becomes an interactive Angular app. You should also be comfortable with standalone components and ApplicationConfig.
The gap between visible and interactive
With SSR, the browser shows the server-rendered HTML as soon as it arrives. The page looks ready, but Angular has not yet downloaded and run its JavaScript. Until hydration finishes, the buttons and links in that HTML have no Angular listeners attached.
If a user clicks during this gap, the click is lost. The user sees a button, presses it, and nothing happens. Event replay closes this gap.
How event replay works
Event replay uses a small script that Angular inlines into the server-rendered page. It runs before your application bundle loads.
- The script registers listeners at the document level for the events your templates use.
- When a user interacts with the page, it records each event and its target element.
- Once the application hydrates, Angular attaches the real listeners.
- Angular then replays the recorded events against the matching elements, so the handlers run as if the user had just clicked.
Enabling event replay
Add withEventReplay() to provideClientHydration. It goes in the shared application config, so the server build includes the capture script.
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withFetch()),
provideClientHydration(withEventReplay()),
],
};withEventReplay() needs hydration, so it is passed as a feature of provideClientHydration. It does not work as a separate provider. If you create a new project with the CLI and choose SSR, recent Angular versions add it for you. In an older project, check the config.
What gets replayed
The capture script only records events that your templates listen for. It finds them by reading the Angular template bindings during server rendering. A component with (click) and (keydown) bindings makes the script capture those two event types.
A quick check of the behavior:
- A click on a button with a
(click)handler is recorded and replayed. - Events on elements with no Angular binding are ignored.
- Events are replayed in the order they happened.
- Replay happens per element, after that element's component is hydrated.
The last point matters most once you use incremental hydration. If a click lands inside a deferred block that has not hydrated, Angular holds the event and triggers hydration of that block. It then replays the event once the block is ready. The next chapter covers this in detail.
Behavior to watch for
Replay runs handlers late, so handlers should not assume they ran at the moment of the click.
- Time-sensitive logic. A handler that reads
Date.now()or the current scroll position sees the values at replay time, not at click time. - Navigation links. A click on a
routerLinkis replayed after hydration, so the navigation happens a moment later than the user expects. This is still better than a dead link. - Repeated clicks. A user who clicks several times while waiting produces several replayed events. For a handler like "submit order", guard against double runs, for example by disabling the button once the first call starts.
- Forms. Replay covers events, not typed text. The browser keeps what the user typed in an input, and hydration reuses the existing DOM node, so the value stays.
A handler that tolerates replay
This component guards against the double-submit problem that replay can make more visible.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-buy-button',
template: `
<button (click)="buy()" [disabled]="pending()">
{{ pending() ? 'Sending...' : 'Buy now' }}
</button>
`,
})
export class BuyButtonComponent {
protected pending = signal(false);
buy() {
if (this.pending()) {
return;
}
this.pending.set(true);
// Call your order service here, then reset pending when it finishes.
}
}The pending check makes the handler safe whether the click arrives live or from the replay queue.
Verifying it works
- Build and serve the SSR app.
- In the browser's developer tools, throttle the network to a slow profile and disable the cache.
- Reload, and click a button as soon as it appears, before the bundle finishes loading.
- When hydration completes, the handler should run once without a second click.
Without withEventReplay(), the same test loses the click.
Exercise
A user clicks a "Like" button three times while the page is still loading, and the app uses withEventReplay(). What happens when hydration completes, and what would you change in the handler to make this safe?
Show answer
Angular replays all three recorded click events in order, so the handler runs three times. To make it safe, make the handler idempotent or guard it, for example by ignoring calls while a request is pending or by disabling the button after the first call.