Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@djforth/lazyload-polyfill

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@djforth/lazyload-polyfill

A Lazyload polyfil that uses lozad to fill native lazyloading

latest
npmnpm
Version
1.1.2
Version published
Maintainers
1
Created
Source

lazyload-polyfill

A Lazyload polyfill that converts JS lazyload to native lazyloading.

Because of the way images load, it is important to set up the lazyload for the JS and then if native lazyload is available this polyfill will re-write the images into the lazyload syntax. This is because the images will start to load before the JS initialised in browsers that don't support native lazyload.

By default this is set up for (lozad)[https://apoorv.pro/lozad.js], however it can be configured to other lazyloading libraries.

The best way to set this is using (dynamic imports)[https://github.com/tc39/proposal-dynamic-import#import] which can be set up in (webpack)[https://webpack.js.org/guides/code-splitting/#dynamic-imports], (rollup)[https://rollupjs.org/guide/en/#importing] or (parcel)[https://parceljs.org/code_splitting.html]

This example uses lozad standard set up:

<img class="lozad" data-src="image.png" data-alt="Some image" />

In the JS -

if ('loading' in HTMLImageElement.prototype) {
    import(
    /* webpackChunkName: "lazyload-polyfill" */ '@djforth/lazyload-polyfill'
  ).then(({ default: lazyload }) => {
    lazyload();
  });
} else {
   import(
    /* webpackChunkName: "lozad" */ 'lozad'
  ).then(({ default: lozad }) => {
    const observer = lozad(); // lazy loads elements with default selector as '.lozad'
    observer.observe();
  });
}

If you want to change the defaults to your lazyloader of choice they are set to the following:

OptionDescriptiondefault
selectorSet the selectors for the to replace'img.lozad, iframe.lozad, picture.lozad'
imgDataSets the data attributes used for src and srcset for images{ src: 'src', srcset: 'srcset'}
removeClassClass to remove from element'lozad'
picDataClass to remove from element{src: 'iesrc', alt: 'alt'}

Bug reports

If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as possible to help us fixing the possible bug. We also encourage you to help even more by forking and sending us a pull request.

https://github.com/djforth/lazyload-polyfill/issues

Contribute

If you'd like to contribute, lazyload-polyfill is written using babel and rollup in ES6.

Please make sure any additional code should be covered in tests (Jest).

If you need to run the test please use:


yarn test

or to rebuild the JS run:


yarn build

Maintainers

Adrian Stainforth (https://github.com/djforth)

License

lazyload-polyfill is an open source project falling under the MIT License. By using, distributing, or contributing to this project, you accept and agree that all code within the @morsedigital/select-filter project are licensed under MIT license.

FAQs

Package last updated on 06 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts