How to lazyload Javascript using LocalStorage

Learn efficient JavaScript lazy loading with LocalStorage for improved website performance.

Website performance is a critical factor in providing a smooth and enjoyable user experience. One common technique to enhance performance is lazy loading JavaScript. Lazy loading allows you to load JavaScript files only when they are needed, reducing initial page load times and conserving bandwidth. In this tutorial, we'll explore an interesting twist on lazy loading by leveraging the power of LocalStorage, a web storage technology available in modern browsers. By the end of this guide, you'll have a solid understanding of how to implement lazy loading with LocalStorage and optimize your website's performance.

Lazyload Javascript using LocalStorage
Lazyload Javascript using LocalStorage | Fineshop Design

I have written a JavaScript Library which can be used to lazyload resources. To use it, paste the following JavaScript code in the head section of your webpage.

/*!
 * lazy.js by Fineshop Design
 * ----------------------------
 *
 * MIT License
 *
 * Copyright (c) 2021 Fineshop Design
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
window.lazy =
  window.lazy ||
  new Promise((resolve) => {
    const LOCAL_KEY = "IS_LAZIED";
    const LOCAL_VALUE = "true";
    // Window events to be attached initially which can trigger lazy
    const WINDOW_INITIAL_EVENTS = ["scroll", "click"];
    // Window events to be attached after window is fully loaded which can trigger lazy
    const WINDOW_ONLOAD_EVENTS = [
      "keydown",
      "mouseover",
      "touchmove",
      "touchstart",
    ];
    // All window events which will be attached
    const WINDOW_EVENTS = WINDOW_INITIAL_EVENTS.concat(WINDOW_ONLOAD_EVENTS);

    function getLazied() {
      try {
        return localStorage.getItem(LOCAL_KEY) === LOCAL_VALUE;
      } catch (_) {
        return true;
      }
    }
    function setLazied(lazied = true) {
      try {
        if (lazied) {
          localStorage.setItem(LOCAL_KEY, LOCAL_VALUE);
        } else {
          localStorage.removeItem(LOCAL_KEY);
        }
      } catch (_) {
        // do nothing
      }
    }
    function execute(data) {
      setLazied(true);
      resolve({ type: data.type.toLowerCase() });
      // detach event listeners
      for (const type of WINDOW_EVENTS) {
        window.removeEventListener(type, execute);
      }
    }
    if (getLazied()) {
      resolve({ type: "local" });
    } else {
      // check if document is already scrolled
      if (
        document.documentElement.scrollTop !== 0 ||
        (document.body && document.body.scrollTop !== 0)
      ) {
        execute({ type: "scroll" });
      } else {
        // events to be attached after window is loaded
        const onLoad = () => {
          window.removeEventListener("load", onLoad);
          for (const type of WINDOW_ONLOAD_EVENTS) {
            window.addEventListener(type, execute);
          }
        };
        window.addEventListener("load", onLoad);

        // events to be attached initially
        for (const type of WINDOW_INITIAL_EVENTS) {
          window.addEventListener(type, execute);
        }
      }
    }
  });

Usage

The lazy promise is resolved when the user interacts with the page and so on. Once the promise is resolved, you can load your external javascript, stylesheet or make HTTP requests.

lazy.then((e) => {
  // Load your JS or CSS dynamically
  // Make HTTP requests
  console.log(e.type) // could be 'scroll', 'click', 'keydown', 'mouseover', 'touchmove', 'touchstart' or 'local'
});

Examples

Here I have written a Javascript function loadJS to load Javascript from external sources dynamically.

/**
 * Function to load Javascript file
 *
 * @author Deo Kumar <deo@fineshopdesign.com>
 *
 * @param {string | URL} source - Source of the script
 * @param {((script: HTMLScriptElement) => void) | null} [beforeLoad] - Function for modifying script element before loading
 *
 * @returns {Promise<HTMLScriptElement>} - Returns a Promise which resolves with HTMLScriptElement instance
 */
window.loadJS = function loadJS(source, beforeLoad) {
  return new Promise((resolve, reject) => {
    const script = document.createElement("script");

    // Enable async and defer by default
    Object.assign(script, { async: true, defer: true });

    if (typeof beforeLoad === "function") {
      beforeLoad(script);
    }

    function cleanUp() {
      script.onload = null;
      script.onerror = null;
    }

    script.src = source instanceof URL ? source.href : source;
    script.onload = (e) => {
      cleanUp();
      resolve(e.target);
    };
    script.onerror = (e) => {
      cleanUp();
      reject(new URIError(`The script ${e.target.src} didn't load correctly.`));
    };

    const firstScript = document.getElementsByTagName("script")[0];

    if (firstScript && firstScript.parentNode) {
      firstScript.parentNode.insertBefore(script, firstScript);
    } else {
      document.head.appendChild(script);
    }
  });
}

We can use it in our lazy.then() callback function as shown belown:

lazy.then(() => { 
  return loadJS("./my-example-script.js", (script) => {
    // Add async attribute before loading
    script.async = true;
  });
}).then(() => {
  // JS is lazyloaded, perform actions here
  // myExampleLib.myMethod();
});

Lazyload Adsense Script

You can lazyload AdSense Script using this library, an example is shown below:

lazy.then(() => {
  const adsenseScriptSource = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX";
  return loadJS(adsenseScriptSource, (script) => {
    script.async = true;
    script.crossOrigin = "anonymous";
  });
}).then(() => {
  // Adsense script loaded
  // ...
});

Conclusion

Lazy loading JavaScript using LocalStorage is a powerful strategy to enhance your website's performance and user experience. By deferring the loading of non-essential scripts until they are needed, you can improve initial page load times and reduce bandwidth usage. This tutorial will guide you through the process of implementing this technique effectively, helping you create faster, more efficient websites for your users.

Copyright (c):
fineshopdesign.com

About the author

Deø Kumar
Lost in the echoes of another realm.

2 comments

  1. Team Tokko
    Team Tokko
    How to implememtation ?
    1. Deø Kumar
      Deø Kumar
      I have updated the post describing how you can implement it with few examples.
To avoid SPAM, all comments will be moderated before being displayed.