imgLazyLoading()
Add loading="lazy" attribute to images for deferred loading.
import { imgLazyLoading } from "@unifast/node";Signature
function imgLazyLoading(options?: ImgLazyLoadingPluginOptions): UnifastPluginParameters
options?
Configuration for lazy loading behavior
| Property | Type | Default | Description |
|---|---|---|---|
skipFirst | number | 0 | Number of images to skip (e.g. skip hero image) |
The plugin adds both loading="lazy" and decoding="async" attributes to matched <img> elements, including images nested inside other elements.
Usage
import { compile, imgLazyLoading } from "@unifast/node";
const md = `



`;
const result = compile(md, {
plugins: [imgLazyLoading()],
});
// All images get loading="lazy" and decoding="async":
// <img src="photo1.jpg" alt="Photo 1" loading="lazy" decoding="async">
// <img src="photo2.jpg" alt="Photo 2" loading="lazy" decoding="async">
// <img src="photo3.jpg" alt="Photo 3" loading="lazy" decoding="async">Examples
Skip first image (hero image pattern)
The first image on a page is often a hero or banner image that should load eagerly. Use skipFirst to exclude it from lazy loading:
import { compile, imgLazyLoading } from "@unifast/node";
const md = `

Some introductory content...

More content...

`;
const result = compile(md, {
plugins: [
imgLazyLoading({
skipFirst: 1,
}),
],
});
// First image loads eagerly (no loading attribute):
// <img src="hero.jpg" alt="Hero banner">
//
// Remaining images are lazy loaded:
// <img src="diagram.jpg" alt="Diagram" loading="lazy" decoding="async">
// <img src="screenshot.jpg" alt="Screenshot" loading="lazy" decoding="async">Skip multiple above-the-fold images
import { compile, imgLazyLoading } from "@unifast/node";
const result = compile(md, {
plugins: [
imgLazyLoading({
skipFirst: 3, // skip the first 3 images
}),
],
});