All Projects → liamfiddler → Eleventy Plugin Lazyimages

liamfiddler / Eleventy Plugin Lazyimages

Licence: mit
Eleventy plugin that adds blurry placeholders & lazy loading to your images

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Eleventy Plugin Lazyimages

Sqip
"SQIP" (pronounced \skwɪb\ like the non-magical folk of magical descent) is a SVG-based LQIP technique.
Stars: ✭ 3,074 (+2746.3%)
Mutual labels:  images, performance, placeholder
Xamarin Forms Perf Playground
Xamarin.Forms Performance Playground (Layouts, Bindings, XAMLC, etc)
Stars: ✭ 119 (+10.19%)
Mutual labels:  images, performance
Lozad.js
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more
Stars: ✭ 6,932 (+6318.52%)
Mutual labels:  images, performance
Yall.js
A fast, flexible, and small SEO-friendly lazy loader.
Stars: ✭ 1,163 (+976.85%)
Mutual labels:  images, performance
Pg flame
A flamegraph generator for Postgres EXPLAIN ANALYZE output.
Stars: ✭ 1,391 (+1187.96%)
Mutual labels:  performance
Repl
REPL rewrite for Node.js ✨🐢🚀✨
Stars: ✭ 101 (-6.48%)
Mutual labels:  performance
Optick
C++ Profiler For Games
Stars: ✭ 1,364 (+1162.96%)
Mutual labels:  performance
Nider
Python package to add text to images, textures and different backgrounds
Stars: ✭ 100 (-7.41%)
Mutual labels:  images
Gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Stars: ✭ 53,971 (+49873.15%)
Mutual labels:  performance
Server Tech Tree
服务端软件技术树:服务端主流技术九大分类和全景图
Stars: ✭ 106 (-1.85%)
Mutual labels:  performance
Tinyraycaster
486 lines of C++: old-school FPS in a weekend
Stars: ✭ 1,383 (+1180.56%)
Mutual labels:  images
Gif Frames
🖼 Extract frames from an animated GIF with pure JS
Stars: ✭ 100 (-7.41%)
Mutual labels:  images
Hermes
Hermes: a fault-tolerant replication protocol, implemented over RDMA, guaranteeing linearizability and achieving low latency and high throughput.
Stars: ✭ 105 (-2.78%)
Mutual labels:  performance
Imgix Php
A PHP client library for generating URLs with imgix
Stars: ✭ 100 (-7.41%)
Mutual labels:  images
Pg stat kcache
Gather statistics about physical disk access and CPU consumption done by backends.
Stars: ✭ 106 (-1.85%)
Mutual labels:  performance
Libfpta
Ultra fast, compact, Embedded Database for tabular and semistructured data.
Stars: ✭ 100 (-7.41%)
Mutual labels:  performance
Fast React Render
[DEPRECATED] Use last versions of React and Node.js for better performance
Stars: ✭ 102 (-5.56%)
Mutual labels:  performance
Taurus
Automation-friendly framework for Continuous Testing by
Stars: ✭ 1,566 (+1350%)
Mutual labels:  performance
Serverless Sharp
Serverless image optimizer for S3, Lambda, and Cloudfront
Stars: ✭ 102 (-5.56%)
Mutual labels:  images
Shopify Theme Inspector
A Chrome DevTools plugin that visualizes Shopify Liquid render profiling data so you can triage long-running code and reduce server response times!
Stars: ✭ 102 (-5.56%)
Mutual labels:  performance

LazyImages plugin for 11ty

Banner image

What this plugin does:

  • 🔍 Finds IMG elements in your markup
  • ➕ Adds width and height attributes to the element
  • ✋ Defers loading the image until it is in/near the viewport (lazy loading)
  • 🖼️ Displays a blurry low-res placeholder until the image has loaded (LQIP)

This plugin supports:

  • Any 11ty template format that outputs to a .html file
  • Absolute image paths
  • Relative image paths (improved in v2.1!)
  • Custom image selectors; target all images or only images in a certain part of the page
  • Placeholder generation for all image formats supported by Sharp; including JPEG, PNG, WebP, TIFF, GIF, & SVG
  • Responsive images using srcset; the image in the src attribute will be used for determining the placeholder image and width/height attributes

v2.1 just released! View the release/upgrade notes


Like this project? Buy me a coffee via PayPal or ko-fi


Getting started

Install the plugin

In your project directory run:

# Using npm
npm install eleventy-plugin-lazyimages --save-dev

# Or using yarn
yarn add eleventy-plugin-lazyimages --dev

Then update your project's .eleventy.js to include the plugin:

const lazyImagesPlugin = require('eleventy-plugin-lazyimages');

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(lazyImagesPlugin);
};

Tweak your CSS (optional)

This plugin will automatically set the width and height attributes for each image based on the source image dimensions. You might want to overwrite this with the following CSS:

img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
}

The above CSS will ensure the image is never wider than its container and the aspect ratio is maintained.

Configure the plugin (optional)

You can pass an object with configuration options as the second parameter:

eleventyConfig.addPlugin(lazyImagesPlugin, {
  imgSelector: '.post-content img', // custom image selector
  cacheFile: '', // don't cache results to a file
});

A full list of available configuration options are listed below, and some common questions are covered at the end of this file.

Configuration options

Key Type Description
maxPlaceholderWidth Integer The maximum width in pixels of the generated placeholder image. Recommended values are between 15 and 40.
Default: 25
maxPlaceholderHeight Integer The maximum height in pixels of the generated placeholder image. Recommended values are between 15 and 40.
Default: 25
imgSelector String The DOM selector used to find IMG elements in the markup.
Default: img
transformImgPath Function A function that takes the IMG src attribute and returns a string representing the actual file path to your image.
cacheFile String Cache image metadata and placeholder images to this filename. Greatly speeds up subsequent builds. Pass an empty string to turn off the cache.
Default: .lazyimages.json
appendInitScript Boolean Appends code to initialise lazy loading of images to the generated markup. Set this to false if you include your own lazy load script.
Default: true
scriptSrc String The URI for the lazy load script that is injected into the markup via appendInitScript.
Default: https://cdn.jsdelivr.net/npm/[email protected]/lazysizes.min.js
preferNativeLazyLoad Boolean Use the native browser loading="lazy" instead of the lazy load script (if available).
Default: false
setWidthAndHeightAttrs Boolean Set the width and height attributes on img elements to the actual size of the image file.
Default: true
className String[] The class names added to found IMG elements. You usually don't need to change this unless you're using a different scriptSrc.
Default: ['lazyload']

Example projects

Example projects using the plugin can be found in the /example directory.

Built with

  • JSDOM - To find and modify image elements in 11ty's generated markup
  • Sharp - To read image metadata and generate low-res placeholders
  • LazySizes - Handles lazy loading

Contributing

This project welcomes suggestions and Pull Requests!

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

Common questions

Can I host the lazy load script locally?

Yes! This plugin defaults to LazySizes from JSDelivr but you can specify a relative path via the scriptSrc configuration option.

Does my local image path have to match the output path?

(a.k.a Why do I have "Input file is missing" messages in my terminal?)

By default this plugin will look for the file referenced in a src attribute like <img src="/images/dog.jpg" /> at <project root>/images/dog.jpg or <project root>/src/images/dog.jpg.

Whereas a file referenced like <img src="./images/dog.jpg" /> or <img src="images/dog.jpg" /> is expected to be found at <input file directory>/images/dog.jpg.

If you prefer to store your images elsewhere the transformImgPath config option allows you to specify a function that points the plugin to your internal image path.

For example, if your file structure stores <img src="/images/dog.jpg" /> at <project root>/assets/dog.jpg you could set transformImgPath like:

// .eleventy.js
eleventyConfig.addPlugin(lazyImagesPlugin, {
  transformImgPath: (imgPath) => imgPath.replace('/images/', './assets/'),
});

The transformImgPath configuration option takes a function that receives two parameters; src, and options.

src is a string containing the value of the img elements src attribute.

options is an object containing the outputPath of the file being processed, as well as the outputDir, inputPath, inputDir, and extraOutputSubdirectory values from eleventy config.

Can I use a different lazy load script?

Yes! By default this plugin uses LazySizes to handle lazy loading, but any lazy load script that reads from the data-src attribute is supported via the scriptSrc configuration option.

We've included an example project in this repo demonstrating this plugin using vanilla-lazyload.

Note: if you need to modify the custom script's parameters the recommended approach is to set appendInitScript: false in this plugin's config. This tells the plugin to skip adding the script loader code to the page. It ignores any value set for scriptSrc and allows you to use your own method for including the custom script. The plugin will still set the data-src + width + height attributes on IMG tags and generate the low quality image placeholders, it just doesn't manage the actual lazy loading.

Can I use this plugin with a plugin that moves/renames image files?

Yes! The key to solving this problem is the order in which the plugins are defined in .eleventy.js. It is important this plugin runs after the plugin that moves/renames files otherwise this plugin may still be referencing the original filepath in the markup, not the one generated by the other plugin.

We've included an example project in this repo demonstrating this plugin with eleventy-plugin-local-images.

Upgrade notes

v2.1.0

This release improves support for relative file paths in src attributes.

transformImgPath now receives an optional second parameter containing the outputPath of the file being processed, as well as the outputDir, inputPath, inputDir, and extraOutputSubdirectory values from eleventy config.

This release also adds the setWidthAndHeightAttrs config option which allows you to turn off the setting of width and height attributes being added to img elements.

v2.0.0

The underlying tool used to generate placeholders has switched from JIMP to Sharp. This allows the plugin to handle a greater variety of image formats, while also increasing in speed.

The API remains largely the same so most sites should not need to adjust their config.

  • The default values for maxPlaceholderWidth and maxPlaceholderHeight have been increased from 12 to 25 - this increases the quality of the LQIP without a significant change in filesize
  • placeholderQuality has been removed - at the size of the LQIP it didn't make much of a difference to filesize or image quality
  • The default value for preferNativeLazyLoad is now false - most users install this plugin to generate LQIP and the previous default meant the LQIP weren't visible in modern browsers

Like this project? Buy me a coffee via PayPal or ko-fi


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].