So, I read a bunch of articles and Google docs about lazy loading images in a proper way nowadays, and found this solution:
- Use the newer
loading="lazy"in<img />tags where it's supported - Fallback to the Intersection Observer where it is not
So far so good, but when I am delivering my web app with server-side rendering (in my case an Angular 8 app with ANgular Universal enabled), and I can't verify which browser my client uses when rendering, just when the app is rendered and then the lazy loading doesn't work.
I am using this great approach from Natanel: Link
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: 'img' })
export class LazyImgDirective {
constructor({ nativeElement }: ElementRef<HTMLImageElement>) {
const supports = 'loading' in HTMLImageElement.prototype;
if (supports) {
nativeElement.setAttribute('loading', 'lazy');
}
}
}
The problem is that this if is always falsy in server.