Skip to content

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 ​

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.

PropTypeDefaultDescription
srcString-Image source URL to be lazy-loaded.
srcSetString-Image source set to be lazy-loaded.
sourcesArray-Array of UnLazySource objects to render a <picture> element.
autoSizesBooleanfalseWhether the sizes attribute should be automatically calculated.
blurhashString-A BlurHash string representing the blurry placeholder image.
thumbhashString-A ThumbHash string representing the blurry placeholder image. If both are provided, thumbhash takes precedence.
placeholderSrcString-Optional image source URL for a custom placeholder image. Ignored if a hash is provided. Give each image its own URL.
placeholderSizeNumber32The size of the longer edge for BlurHash decoding, capped at 100. Ignored for ThumbHash.
preloadBooleanfalseWhether 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.
loadingString'lazy'Loading strategy for the image ('lazy' or 'eager'). 'eager' without autoSizes renders the real sources – see The Eager Path.
lazyLoadBooleantrueWhether 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':

ts
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:

  1. The component should wrap a native <img> element with the loading="lazy" attribute set.
  2. The component should support at least a src or srcset attribute.
  3. Import the lazyLoad function from the unlazy library and call it with the HTMLImageElement as the first argument:
ts
import { lazyLoad } from 'unlazy'

lazyLoad(target)
  1. Call the cleanup function returned by lazyLoad when the component unmounts or its sources change, and call lazyLoad again for the new sources once the DOM shows them. In Vue, for example:
vue
<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>
  1. Pass an image with its real src or srcset in the markup to lazyLoad too, so it reports the outcome. For an image you do not pass to lazyLoad, pass its load and error events to reportImageOutcome, which sets or removes the data-error attribute and calls onImageLoad or onImageError:
ts
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 to lazyLoad next to your onImageLoad and onImageError.
  • loadKey – a string of everything that decides how the image loads. Run lazyLoad again when it changes instead of on every new props object, so an inline sources array does not reload the image.

Released under the MIT License.