{"id":15016468,"url":"https://github.com/liamfiddler/eleventy-plugin-lazyimages","last_synced_at":"2025-04-05T16:11:01.968Z","repository":{"id":34947552,"uuid":"190408612","full_name":"liamfiddler/eleventy-plugin-lazyimages","owner":"liamfiddler","description":"Eleventy plugin that adds blurry placeholders \u0026 lazy loading to your images","archived":false,"fork":false,"pushed_at":"2024-06-19T07:33:53.000Z","size":404,"stargazers_count":139,"open_issues_count":5,"forks_count":29,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T15:08:54.822Z","etag":null,"topics":["11ty","eleventy","eleventy-plugin","images","lqip","performance","placeholder","placeholder-image"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liamfiddler.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-05T14:18:15.000Z","updated_at":"2024-10-10T04:30:17.000Z","dependencies_parsed_at":"2024-02-26T23:42:36.285Z","dependency_job_id":"5c2d4689-ab98-4209-a6cd-fa577b54aa28","html_url":"https://github.com/liamfiddler/eleventy-plugin-lazyimages","commit_stats":{"total_commits":99,"total_committers":14,"mean_commits":7.071428571428571,"dds":"0.23232323232323238","last_synced_commit":"8c43de3ea469e3d8d699b84b414fb6e37cc70ff5"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liamfiddler%2Feleventy-plugin-lazyimages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liamfiddler%2Feleventy-plugin-lazyimages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liamfiddler%2Feleventy-plugin-lazyimages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liamfiddler%2Feleventy-plugin-lazyimages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liamfiddler","download_url":"https://codeload.github.com/liamfiddler/eleventy-plugin-lazyimages/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361695,"owners_count":20926643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["11ty","eleventy","eleventy-plugin","images","lqip","performance","placeholder","placeholder-image"],"created_at":"2024-09-24T19:48:56.532Z","updated_at":"2025-04-05T16:11:01.945Z","avatar_url":"https://github.com/liamfiddler.png","language":"JavaScript","funding_links":["https://paypal.me/liamfiddler","https://ko-fi.com/liamfiddler"],"categories":[],"sub_categories":[],"readme":"# LazyImages plugin for [11ty](https://www.11ty.io/)\n\n![Banner image](https://repository-images.githubusercontent.com/190408612/4305b000-94d2-11e9-922c-72a93cafadcf)\n\nWhat this plugin does:\n\n- 🔍 Finds IMG elements in your markup\n- ➕ Adds width and height attributes to the element\n- ✋ Defers loading the image until it is in/near the viewport\n  (lazy loading)\n- 🖼️ Displays a blurry low-res placeholder until the image has loaded\n  (\u003cabbr title=\"Low Quality Image Placeholder\"\u003eLQIP\u003c/abbr\u003e)\n\nThis plugin supports:\n\n- Any 11ty template format that outputs to a .html file\n- Absolute image paths\n- Relative image paths (improved in v2.1!)\n- Custom image selectors; target all images or only images in a certain part\n  of the page\n- Placeholder generation for all image formats supported by\n  [Sharp](https://sharp.pixelplumbing.com/); including JPEG, PNG, WebP, TIFF, GIF, \u0026 SVG\n- Responsive images using `srcset`; the image in the `src` attribute will be\n  used for determining the placeholder image and width/height attributes\n\n---\n\n**v2.1 just released! [View the release/upgrade notes](#upgrade-notes)**\n\n---\n\n**Like this project? Buy me a coffee via [PayPal](https://paypal.me/liamfiddler) or [ko-fi](https://ko-fi.com/liamfiddler)**\n\n---\n\n## Getting started\n\n### Install the plugin\n\nIn your project directory run:\n\n```sh\n# Using npm\nnpm install eleventy-plugin-lazyimages --save-dev\n\n# Or using yarn\nyarn add eleventy-plugin-lazyimages --dev\n```\n\nThen update your project's `.eleventy.js` to include the plugin:\n\n```js\nconst lazyImagesPlugin = require('eleventy-plugin-lazyimages');\n\nmodule.exports = function (eleventyConfig) {\n  eleventyConfig.addPlugin(lazyImagesPlugin);\n};\n```\n\n### Tweak your CSS (optional)\n\nThis plugin will automatically set the width and height attributes\nfor each image based on the source image dimensions. You might want\nto overwrite this with the following CSS:\n\n```css\nimg {\n  display: block;\n  width: 100%;\n  max-width: 100%;\n  height: auto;\n}\n```\n\nThe above CSS will ensure the image is never wider than its\ncontainer and the aspect ratio is maintained.\n\n### Configure the plugin (optional)\n\nYou can pass an object with configuration options as the second\nparameter:\n\n```js\neleventyConfig.addPlugin(lazyImagesPlugin, {\n  imgSelector: '.post-content img', // custom image selector\n  cacheFile: '', // don't cache results to a file\n});\n```\n\nA full list of available configuration options are listed below,\nand some common questions are covered at the end of this file.\n\n## Configuration options\n\n| Key                      | Type     | Description                                                                                                                                                                   |\n| ------------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `maxPlaceholderWidth`    | Integer  | The maximum width in pixels of the generated placeholder image. Recommended values are between 15 and 40.\u003cbr\u003eDefault: `25`                                                    |\n| `maxPlaceholderHeight`   | Integer  | The maximum height in pixels of the generated placeholder image. Recommended values are between 15 and 40.\u003cbr\u003eDefault: `25`                                                   |\n| `imgSelector`            | String   | The DOM selector used to find IMG elements in the markup.\u003cbr\u003eDefault: `img`                                                                                                   |\n| `transformImgPath`       | Function | A function that takes the IMG `src` attribute and returns a string representing the actual file path to your image.                                                           |\n| `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.\u003cbr\u003eDefault: `.lazyimages.json` |\n| `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.\u003cbr\u003eDefault: `true`                   |\n| `scriptSrc`              | String   | The URI for the lazy load script that is injected into the markup via `appendInitScript`.\u003cbr\u003eDefault: `https://cdn.jsdelivr.net/npm/lazysizes@5/lazysizes.min.js`             |\n| `preferNativeLazyLoad`   | Boolean  | Use the native browser `loading=\"lazy\"` instead of the lazy load script (if available).\u003cbr\u003eDefault: `false`                                                                   |\n| `setWidthAndHeightAttrs` | Boolean  | Set the `width` and `height` attributes on `img` elements to the actual size of the image file.\u003cbr\u003eDefault: `true`                                                            |\n| `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`.\u003cbr\u003eDefault: `['lazyload']`                    |\n\n## Example projects\n\nExample projects using the plugin can be found in the\n[`/example`](./example) directory.\n\n- [Basic](./example/basic) - using default configuration\n- [Custom selector](./example/custom-selector) - using a custom image selector to only target image in certain DIVs\n- [Usage with eleventy-plugin-local-images](./example/eleventy-plugin-local-images) - using this plugin with [eleventy-plugin-local-images](https://github.com/robb0wen/eleventy-plugin-local-images)\n- [Usage with vanilla-lazyload](./example/verlok-vanilla-lazyload) - using this plugin with [vanilla-lazyload](https://www.npmjs.com/package/vanilla-lazyload)\n\n## Built with\n\n- [JSDOM](https://github.com/jsdom/jsdom) - To find and modify image\n  elements in 11ty's generated markup\n- [Sharp](https://sharp.pixelplumbing.com/) - To read image\n  metadata and generate low-res placeholders\n- [LazySizes](https://github.com/aFarkas/lazysizes) - Handles lazy loading\n\n## Contributing\n\nThis project welcomes suggestions and Pull Requests!\n\n## Authors\n\n- **Liam Fiddler** - _Initial work / maintainer_ - [@liamfiddler](https://github.com/liamfiddler)\n\nSee also the list of\n[contributors](https://github.com/liamfiddler/eleventy-plugin-lazyimages/contributors)\nwho participated in this project.\n\n## License\n\nThis project is licensed under the MIT License -\nsee the [LICENSE](LICENSE) file for details\n\n## Acknowledgments\n\n- The wonderfully supportive team at\n  [Mentally Friendly](https://mentallyfriendly.com)\n- Everyone who has contributed to the\n  [11ty](https://www.11ty.io/) project, without whom\n  this plugin wouldn't run\n- [José M. Pérez's blog post about progressive image loading](https://jmperezperez.com/medium-image-progressive-loading-placeholder/)\n  which served as the inspiration for this plugin\n- [Addy Osmani's blog post about lazy loading](https://addyosmani.com/blog/lazy-loading/)\n  which served as the inspiration for the init script\n\n## Common questions\n\n### Can I host the lazy load script locally?\n\nYes! This plugin defaults to\n[LazySizes from JSDelivr](https://cdn.jsdelivr.net/npm/lazysizes@5/lazysizes.min.js)\nbut you can specify a relative path via the `scriptSrc` configuration option.\n\n### Does my local image path have to match the output path?\n\n**(a.k.a Why do I have \"Input file is missing\" messages in my terminal?)**\n\nBy default this plugin will look for the file referenced in a `src` attribute like\n`\u003cimg src=\"/images/dog.jpg\" /\u003e` at `\u003cproject root\u003e/images/dog.jpg` or\n`\u003cproject root\u003e/src/images/dog.jpg`.\n\nWhereas a file referenced like\n`\u003cimg src=\"./images/dog.jpg\" /\u003e` or `\u003cimg src=\"images/dog.jpg\" /\u003e` is expected to\nbe found at `\u003cinput file directory\u003e/images/dog.jpg`.\n\nIf you prefer to store your images elsewhere the `transformImgPath` config\noption allows you to specify a function that points the plugin to your\ninternal image path.\n\nFor example, if your file structure stores `\u003cimg src=\"/images/dog.jpg\" /\u003e`\nat `\u003cproject root\u003e/assets/dog.jpg` you could set `transformImgPath` like:\n\n```js\n// .eleventy.js\neleventyConfig.addPlugin(lazyImagesPlugin, {\n  transformImgPath: (imgPath) =\u003e imgPath.replace('/images/', './assets/'),\n});\n```\n\nThe `transformImgPath` configuration option takes a function that receives two\nparameters; `src`, and `options`.\n\n`src` is a string containing the value of the `img` elements `src` attribute.\n\n`options` is an object containing the `outputPath` of the file being processed,\nas well as the `outputDir`, `inputPath`, `inputDir`, and\n`extraOutputSubdirectory` values from eleventy config.\n\n### Can I use a different lazy load script?\n\nYes! By default this plugin uses [LazySizes](https://github.com/aFarkas/lazysizes)\nto handle lazy loading, but any lazy load script that reads from the `data-src`\nattribute is supported via the `scriptSrc` configuration option.\n\nWe've included an [example project in this repo](./example/verlok-vanilla-lazyload)\ndemonstrating this plugin using\n[vanilla-lazyload](https://www.npmjs.com/package/vanilla-lazyload).\n\nNote: if you need to modify the custom script's parameters the recommended approach\nis to set `appendInitScript: false` in this plugin's config. This tells the plugin\nto skip adding the script loader code to the page. It ignores any value set for\nscriptSrc and allows you to use your own method for including the custom script.\nThe plugin will still set the `data-src` + `width` + `height` attributes on IMG\ntags and generate the low quality image placeholders, it just doesn't manage the\nactual lazy loading.\n\n### Can I use this plugin with a plugin that moves/renames image files?\n\nYes! The key to solving this problem is the order in which the plugins are\ndefined in `.eleventy.js`. It is important this plugin runs after the plugin\nthat moves/renames files otherwise this plugin may still be referencing the\noriginal filepath in the markup, not the one generated by the other plugin.\n\nWe've included an\n[example project in this repo](./example/eleventy-plugin-local-images)\ndemonstrating this plugin with\n[eleventy-plugin-local-images](https://github.com/robb0wen/eleventy-plugin-local-images).\n\n## Upgrade notes\n\n### v2.1.0\n\nThis release improves support for relative file paths in `src` attributes.\n\n`transformImgPath` now receives an optional second parameter containing the `outputPath`\nof the file being processed, as well as the `outputDir`, `inputPath`, `inputDir`, and\n`extraOutputSubdirectory` values from eleventy config.\n\nThis release also adds the `setWidthAndHeightAttrs` config option which allows you to turn\noff the setting of `width` and `height` attributes being added to `img` elements.\n\n### v2.0.0\n\nThe underlying tool used to generate placeholders has switched from JIMP to Sharp.\nThis allows the plugin to handle a greater variety of image formats, while also increasing in speed.\n\nThe API remains largely the same so most sites should not need to adjust their config.\n\n- 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\n- `placeholderQuality` has been removed - at the size of the LQIP it didn't make much of a difference to filesize or image quality\n- 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\n\n---\n\n**Like this project? Buy me a coffee via [PayPal](https://paypal.me/liamfiddler) or [ko-fi](https://ko-fi.com/liamfiddler)**\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliamfiddler%2Feleventy-plugin-lazyimages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliamfiddler%2Feleventy-plugin-lazyimages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliamfiddler%2Feleventy-plugin-lazyimages/lists"}