|
1 | 1 | --- |
2 | 2 | layout: post |
3 | | -title: "Intersection Observer API: speed up your web applications" |
4 | | -description: "In this post I will talk about Intersection Observer and how you can use them to speed up your web pages." |
| 3 | +title: "Intersection Observer API: speed up your web applications with lazy loading" |
| 4 | +description: "Intersection Observer can improve your web applications performance and by helping you to implement lazy loading of images." |
5 | 5 | date: 2019-05-10 |
6 | 6 | image: /assets/images/posts/XXXXXX |
7 | 7 | tags: [web development, javascript, typescript] |
|
11 | 11 | authors: [fabrizio_duroni] |
12 | 12 | --- |
13 | 13 |
|
14 | | -*In this post I will talk about Intersection Observer and how you can use them to speed up your web pages.* |
| 14 | +*Intersection Observer can improve your web applications performance and by helping you to implement lazy loading of images.* |
15 | 15 |
|
16 | 16 | --- |
17 | 17 |
|
| 18 | +In the last few months I worked hard to improved the page speed of my website (yeah, the one you're visiting right now). I improved all my client side code in order to be able to reach a performance score above 90 points on [Lighthouse](https://developers.google.com/web/tools/lighthouse/), the offical Google Chrome tool to measure performance, accessibility, progressive web apps compliance and more on your web application. |
| 19 | +One of the last thing that was contained in the report was a warning about offscreen images, like the one contained in the following screenshot: |
18 | 20 |
|
| 21 | +{% include blog-lazy-image.html description="intersection observer offscreen audit" src="/assets/images/posts/intersection-observer-offscreen-audit.jpg" %} |
| 22 | + |
| 23 | +So I followed the link contained in the report that points to a page where are contained the [official Google Guidelines about loading offscreen images](https://developers.google.com/web/tools/lighthouse/audits/offscreen-images). The main topic of the page is the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) and how it can help you to load specific content only when it becomes visible in the viewport. I found also anothe article on the official Google developer website that explains in details how to leverage the power of Intersection Observer to lazy load the images in your web applications. So as you may imagine I "accepted the challenge" (like only [Barney Stinson](https://en.wikipedia.org/wiki/Barney_Stinson) in how I met your mother is used to do :stuck_out_tongue_winking_eye:) and I started to implement the lazy loading of images for my website. |
| 24 | + |
| 25 | +#### Implementation |
| 26 | + |
| 27 | +.... |
| 28 | + |
| 29 | + |
| 30 | +[Interserction Observer Polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill) |
| 31 | + |
| 32 | +```javascript |
| 33 | +/* @flow */ |
| 34 | +import 'intersection-observer' |
| 35 | +import { removeCssClass } from './css-class' |
| 36 | + |
| 37 | +const lazyLoadImages = (selector: string, loadCompleted: (image: HTMLImageElement) => void) => { |
| 38 | + const intersectionObserver: IntersectionObserver = new IntersectionObserver( |
| 39 | + (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => onIntersection(entries, observer, loadCompleted), |
| 40 | + { rootMargin: '50px 0px', threshold: 0.01 } |
| 41 | + ) |
| 42 | + document.querySelectorAll(selector).forEach(image => intersectionObserver.observe(image)) |
| 43 | +} |
| 44 | + |
| 45 | +const onIntersection = (entries: IntersectionObserverEntry[], observer: IntersectionObserver, loadCompleted: (image: HTMLImageElement) => void) => { |
| 46 | + entries.forEach(entry => { |
| 47 | + if (entry.intersectionRatio > 0) { |
| 48 | + observer.unobserve(entry.target) |
| 49 | + eventuallyLoadImage(entry.target, loadCompleted) |
| 50 | + } |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +const eventuallyLoadImage = (element: HTMLElement, loadCompleted: (image: HTMLImageElement) => void) => { |
| 55 | + if (element instanceof HTMLImageElement) { |
| 56 | + loadImage(element, loadCompleted) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +const loadImage = (image: HTMLImageElement, loadCompleted: (image: HTMLImageElement) => void) => { |
| 61 | + const src: string = image.dataset.src |
| 62 | + fetchImage(src).then(() => { |
| 63 | + image.src = src |
| 64 | + removeCssClass(image, 'lazy') |
| 65 | + loadCompleted(image) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +const fetchImage = (src: string) => { |
| 70 | + return new Promise((resolve, reject) => { |
| 71 | + const image = new Image() |
| 72 | + image.src = src |
| 73 | + image.onload = resolve |
| 74 | + image.onerror = reject |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +export { lazyLoadImages } |
| 79 | +``` |
| 80 | + |
| 81 | +#### Conclusion |
| 82 | + |
| 83 | +Inteserction Observer is a powerful API. It let you implmenent lazy loading for rsources loading and reach performance and architectural application pattern that I had a chance to see only in mobile native apps. The web is filling the gap with native apps, and Intersection Observer are another demonstration that the 90% of the existing native mobile apps could become powerful web apps. As a consequence of the fact that in my daily job I'm still a native mobile app developer, I'm still following the iOS, Android and React Native scene and I'm still studying all the new tools and SDKs improvement released by Apple, Google and Facebook. But, you know, technology goes fast I have to be prepared for the future :relaxed:. Sooo, long live Intersection Observer!!! Web applications will be much more performant with your help :green_heart:. |
0 commit comments