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

@bautrukevich/preload

v0.2.0

Published

It's a small library for preload images in browser

Downloads

12

Readme

Preload

NPM version

It's a small library for preload images in browser.

See demo

For what?

The problem

Large or a lot of image files may take a second or two to load on the page. Preloading those images early rather than later helps ensure that visitors have a great experience viewing your content.

The solution

JavaScript includes the Image among its native object types. The Image object represents an HTML image tag on the page and exposes the same properties and events. Perhaps oddly, the Image has no constructor that accepts an image source, so an image must be loaded into the object by setting its src attribute. Doing so causes the image to be downloaded from the server at that point.

Usage

Usage from npm

First of all you need to install it from npm using

yarn add @bautrukevich/preload

or

npm install @bautrukevich/preload --save

After you can import it and use like this:

import preload from '@bautrukevich/preload';
    
const IMAGE_URL = 'https://www.google.ru/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
const FAILED_IMAGE_URL = 'https://www.google.ru/images/branding/googlelogo/2x.jpg';
let orImageWithSrc = new Image();
let orImageWithoutSrc = new Image(); // it's valid and you can set src later

orImageWithSrc.src = IMAGE_URL;

// It would be resolved
preload(IMAGE_URL, orImageWithSrc).then(resolved => {
  console.log(resolved); // [[HTMLImageElement, 'loaded'], [HTMLImageElement, 'loaded']]
}, rejected => {
  console.log(rejected);
});

// It would be rejected
preload(orImageWithoutSrc).then(resolved => {
  console.log(resolved);
}, rejected => {
  console.log(rejected); // [[HTMLImageElement, 'new']]
});

// It would be rejected — if at least one image was not loaded
preload(IMAGE_URL, orImageWithoutSrc).then(resolved => {
  console.log(resolved);
}, rejected => {
  console.log(rejected); // [[HTMLImageElement, 'loaded'], [HTMLImageElement, 'new']]
});

// It would be rejected — if at least one image was not loaded
preload(FAILED_IMAGE_URL, orImageWithSrc).then(resolved => {
  console.log(resolved);
}, rejected => {
  console.log(rejected); // [[HTMLImageElement, 'failed'], [HTMLImageElement, 'loaded']]
});

preload function accept any number of arguments such as URL (string) or HTMLImageElement (new Image() or document.createElement('img')) and return Promise with [[source, 'state'], [source, 'state']...], where source — HTMLImageElement and state — state of his loading (can be 'new', 'loaded', 'failed').

Promise resolves if all images was loaded successfully and rejected if at least one not.

Usage from dist

You can simply install library using npm or download and add script /dist/preload.min.js to your page and use preload() function.

Scripts

You can use npm or yarn (as you wish).

yarn build or npm run build — build UMD library

yarn dev or npm run dev — for development. With opening browser use yarn dev --open or npm run dev --open.

yarn test or npm run test — run tests once.

yarn test:watch or npm run test:watch — run tests in watch mode.

Browser support

Last 2 versions. No IE.