Google PageSpeed Insights is a wonderful tool for improving your website. However, it can also be difficult to tell which improvements you should focus on first. For mobile devices, deferring offscreen images can improve page speed significantly. But keeping track of the user’s scroll position can be costly, and you do not want to trade download time for processing time. This post will explore an option using pure JavaScript that does not rely on scroll position.

The Solution

First off, how do you even know if this post applies to you?? You can use an automated reporting tool, such as Lighthouse or PageSpeed Insights, to check for offscree images that should be deferred. In most browsers, you can open the developer console, click on the Audits or Lighthouse tab, and then click Run Report to kick off a new report. Otherwise, you can visit the PageSpeed Insights website and paste in the URL you want to audit. If “Defer offscreen images” is in the list of improvements, then this post will apply to you! Here is an example of what that recommendation looks like in my browser.

Deferring offscreen images on this page is recommended

Now that you know this post applies to you, we can cover the general approach. The basic idea of this approach is to load in a small base-64 image as a placeholder until the onload event, and then after the onload event we will replace the placeholder with the actual image. This process is based largely on the wonderful post here.

Encoding an Image

For this website, we’re going to use the strangePy logo as the placeholder image. All you need to do is upload your desired image to an encoder website, such as base64-image.de and then copy the output. Below is the raw output of the base64 encoded image.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAQAAABNTyozAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElNRQfkAQECMiOITkyMAAADPklEQVR42u3dTUiTcQDH8d9yYQeNioKKoDdJKjoICWGX6jiCTkJh5MFLnTpYBwnxJNQtCuwQVIQQeQkhIgkKqUMlHQSJQupSRNgbFmjZix3G3Nvz7Den25j7fnbZ/v//nmfPl82NqZsEAAAAAEC4Uc0W4TSpPbl2uqzcR112KzWkzeHTlRRotkjb3aB7WhU2SSBJ2qU7qg2eIlDcAV1XJGiCQAnH1B00HC33Uc/DwgJ91zU90ZQa1aa9gSt69EoD5T7IhRhZwJP5U62f205EZ0JWTWt/5k4j87qJqa6qqYgxOjWcNfZczQVu7bN2ayJtpE+nQla2aHxxDmG4KC/bEqcjAXt8VvD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to be typing that in anywhere. This is what the image looks like when it actually renders on the page (you can even inspect it in your browser to see how it works).

strangePy logo base64 encoded

Now that we have the base-64 encoded image ready to go, we can start implementing the image deferral logic into the website. First, we’ll want to write a few lines of Javascript to swap the encoded placeholder image for the proper image. Then, we’ll want to update our webpages to use the encoded placeholder image by default, and add a data-src parameter onto the <img> tag in order to hold the proper image source.

JavaScript

To get things started, we want to write a function to swap out the image source of one image. We should check to make sure the data-src parameter is attached to the image. If it is, then we can set the src attribute from the base-64 encoded placeholder image to the data-src attribute containing the path to the proper image.

function deferImg(image){
    if (image.getAttribute('data-src')) {
            image.setAttribute('src', image.getAttribute('data-src'));
        }
}

Next, we’ll grab all of the images and put them into one array so we can later apply the function to each image in this array.

var images = document.querySelectorAll('img');

We have all the logic set up, but nothing to actually run the new function. This implementation may vary depending on if you are using jQuery or another JavaScript library. For this example, we’ll use pure JavaScript and an event listener for the document.state to be complete.

document.addEventListener('readystatechange', () => {    
  if (document.readyState == 'complete') images.forEach(deferImg);
});

That’s it! Just a few lines of JavaScript and we are all set to add the encoded placeholder images. For completeness, here is all of the JavaScript logic put together.

var images = document.querySelectorAll('img');

function deferImg(image){
    if (image.getAttribute('data-src')) {
            image.setAttribute('src', image.getAttribute('data-src'));
        }
}

document.addEventListener('readystatechange', () => {    
  if (document.readyState == 'complete') images.forEach(deferImg);
});

HTML

The specific HTML will be a bit different depending on how your site is structured. In general, you should have an image with a src attribute by default, and you want to switch the src value into the data-src parameter, and then set the src to your base-64 encoded placeholder image.

<!-- Before -->
<img src="/img/mstile-144x144.png" >

<!-- After -->
<img data-src="/img/mstile-144x144.png" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAQAAABNTyozAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElNRQfkAQECMiOITkyMAAADPklEQVR42u3dTUiTcQDH8d9yYQe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AAAAAJfEfaGgQuLoUUnMAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjAtMDEtMDFUMDI6NTA6MzUrMDE6MDAAFiETAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIwLTAxLTAxVDAyOjUwOjM1KzAxOjAwcUuZrwAAAFd6VFh0UmF3IHByb2ZpbGUgdHlwZSBpcHRjAAB4nOPyDAhxVigoyk/LzEnlUgADIwsuYwsTIxNLkxQDEyBEgDTDZAMjs1Qgy9jUyMTMxBzEB8uASKBKLgDqFxF08kI1lQAAAABJRU5ErkJggg==">

The above is an example for one specific image. You likely don’t want to manually do this for every single one of your images on your website. How to set it for your website may be different than mine. My website is hosted on GitHub pages and uses Jekyll with Liquid variables, so the change is quite a bit more concise here.

<!-- Before -->
<img src="" >
     

<!-- After -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAi4AAAIuCAYAAACYSoPfAAAgAElEQVR4Xu3dYdYct60tUN2Z5c08M/NbvivJtR1L3VUASBDc+atuFrkPFRzXJ9n/88P/CBAgQIAAAQKHCPzPIfu0TQIECBAgQIDAD8XFJSBAgAABAgSOEVBcjonKRgkQIECAAAHFxR0gQIAAAQIEjhFQXI6JykYJECBAgAABxcUdIECAQIbA7/9v+lvGQtYgQOBXAoqL+0HgBAFD8YSU7JEAgQUCissCZI8gQIAAAQIEcgQUlxxHqxAgQIAAAQILBBSXBcgeQYAAAQIECOQIKC45jlYhQIAAAQIEFggoLguQPYIAAQIECBDIEVBcchytQoAAAQIECCwQUFwWIHsEAQIECBAgkCOguOQ4WoUAAQIECBBYIKC4LED2CAIECBAgQCBHQHHJcbQKAQIECBAgsEBAcVmA7BEECBAgQIBAjsD3xcV/KyVH3CoECBAgQIDAa4Hvi8vrR/giAQIECBAgQCBHQHHJcbQKAQIECBAgsEBAcVmA7BEECBAgQIBAjoDikuNoFQIECBAgQGCBgOKyANkjCBAgQIAAgRwBxSXH0SoECBAgQIDAAgHFZQGyRxAgQIAAAQI5AopLjqNV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ZmatTESBA4D4Bf4j3iswVlytidkgCBAgQIDBDQHGZkaNTECBAgACBKwQUlytidkgCBAgQIDBDQHGZkaNTECBAgACBKwQUlytidkgCBAgQIDBDQHGZkaNTECBAgACBKwT+Pz4Z/MVagWj3AAAAAElFTkSuQmCC" data-src="">

Note that the above is only possible due to the support for Liquid variables. If you do not use the Liquid templating engine in your website, then this approach will not work, but you should have something similar for dynamically loading in images without manually setting each one. One more note on the example above, I saved the base-64 encoded image into a site variable named encodedlogo so there is just one easy place for me to update the encoded image in the future if I ever need to.

Closing Thoughts

This should be all you need to do on your website! Just encode an image (such as your logo), enter the encoded image into your src attribute, and set the data-src attribute to the proper image reference. The few lines of JavaScript that we wrote earlier will take care of the rest!

For a quick sanity check, we can run another audit and confirm that “Defer offscreen images” is no longer one of the recommendations. You can expand the “Passed audits” section to verify that the check has passed.

Offscreen images have been successfully deferred

Further Reading