npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazyload-script

v1.0.5

Published

Promise based method for adding a script to the page if it has not already been added

Downloads

436

Readme

lazyload-script 😴 · Build Status npm version

Promise based method for adding a script to the page if it has not already been added.

Install

yarn add lazyload-script
bower install lazyload-script

Weight In

Imported Weight

When used with require() you'll notice very little weight is added to your bundle.

const lazyLoadScript = require('lazyLoadScript');

VanillaJS Weight

| Script | Disk Size | GZIP | | ------------- | ------------- | ----- | | lazyload-script.0.0.4.js | 4.82kB | 1.48kB | | lazyload-script.0.0.4.min.js | 1.64kB | 773b |

The UMD module wrapper weighs more than the lazyLoadScript() method itself.
If you want to go rogue, you can load directly from source.

Usage

lazyLoadScript accepts two parameters. The path to the script to load and either an id or configuration object.

lazyLoadScript('js/main.js', 'main').then(() => {
  // main.js is loaded now with an id of main
})

The id parameter is optional. It is used to ensure that subsequent requests to load a script with that same id immediately resolve. If you omit the id parameter, the DOM will first be queried for a <script> with the same src attribute, before making a new request by appending a new <script> tag.

lazyLoadScript uses this id to ensure scripts with the same id are only loaded once. This allows web components to request dependencies with lazyLoadScript and rest assured the script will always be ready but only be requested as needed.

lazyLoadScript is packaged as a UMD module so it can be included in several ways.

The UMD pattern typically attempts to offer compatibility with the most popular script loaders of the day (e.g RequireJS amongst others). In many cases it uses AMD as a base, with special-casing added to handle CommonJS compatibility.
 — umd

With require()

const lazyLoadScript = require(`lazyLoadScript`);
lazyLoadScript('main.js', 'main').then(() => {
  /// main.js loaded
});

With VanillaJS

lazyLoadScript('main.js', 'main').then(() => {
  /// main.js loaded
});

Multiple scripts can asynchronously be loaded by passing an Array of lazyLoadScript promises to Promise.all().

  Promise.all([
    lazyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js", "react.15.4.2.min.js"),
    lazyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js", "react-dom.15.4.2.min.js")
  ]).then(() => {
    // React is ready, maybe load your component with lazyLoadScript() now?
  });

Configuration

lazyLoadScript accepts two parameters. The path to the script to load and either an id or configuration object.

| Option | Default | Description | | ------------- | ------------- | ----- | | async | undefined | If true adds an async attribute | | defer | undefined | If true adds a defer attribute | | integrity | undefined | If set adds an integrity attribute | | type | undefined | If set adds a type attribute | | text | undefined | If set adds an text attribute | | charset | undefined | If set adds an charset attribute | | crossorigin | undefined | If set adds an crossorigin attribute | | force | false | If true forces an asset to be loaded even if another with the same id or href are found |

CDN Fallbacks

Loading common libraries and frameworks from CDNs can be great for leveraging the browser cache, but to keep your experience functional in the event the CDN is reachable, it is recommended to load a local fallback.

For example:

const promises = [
  // try to load React from a CDN, fallback to a local copy
  lazyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js", "react.15.4.2.min.js").catch((err => (
    lazyLoadScript(`./js/vendor/react.15.4.2.min.js`, "react.15.4.2.min.js")
  ))),
  // try to load React DOM from a CDN, fallback to a local copy
  lazyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js", "react-dom.15.4.2.min.js").catch((err => {
    lazyLoadScript(`./js/vendor/react-dom.15.4.2.min.js`, "react-dom.15.4.2.min.js")
  })),
  // try to load Redux from a CDN, fallback to a local copy
  lazyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js", "redux.3.6.0.min.js").catch((err => {
    lazyLoadScript(`./js/vendor/redux.3.6.0.min.js`, "redux.3.6.0.min.js")
  })),
  // try to load React Redux from a CDN, fallback to a local copy
  lazyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/react-redux/5.0.3/react-redux.min.js", "react-redux.5.0.3.min.js").catch((err => {
    lazyLoadScript(`./js/vendor/react-redux.5.0.3.min.js`, "react-redux.5.0.3.min.js")
  }))
];


Promise.all(promises).then(() => {
  // React, React DOM, Redux, and React Redux are ready. woohoo! maybe load your component with lazyLoadScript() now?
});

See Also

✅ Getting Started

We're going to use yarn so make sure that is installed.

npm install yarn -g

Now clone the repo and run the tests.

git clone -b master git://github.com/jpdevries/lazyload-script.git
cd lazyload-script
yarn
yarn test