imgLazyLoading()
Menambahkan atribut loading="lazy" pada gambar untuk pemuatan tertunda.
import { imgLazyLoading } from "@unifast/node";Signature
function imgLazyLoading(options?: ImgLazyLoadingPluginOptions): UnifastPluginParameter
options?
Konfigurasi untuk perilaku lazy loading
| Properti | Tipe | Default | Deskripsi |
|---|---|---|---|
skipFirst | number | 0 | Jumlah gambar yang dilewati (misalnya melewati gambar hero) |
Plugin menambahkan atribut loading="lazy" dan decoding="async" pada elemen <img> yang cocok, termasuk gambar yang bersarang di dalam elemen lain.
Penggunaan
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">Contoh
Melewati gambar pertama (pola hero image)
Gambar pertama pada sebuah halaman seringkali merupakan gambar hero atau banner yang harus dimuat secara eager. Gunakan skipFirst untuk mengecualikannya dari 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">Melewati beberapa gambar above-the-fold
import { compile, imgLazyLoading } from "@unifast/node";
const result = compile(md, {
plugins: [
imgLazyLoading({
skipFirst: 3, // skip the first 3 images
}),
],
});