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

defer-css

v1.0.1

Published

ExtraSlim Javascript to Load css in your page without affecting load speed.

Downloads

54

Readme

Defer Css

Simple LightWeight function to defer css in your web applications or websites.

DEMO: stackblitz.com

It's 2023 and there is still a need to defer CSS yourself. One would think that with all the frameworks and libraries out there, this would be a solved problem. But it's not. This is a simple plugin that will help you defer your CSS.

Why or when should I use this plugin?

There are some styles that are not needed for the initial page load. A good list of these styles are:

  • Fonts
  • Icons
  • AnimateCSS

With this plugin you can defer these styles and load them when they are needed.

Do I need this if am I using a framework like angular, react or vue?

Yes, you do. Most frameworks have a way to defer CSS, but they are not perfect. You don't get to decide when to load the CSS. You can only decide if you want to load it or not in the component.

Direct Browser Installation

<!--Using JsDeliver CDN-->
<script src="https://cdn.jsdelivr.net/npm/defer-css"></script>

<!-- Or Using UnPkg CDN-->
<script src="https://unpkg.com/defer-css"></script>

<!-- Example Script -->
<script>
  const { deferCss } = window;
</script>

Using Package Managers

npm i defer-css
# or using yarn
yarn add defer-css

Import the package in your project

import { deferCss } from 'defer-css';

Example Usage


<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/defer-css"></script>

  <!-- Styles will be placed before this element-->
  <link id="defer-css" />
  <!--  OR   -->
  <link id="custom-id" />
</head>
</html>

The deferCss function takes two arguments, the first is the path/paths to the css file and the second is the id of the element to place the styles before.

The default id is defer-css but you can change it to any id you want by passing it as the second argument.

// Using a string
deferCss("/style-1.min.css");

// Using a single object
deferCss({ href: "/style-2.min.css", crossOrigin: "anonymous" });

// using a mix of string and object in an array
const styles = [
    "/style-3.min.css",
    { href: "/style-4.min.css", crossOrigin: "anonymous" }, 
];

deferCss(styles, "custom-id");

Result:


<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/defer-css"></script>
  
  <link rel="stylesheet" href="/style-1.min.css" />
  <link rel="stylesheet" href="/style-2.min.css" crossorigin="anonymous" />
  <link id="defer-css" />
  
  <link rel="stylesheet" href="/style-3.min.css" />
  <link rel="stylesheet" href="/style-4.min.css" crossorigin="anonymous" />
  <link id="custom-id">
</head>

If a link object includes an onDefer function, it is executed when the css file is loaded.

deferCss([
  {
    href: 'style-1.min.css',
    onDefer: function() {
      // do something
    }
  },
])

Multiple deferCss

Let's say you want to mount css in multiple places.


<head>
  <link id="main-css">
  <style>
    .some-style-before-other-css {
      background: teal;
    }
  </style>
  <link id="other-css">
</head>
deferCss(['main-css-1.css', 'main-css-2.css'], 'main-css');
deferCss([
  { href: 'other-css-1.css', crossOrigin: 'anonymous' },
  'other-css-2.css'
], 'other-css');

This will result to.


<link rel="stylesheet" href="main-css-1.css">
<link rel="stylesheet" href="main-css-2.css">
<style>
  .some-style-before-other-css {
    background: teal;
  }
</style>
<link rel="stylesheet" href="other-css-1.css" crossorigin="anonymous">
<link rel="stylesheet" href="other-css-2.css">

DeferCssData

The deferCssData includes details you may need.

({
  // Element mounted on, default = 'defer-css'
  "defer-css": {
    total: Number, // total number of css defined
    loaded: Number // total number of css loaded (at the moment)
  }
});