Streaming image loading seamlessly with Progressive Image
In the modern web, we must handle resources that are MBs in size. One such resource is images. Large images can harm performance since they have slower load times, something that could lead to a negative user experience and frustration. To address these issues, one of the common patterns to use is the LowQualityImagePlaceholder pattern, also known as Progressive Image, where we load an image in stages. First, we show the lightweight version of an image (placeholder image). Then, in the background, we load the original image.
How to do it…
In this recipe, we’ll learn how to handle the Progressive Image pattern with ease with the help of RxJS magic.
Step 1 – Defining image sources
Inside our pro-img.component.ts
file, we must define paths to our local image and a placeholder/blurry version of the same image from our assets
folder:
src = 'image.jpg';
placeholderSrc = 'blurry-image.jpeg';
const img = new Image();
img.src = this.src;
const placeholderImg = new Image();
placeholderImg.src = this.placeholderSrc;
Step 2 – Creating a progress stream
While the image is loading, every 100 milliseconds, we’ll increase the progress percentage, until the load
event is triggered. This indicates that the image has been fully loaded. If an error occurs, we’ll say that the progress is at –1
:
const loadProgress$ = timer(0, 100);
const loadComplete$ = fromEvent(img, 'load')
.pipe(map(() => 100));
const loadError$ = fromEvent(img, 'error')
.pipe(map(() => -1));
Now, we can merge these load events and stream them into the Progressive Image load:
loadingProgress$ = new BehaviorSubject<number>(0);
this.imageSrc$ = merge(loadProgress$, loadComplete$,loadError$).pipe(
tap((progress) => this.loadingProgress$.next(progress)),
map((progress) => (progress === 100 ?img.src :placeholderImg.src)),
startWith(placeholderImg.src),
takeWhile((src) => src === placeholderImg.src, true),
catchError(() => of(placeholderImg.src)),
shareReplay({ bufferSize: 1, refCount: true })
);
We’ll use startWith
on the placeholder image and show it immediately in the UI while continuously tracking the progress of the original image load. Once we get 100%, we’ll replace the placeholder image source with the original image.
Step 3 – Subscribing to the image stream in the template
Meanwhile, in the component template, pro-img.component.html
, we can subscribe to the progress that’s been made while the image is loading in the background:
<div class="pro-img-container">
@if ((loadingProgress$ | async) !== 100) {
<div class="progress">
{{ loadingProgress$ | async }}%
</div>
}
<img
[src]="imageSrc$ | async"
alt="Progressive image"
class="pro-img"
>
</div>
Finally, if we open our browser, we may see this behavior in action:


Figure 2.5: Progressive Image
Common gotcha
In this recipe, for simplicity, we’ve chosen to artificially increase the download progress of an image. The obvious drawback is that we don’t get the actual progress of the image download. There’s a way to achieve this effect: by converting the request of an image’s responseType
into a blob. More details can be found here: https://stackoverflow.com/questions/14218607/javascript-loading-progress-of-an-image.
See also
- The Ultimate LQIP Technique, by Harry Roberts: https://csswizardry.com/2023/09/the-ultimate-lqip-lcp-technique/
- The HTML
load
event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event - The
takeWhile
operator: https://rxjs.dev/api/operators/takeWhile - The
startWith
operator: https://rxjs.dev/api/operators/startWith