Under The Hood
frontend angular Angular 15+ (NgOptimizedImage, image directive)

NgOptimizedImage: Priority Preloading & Layout Shifting

Last updated
Prerequisites:
Angular directives and template basics
Understanding of Core Web Vitals (LCP)
  • angular
  • images
  • lcp
  • ngoptimizedimage
  • performance
  • lazy-loading

Read at your depth

The practical view

NgOptimizedImage is Angular's image directive (import { NgOptimizedImage } from '@angular/common'). Add priority to the LCP image: <img ngSrc="/hero.png" width="800" height="400" priority>. It sets fetchpriority=high, preloads the image, and lazy-loads non-priority images (loading=lazy). You must provide width/height (or fill) — this prevents layout shift by reserving space. Use ngSrc instead of src to get automatic srcset/optimization via the configured image loader. Include it in standalone imports: imports: [NgOptimizedImage].

The same idea in other frameworks

react equivalent
next/image and React's img loading hints cover the same LCP/CLS ground.

Legacy vs modern

Plain img with manual attributes vs NgOptimizedImage

Hand-optimizing images requires many manual attributes and is easy to get wrong; NgOptimizedImage encodes the LCP/CLS rules and enforces dimensions.

before → after
Plain img with manual attrs
<img src="/hero.png"> <!-- ❌ OLD: no dimensions, no preload, no lazy → CLS + slow LCP -->
NgOptimizedImage
<img ngSrc="/hero.png" width="800" height="400" priority> <!-- ✅ NEW -->

Interview gotchas

Context

This tests the CLS mechanism — interviewers want the reserved-space rationale and the aspect-ratio behavior, not just 'it's a lint rule'.

The mechanical answer

Browsers do not know an image's intrinsic size until it downloads. Without width/height, the image occupies 0 height initially; when it loads, the layout reflows to fit it, pushing content down — Cumulative Layout Shift. Providing width and height lets the browser compute the aspect ratio and reserve the box before the image arrives, so no reflow occurs. NgOptimizedImage enforces this (and the fill mode, which uses CSS aspect-ratio positioning) precisely to eliminate CLS. The lint rule NGD001 fails the build if an ngSrc image lacks dimensions, catching the mistake at compile time rather than letting it degrade Core Web Vitals in production.

Trap

The naive answer is 'dimensions are just for accessibility'. They are the CLS prevention mechanism. Another trap: thinking width/height must match the displayed CSS size exactly — they only need to encode the correct aspect ratio; CSS (max-width: 100%; height: auto) scales responsively from the ratio. Candidates who explain the aspect-ratio box reservation demonstrate real CLS knowledge.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.