Skip to content

@unlazy/nuxt ​

The Nuxt module provides an auto-imported UnLazyImage component as a drop-in replacement for native <img> and <picture> elements (when using the sources prop). It requires Nuxt 4.

Installation ​

Install the @unlazy/nuxt package using your favorite package manager:

bash
pnpm add @unlazy/nuxt
bash
yarn add @unlazy/nuxt
bash
npm install @unlazy/nuxt

Add the @unlazy/nuxt module to your Nuxt configuration:

ts
// `nuxt.config.ts`
export default defineNuxtConfig({
  modules: ['@unlazy/nuxt'],
})

Module Options ​

ts
export interface ModuleOptions {
  /**
   * Whether to generate the blurry placeholder on the server-side if a BlurHash
   * or ThumbHash is provided via the `blurhash`, respectively `thumbhash` prop.
   *
   * @default true
   */
  ssr?: boolean

  /**
   * The size of the longer edge (width or height) of the BlurHash image to be
   * decoded, depending on the aspect ratio.
   *
   * @remarks
   * This option is ignored if a ThumbHash is used.
   *
   * @default 32
   */
  placeholderSize?: number
}

Set the options under the unlazy key:

ts
// `nuxt.config.ts`
export default defineNuxtConfig({
  modules: ['@unlazy/nuxt'],

  // Module options
  unlazy: {
    ssr: false,
  },
})

With ssr: true, UnLazyImage decodes the hash on the server and again during hydration to the same src, so the server-rendered placeholder stays until the real image has loaded.

WARNING

With ssr: false, the server-rendered HTML has no hash placeholder. The browser decodes the hash after hydration and takes a BlurHash's aspect ratio from the rendered image, so the placeholder appears only then instead of on first paint.

UnLazyImage Component ​

The UnLazyImage component is globally available in your Nuxt application.

Props ​

UnLazyImage takes the props every integration shares and two of its own:

PropTypeDefaultDescription
placeholderRatioNumber-Aspect ratio (width / height) of a BlurHash decoded on the server. Defaults to the ratio of the width and height attributes when both are positive numbers, otherwise square.
ssrBooleanModule option ssrWhether hashes should be decoded on the server. Overrides the module option.

The component also accepts all standard <img> HTML attributes via Nuxt's attribute inheritance.

Emitted Events ​

EventPayloadDescription
image-loadHTMLImageElementFired once when the real image finishes loading.
image-errorHTMLImageElement, EventFired when the image fails to load.

A native @error listener falls through to the <img> and fires both for the browser's errors and for the synthetic error event unlazy dispatches when its preload fails. Use @image-error for the image element with a typed payload.

Examples ​

html
<UnLazyImage
  :blurhash="blurhash"
  src-set="image-320w.jpg 320w, image-640w.jpg 640w"
  auto-sizes
  width="640"
  height="320"
/>

For a ThumbHash, pass :thumbhash instead of :blurhash, and add :ssr="false" to decode the hash in the browser. For an inlined placeholder image, pass placeholder-src="data:image/svg+xml, ..." instead.

INFO

When using BlurHash, set explicit width and height attributes. See Hash-Based Placeholders for why.

Multiple Image Sources ​

Pass an array of UnLazySource objects as sources to render a <picture> element with format negotiation and art direction. Each entry becomes a <source> child:

vue
<script setup lang="ts">
import type { UnLazySource } from '@unlazy/nuxt'

const sources: UnLazySource[] = [
  { type: 'image/avif', srcSet: 'hero.avif 1x, hero@2x.avif 2x' },
  {
    media: '(max-width: 600px)',
    srcSet: 'hero-mobile.jpg',
    width: 480,
    height: 640,
  },
]
</script>

<template>
  <UnLazyImage src="/hero.jpg" :sources="sources" width="640" height="427" />
</template>

Released under the MIT License.