Integrations
unlazy supports lazy loading images in your framework of choice. Each package provides a UnLazyImage component as a drop-in replacement for the native <img> element.
Creating Custom Integrations
To create a component for your framework, keep the following in mind:
- The component should wrap a native
<img>element with theloading="lazy"attribute set. - The component should support at least a
srcorsrcsetattribute. - Import the
lazyLoadfunction from the unlazy library and call it with aHTMLImageElementorHTMLSourceElementas the first argument:
ts
import { lazyLoad } from 'unlazy'
lazyLoad(target)- If the component gets unmounted before the image is loaded, call the cleanup function returned by
lazyLoad. Take the following example from the Vue integration:
ts
import { lazyLoad } from 'unlazy'
const target = ref<HTMLImageElement | undefined>()
onMounted(() => {
if (target.value) {
const cleanup = lazyLoad(target.value)
onBeforeUnmount(cleanup)
}
})Server-Side Rendering
unlazy's hash decoders work in any SSR runtime (Node, Deno, Bun, Workers). See Server-Side Rendering for framework-specific recipes.
Available Integrations
The following integrations are available as of now:
Frontend Frameworks
Meta-Frameworks
TIP
These include SSR-decoding of hash-based placeholders (BlurHash and ThumbHash).