Lazy load images in Preact using Intersection Observer

Ashok Vishwakarma
Ashok Vishwakarma

Mar 10, 2019 • 2 min read

We love Preact for its tiny size and capabilities which makes it an ideal choice for smaller web applications.

Intersection Observer

Making this visibility test more efficient is what IntersectionObserver was designed for, and it’s landed in Chrome 51 (which is, as of this writing, the beta release). IntersectionObservers let you know when an observed element enters or exits the browser’s viewport.

To know more about Intersection Observer please check out the awesome article written by Surma on Google Web Fundamentals

IntersectionObserver’s Coming into View  |  Web  |  Google Developers

IntersectionObservers let you know when an observed element enters or exits the browser’s viewport.

Which browsers are supported

Most of the modern browsers support Intersection Observer out of the box.

Update: Check latest support details

Can I use... Support tables for HTML5, CSS3, etc

If you still need support for older browsers and IE there is a well written and documented polyfill available on Github in the w3c repo, find the link below.


Intersection Observer. Contribute to w3c/IntersectionObserver development by creating an account on GitHub.

The Image Component in Preact

To enable lazy loading for images the best approach is to create an Image component in Preact and use the same instead of the img tag.

import { h, Component } from 'preact';
import classy from 'classnames';

export default class Image extends Component {

state = {
dataSrc: false,
loaded: false

inview(entries, observer){
entries.forEach(entry => {
if(entry.intersectionRatio){'load', this.loading.bind(this)); ="data-src");

if( this.setState({
loaded: true

dataSrc: this.props.src,
loaded: false

const observer = new IntersectionObserver(this.inview.bind(this));


render() {
return (
<div className="image-container">
<div className={classy({loader: true, hidden: this.state.loaded})}>
<img src="assets/img/loading.svg" />
<img src={this.state.src} data-src={this.state.dataSrc} ref={element => this.element = element} />

SCSS for .image-container

position: relative;

position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);

&.hidden {
display: none;

Uses of the Image component

<Image src={data.image} />

How does it work?

The following happens when you use the Image component instead of the img tag.

  1. Image component take original Image URL in src props
  2. Image component replace the original src with data-src
  3. IntersectionObserver initiates in componentDidMount and uses the inview method as a callback.
  4. IntersectionObserver observes the element which is .image-container
  5. If .image-container is in-view it sets the state with loaded property to true
  6. Which shows the image and also hide the loader.


Lazy loading can make the app load fast and provide a better user experience.

Ashok Vishwakarma

Ashok Vishwakarma

Google Develover Expert — WebTechnologies and Angular | Principal Architect at | Entrepreneur | TechEnthusiast | Speaker