Sub topic 12
Measuring the benefits and costs of SSR
This chapter assumes you have added SSR with @angular/ssr, enabled hydration, and chosen render modes per route, as covered earlier. Here you decide whether SSR is paying off and how to prove it with numbers.
What SSR changes
SSR moves the first render from the browser to a server. The browser receives finished HTML, so it can paint content before any JavaScript runs. The JavaScript still has to download and hydrate, so SSR improves some metrics and does nothing for others.
| Metric | Typical effect of SSR | Why |
|---|---|---|
| First Contentful Paint (FCP) | Improves | HTML already contains content |
| Largest Contentful Paint (LCP) | Often improves | The main element is in the first response |
| Time to First Byte (TTFB) | Often worsens | The server must render before it responds |
| Interaction to Next Paint (INP) | Neutral or mixed | Depends on hydration cost and event replay |
| Cumulative Layout Shift (CLS) | Can worsen | Mismatched or late-loading content can shift the layout |
Prerendered routes avoid the TTFB penalty because the HTML is built ahead of time and served as a static file. Routes with RenderMode.Server pay the rendering cost on every request.
Measure before and after
Compare the same route with SSR on and off, using a production build. Development builds are not representative.
- Build the app with
ng buildand serve it, once with SSR and once as a client-only build. - Open Chrome DevTools and use the Performance panel with CPU throttling and a slow network profile.
- Run Lighthouse on the same URL several times and take the median, because single runs vary.
- Record FCP, LCP, TTFB, INP, and CLS for each configuration.
Lab tools show what is possible on one device. To see real users, collect field data with the web-vitals library and send it to your analytics endpoint.
import { onLCP, onINP, onCLS, onTTFB, Metric } from 'web-vitals';
function report(metric: Metric): void {
navigator.sendBeacon('/api/vitals', JSON.stringify({
name: metric.name,
value: metric.value,
page: location.pathname,
}));
}
onLCP(report);
onINP(report);
onCLS(report);
onTTFB(report);Segment results by route and render mode. An average across the whole site hides the fact that a prerendered landing page may improve a lot while a server-rendered dashboard gets slower.
Costs to watch
- Server load. Each
RenderMode.Serverrequest runs your components in Node. Measure CPU use and response time under load, not only in a single request. - Slow data. The server waits for data before it responds. A slow API raises TTFB directly. Angular waits for pending HTTP requests before serializing the page.
- Payload size. The HTML plus the transferred state from the HTTP transfer cache can make the response larger. Inspect the
ng-statescript in the page source. - Hydration work. The browser still downloads and runs the application. A page that looks ready but cannot respond yet feels broken. Event replay and
@defer (hydrate on ...)reduce this window. - Complexity. Browser-only code needs guards, and hydration mismatches need debugging. Count that as a maintenance cost.
Decision criteria
SSR tends to pay off for public, content-heavy pages where search visibility and first paint matter, such as product or article pages. Prerender whatever does not change per request. Use RenderMode.Client for pages behind a login that gain little from server HTML, such as admin screens.
Treat the decision as per route. Keep a measurement for each route, and revisit it when the page or its data source changes.
Exercise
A product page uses RenderMode.Server. After enabling SSR, Lighthouse shows better FCP and LCP but a worse TTFB. Name one cause of the worse TTFB and one change that could reduce it.
Show answer
The server must fetch data and render the page before sending any HTML, so a slow API or heavy rendering delays the first byte. Options: if the page content does not depend on the request, switch the route to RenderMode.Prerender so the HTML is served as a static file; otherwise speed up the API call or cache its response on the server.
Exercise
Why should you collect field data with web-vitals in addition to running Lighthouse?
Show answer
Lighthouse measures one simulated device and network, while field data records what real users experience across devices, networks, and routes. It can reveal problems such as poor INP on slow phones that lab runs miss.