afterRender registers a callback that runs only in the browser after server-side rendering and after each application render. It prevents browser-only APIs such as window and document from executing during SSR. section 9.2.3
Use afterRender when:
- DOM updates must occur after component state changes, including manual updates in structural directives. section 9.2.3
- Browser APIs would otherwise fail during SSR with errors such as window is not defined. section 9.2.3
- A callback should run once instead; choose afterNextRender for one-time client initialization such as registering an event listener. section 9.2.3
SSR-safe DOM work
A scroll-to-top component can register browser-dependent behavior inside a render callback, keeping window access on the client. Use afterRender for repeated DOM reactions; use afterNextRender when the listener only needs registration once.
