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.
Available Integrations
- Vue
- React
- Solid
- Svelte
- Nuxt, which also decodes hash-based placeholders (BlurHash and ThumbHash) on the server
To decode hashes on the server in another framework, see Server-Side Rendering.
UnLazyImage Props
Every UnLazyImage component takes these props, typed as UnLazyImageProps in unlazy. Each integration page lists the props and events its component adds.
| Prop | Type | Default | Description |
|---|---|---|---|
src | String | - | Image source URL to be lazy-loaded. |
srcSet | String | - | Image source set to be lazy-loaded. |
sources | Array | - | Array of UnLazySource objects to render a <picture> element. |
autoSizes | Boolean | false | Whether the sizes attribute should be automatically calculated. |
blurhash | String | - | A BlurHash string representing the blurry placeholder image. |
thumbhash | String | - | A ThumbHash string representing the blurry placeholder image. If both are provided, thumbhash takes precedence. |
placeholderSrc | String | - | Optional image source URL for a custom placeholder image. Ignored if a hash is provided. Give each image its own URL. |
placeholderSize | Number | 32 | The size of the longer edge for BlurHash decoding, capped at 100. Ignored for ThumbHash. |
preload | Boolean | false | Whether the image should load right away instead of when it nears the viewport, for example the next slide of a slider. The hash placeholder stays until the image has loaded. |
loading | String | 'lazy' | Loading strategy for the image ('lazy' or 'eager'). 'eager' without autoSizes renders the real sources – see The Eager Path. |
lazyLoad | Boolean | true | Whether the image may load. Set to false to hold it on its placeholder until it turns true; loading then defaults to 'eager', and an eager image renders no real sources. preload takes precedence: with it set, the image loads and loading defaults to 'lazy'. |
UnLazySource
Every UnLazyImage component takes a sources prop. Each UnLazySource entry renders one <source> inside a <picture>. Every adapter package exports the type, such as import type { UnLazySource } from '@unlazy/react':
export interface UnLazySource {
/** Image source set for the `<source>` element. Will be applied lazily via `data-srcset`. */
srcSet: string
/**
* Media query that selects this source, e.g. `(max-width: 600px)`. Use this for art
* direction across breakpoints.
*/
media?: string
/** MIME type for format negotiation, e.g. `image/avif` or `image/webp`. */
type?: string
/**
* Explicit `sizes` value. When unset and the parent has automatic sizing enabled, the
* source falls back to `sizes="auto"` (resolved to a numeric pixel width).
*/
sizes?: string
/**
* Intrinsic width in pixels. When the matching `<source>` is selected, the browser uses
* this together with `height` to set the rendered `<img>`'s aspect ratio. Prevents
* layout shift when the breakpoint changes.
*/
width?: number
/** Intrinsic height in pixels. Pairs with `width` to prevent layout shift across breakpoints. */
height?: number
}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 theHTMLImageElementas the first argument:
import { lazyLoad } from 'unlazy'
lazyLoad(target)- Call the cleanup function returned by
lazyLoadwhen the component unmounts or its sources change, and calllazyLoadagain for the new sources once the DOM shows them. In Vue, for example:
<script setup lang="ts">
import { lazyLoad } from 'unlazy'
import { ref, watchPostEffect } from 'vue'
const props = defineProps<{ src: string }>()
const target = ref<HTMLImageElement | undefined>()
// Runs after the DOM update, so `lazyLoad` reads the new `data-src`
watchPostEffect((onCleanup) => {
if (!target.value) return
// Re-run when the source changes
void props.src
onCleanup(lazyLoad(target.value))
})
</script>
<template>
<img ref="target" :data-src="src" loading="lazy" />
</template>- Pass an image with its real
srcorsrcsetin the markup tolazyLoadtoo, so it reports the outcome. For an image you do not pass tolazyLoad, pass itsloadanderrorevents toreportImageOutcome, which sets or removes thedata-errorattribute and callsonImageLoadoronImageError:
import { reportImageOutcome } from 'unlazy'
const handleOutcome = (event: Event) =>
reportImageOutcome(image, event, { onImageLoad, onImageError })
image.addEventListener('load', handleOutcome)
image.addEventListener('error', handleOutcome)To take the same props as the official components, pass them to resolveImageAttributes. UnLazyImageAttributes types its result:
image/sources– the attributes of the<img>and of every<source>, under their HTML names.lazyLoadOptions– options to pass tolazyLoadnext to youronImageLoadandonImageError.loadKey– a string of everything that decides how the image loads. RunlazyLoadagain when it changes instead of on every new props object, so an inlinesourcesarray does not reload the image.