Skip to the page

Production and testing · senior

Bundle size, @defer, server rendering and hydration

Measure the bundle, lazy load what is not needed first, defer parts of templates, and keep server and browser DOM equal for hydration.

The notes

What you must know

  • Measure: ng build --stats-json with a bundle analyser, and Lighthouse or WebPageTest on a throttled phone.
  • Usual causes of a big initial bundle: eagerly imported feature components, barrel files that pull heavy code, a date library with every locale.
  • @defer loads a block (its components, directives and pipes) only when a trigger fires: on viewport, on interaction, on hover, on idle (default), on timer, when condition. Use prefetch to download earlier.
  • Deferred dependencies must be standalone, and must not be used outside the block, or they stay in the main bundle.
  • Hydration reuses the server DOM. NG0500 means a node did not match. Causes: direct DOM changes (innerHTML, jQuery), invalid HTML nesting, browser-only code run on the server.
  • The server waits until the app is stable before sending HTML. An endless interval or timer started during rendering makes the request hang.
  • Start timers only in the browser, for example after render.

The red pen

Common traps: mistake → fix

  • Mistake: @defer always reduces the bundle

    Fix: only if its dependencies are standalone and not used elsewhere eagerly.

  • Mistake: SSR hydration mismatch is only cosmetic

    Fix: it can re-render or break the part.

Easy to hard

Questions interviewers ask, easy to hard

  1. The bundle is too big. What do you check?
  2. When does @defer not make a page lighter?
  3. What causes NG0500?
  4. The server-rendered home page hangs. Why?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview