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-css

v1.0.5

Published

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

Downloads

93

Readme

lazyload-css 😴 · Build Status npm version

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

Install

yarn add lazyload-css
bower install lazyload-css --save

Weigh In

Imported Weight

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

const lazyLoadCSS = require('lazyLoadCSS');

VanillaJS Weight

| Script | Disk Size | GZIP | | ------------- | ------------- | ----- | | lazyload-css.1.0.0.js | 4.36kB | 1.39kB | | lazyload-css.1.0.0.min.js | 1.47kB | 718b |

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

Usage

lazyLoadCSS accepts two parameters. The path to the script to load and an optional id or configuration Object.

lazyLoadCSS('css/main.css', 'main').then(() => {
  // main.css 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 <link> with the same href attribute, before making a new request by appending a new <link> tag.

lazyLoadCSS uses this id to ensure scripts with the same id are only loaded once. This allows web components to request dependencies with lazyLoadCSS and rest assured the sheets will only be loaded once regardless of how many times they are requested.

lazyLoadCSS 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 lazyLoadCSS = require(`lazyLoadCSS`);
lazyLoadCSS('main.css', 'main').then(() => {
  /// main.css loaded
});

With VanillaJS

lazyLoadCSS('main.css', 'main').then(() => {
  /// main.css loaded
});

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

  Promise.all([
    lazyLoadCSS("assets/css/base.css", "base"),
    lazyLoadCSS("assets/css/layout.css", "layout")
  ]).then(() => {
    // stylesheets are loaded now
  });

Configuration

lazyLoadCSS accepts two parameters. The path to the script to load and an optional id or configuration Object.

| Option | Default | Description | | ------------- | ------------- | ----- | | id | undefined | Used to ensure the same stylesheet isn't added twice | | media | 'all' | media attribute of the <link> to be added | | rel | 'stylesheet' | rel attribute of the <link> to be added | | type | text/css | type attribute of the <link> to be added | | force | false | If true forces an asset to be loaded even if another with the same id or href are found |

In the below example, unless lazyLoadCSS already loaded a <link> with the same id or href, <link type="text/css" rel="stylesheet" media="all" href="main.css" /> will be appended to document.head.

lazyLoadCSS('main.css', {
  id: 'main',
  media: 'screen'
}).then((link) => {
  // link is either the newly added stylesheet or the one that was already there
});

In the next example, a stylesheet will forcefully be added regardless of if one with the same href or id already exists.

lazyLoadCSS('print.css', {
  id: 'print',
  media: 'print',
  force: true
}).then((link) => {
  // link is the newly added stylesheet
});

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-css.git
cd lazyload-css
yarn
yarn test