NG.IX · Angular 22 practice question
Given this Angular 22 component used in an SSR application, how does Angular execute the callback passed to afterNextRender?
import { Component, afterNextRender } from '@angular/core';
@Component({
selector: 'app-viewport-tracker',
template: '<p>Viewport ready</p>',
})
export class ViewportTrackerComponent {
constructor() {
afterNextRender(() => {
console.log(window.innerWidth);
});
}
}
Check answer and explanation
Correct answer: Angular skips the callback during server rendering and executes it only in the browser.
Explanation
Lifecycle hooks such as afterNextRender and afterEveryRender are skipped on the server and run exclusively in the browser, making them safe places to call browser-only APIs like window.
Common trap
Expecting Angular to supply a mocked window object on the server is tempting if you are used to browser-emulation test runners, but Angular skips afterNextRender entirely during SSR instead of mocking browser globals. Thinking window causes a compile error in SSR projects is wrong because Angular allows browser symbols inside browser-only hooks like afterNextRender.
Source: Angular docs: Server and hybrid rendering — Authoring server-compatible components
Checked against the cited text on 2026-11-25.