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

@sitelintcode/improved-attribute-lazy-loading

v0.1.0

Published

Improve lazy loading for audio, video and image

Downloads

26

Readme

Improving attribute lazy loading

Optimizing lazy loading for image, video, and audio that includes time visibility in the viewport. In other words, if the image, video, audio is not minimum 1 second in the browser viewport then don't load it.

Features

  • Set preload attribute value on <video> or <audio> element once it's in the viewport.
  • Load the image <img>, <video> or <audio> when it's visible for at least 1 second in the viewport. This will prevent fetching the image while the user scrolls fast through the page.
  • Pause and resume automatically playing <video> or <audio> when it's not in the viewport. This is configurable.

Settings

Automatically play and pause depending on the visibility in the viewport

For scenarios where you want to play video automatically when it's in the viewport and pause when it's outside of the viewport, you may use data-media-autoplay="true" and data-media-autopause="true".

<video src="video/ljubljana-in-4k.mp4" data-media-autoplay="true" data-media-autopause="true" loop muted preload="none" poster="video/ljubljana-in-4k.thumbnail.jpg"></video>

Demo

Improving attribute lazy loading

Benefits

  • For <audio> and <video> elements this will help to avoid preloading the whole media file or metadata only.
  • For <img> element this will help to fetch the image only when it's visible within the viewport for more than 1 second. There is no need to fetch the image while the user scrolls fast the page.

Getting started

NPM

npm install @sitelintcode/improved-attribute-lazy-loading --save

HTML

Example code with minimum requirements.

<audio preload="none" data-preload="metadata"></audio>
<video preload="none" data-preload="metadata"></video>
<img loading="lazy">

TypeScript

Following options are available:

  • timeout: number, default: 1000 (in ms). Set the timeout after which visibility should be checked.
  • cssSelector: string, default: audio, img[loading="lazy"], video
  • debug: boolean, default: false. Settings it to true will help to set event listeners: play, pause to each media element so you can track it's state when they are in the viewport or outside.
import { ImprovedAttributeLazyLoading } from '@sitelintcode/improved-attribute-lazy-loading';

const improvedAttributeLazyLoading: ImprovedAttributeLazyLoading = new ImprovedAttributeLazyLoading();

improvedAttributeLazyLoading.install(timeout, cssSelector);

Browser environment

Note that options passed to method install are the same as above.

<script src="https://cdn.jsdelivr.net/npm/@sitelintcode/[email protected]/dist/improved-attribute-lazy-loading.js"></script>
<script>
(function() {
  const improvedAttributeLazyLoading = new window.sitelint.ImprovedAttributeLazyLoading();
  improvedAttributeLazyLoading.install();
}())
</script>

Notes

Note the version number in the jsdelivr URL: 0.0.1. Don't forget to set desired version. You may check releases: https://github.com/sitelint/improved-attribute-lazy-loading/releases

Worth to mention that jsdelivr suggests:

Omit the version completely or use latest to load the latest one (not recommended for production usage).

Technical

  1. import { terser } from "rollup-plugin-terser"; was replaced with import { terser } from "rollup-plugin-minification"; because rollup-plugin-terser is not compatible with Rollup 3.x version. See: https://github.com/TrySound/rollup-plugin-terser/issues/119

Contributing

Contributions are welcome, and greatly appreciated! Contributing doesn't just mean submitting pull requests. There are many different ways for you to get involved, including answering questions on the issues, reporting or triaging bugs, and participating in the features evolution process.

License

MOZILLA PUBLIC LICENSE, VERSION 2.0