Web Performance Optimization with NgOptimizedImage in Angular

Leader posted 1 min read

Optimizing web performance is crucial for providing a fast and smooth user experience. One key metric to focus on is the Largest Contentful Paint (LCP), which measures how quickly the main content of a page loads.

Angular’s NgOptimizedImage directive helps improve LCP and overall image loading performance by intelligently handling image priorities and loading behavior.

How NgOptimizedImage Boosts Performance

  • Prioritizes LCP images:
    Automatically sets the fetchpriority attribute to high for above-the-fold images (the main images visible when the page loads). This ensures these images are fetched and rendered faster.

  • Lazy loads other images:
    Images below the fold are loaded lazily by default, reducing initial page load time and saving bandwidth.

  • Preconnect optimization:
    Automatically generates a preconnect link tag in the document’s <head>, establishing early connections to image hosts for faster fetching.

  • Smart loading attributes:
    The loading attribute is set to eager for above-the-fold images (LCP image) to load immediately, while images below the fold are set to lazy, deferring their loading until needed.

  • Responsive images support:
    Automatically generates a srcset attribute for images, enabling browsers to choose the best image size based on the device’s screen resolution and viewport.

Why It Matters

These optimizations collectively improve your app’s perceived speed and user experience by reducing the time taken to render the most important visual content.

How to Measure LCP

You can check your page’s Largest Contentful Paint using Chrome DevTools:

  1. Open Chrome DevTools (F12 or Ctrl+Shift+I)
  2. Go to the Performance tab
  3. Record a page load and look for the LCP marker in the summary or timings

By integrating NgOptimizedImage in your Angular app, you can enhance image loading strategies with minimal effort — making your application faster and more efficient.


Usage: You can add this in your component-

   import { IMAGE_CONFIG, IMAGE_LOADER, ImageLoaderConfig, NgOptimizedImage } from '@angular/common';

   imports: [NgOptimizedImage],

    <img ngSrc="https://example.com/image.jpg" 
          [ngSrcset]="['https://example.com/image-sm.jpg 1x', 'https://example.com/image-lg.jpg 2x']" 
          width="300" height="200"
          alt="Example Image">
If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Unlocking the Power of the Directive Composition API in Angular

Sunny - Jun 11

Role of Web Performance in the Web Architecture

Sunny - Jul 8

Leveraging service workers to enhance web app performance and reliability

Sunny - Jun 8

Implementing Secure Authentication in Modern Web Apps: OAuth 2.0 & JWT with Angular and Node.js

Sunny - Oct 2

How DomSanitizer works to prevent Cross-Site Scripting (XSS) attacks in Angular

Sunny - Aug 23
chevron_left