Next.js Image with BlurHash and Custom Loaders

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Baseline date: 2026-10-07. This page adds the media-pipeline view to Images, Fonts and Scripts, which is the reference for the component, its configuration defaults, preload and the Pages Router. It follows the Next.js 16 line (npm next 16.4.0 on the baseline date). Verify prop names against the Image component reference for the exact version in your project.

Two ways to use next/image with a media backend

  1. Let Next.js optimise. The component requests a /_next/image URL with the source, width and quality as query parameters, and the Next.js server (or the platform’s image service) fetches the original, resizes it and returns AVIF or WebP according to the browser’s Accept header. You store one original per image. This is the quickest to adopt, and it costs CPU on the server and a cache.

  2. Use the variants from your own pipeline. The backend already produced w320.avif, w640.avif, and so on, see responsive images and thumbnails. Use a custom loader that builds the CDN URL for a requested width, so Next.js only produces the srcset. The browser then downloads the pre-optimised file straight from the CDN.

Basic usage

import Image from 'next/image';

export function Cover({ cover }: { cover: { url: string; width: number; height: number; alt: string } }) {
  return (
    <Image
      src={cover.url}
      alt={cover.alt}
      width={cover.width}
      height={cover.height}
      sizes="(min-width: 60em) 20vw, 50vw"
    />
  );
}
  • width and height are the intrinsic size and reserve space. They are required for remote images unless you use fill.

  • sizes is the single most effective prop. Without it, Next.js generates a srcset for fixed densities, and with it a width-based srcset the browser can choose from.

  • fill makes the image fill its positioned parent, for when the size is unknown. Give the parent position: relative and a size, for example with aspect-ratio, and set object-fit. Always combine fill with sizes.

<div style={{ position: 'relative', aspectRatio: '2 / 3' }}>
  <Image src={cover.url} alt={cover.alt} fill sizes="(min-width: 60em) 20vw, 50vw" style={{ objectFit: 'cover' }} />
</div>

Formats, remote patterns and caching

// next.config.ts
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  images: {
    formats: ['image/avif', 'image/webp'],
    remotePatterns: [{ protocol: 'https', hostname: 'cdn.example.com', pathname: '/covers/**' }],
    minimumCacheTTL: 60 * 60 * 24 * 30,
  },
};
export default nextConfig;
  • formats lists the output formats in order of preference. The default is WebP only. AVIF compresses smaller and encodes more slowly, and each format is cached separately.

  • remotePatterns is an allow list. Without it, remote images are rejected, which stops the optimiser from becoming an open proxy. Be as specific as possible.

  • minimumCacheTTL sets the minimum lifetime of an optimised image. Next.js has no invalidation, so use content-hashed URLs from the backend rather than overwriting an image at the same URL.

References: formats and remotePatterns.

placeholder="blur" and blurDataURL

placeholder="blur" shows a blurred image until the real one loads. Static imports (import cover from './cover.jpg') get a generated blurDataURL. For remote images you must supply it, and that is where the BlurHash from the API fits: decode it on the server, once, into a small data URL.

npm install blurhash sharp
// lib/blurhash.ts (server only)
import { decode } from 'blurhash';
import sharp from 'sharp';

export async function blurhashToDataUrl(hash: string, width = 16, height = 16): Promise<string> {
  const pixels = decode(hash, width, height);
  const png = await sharp(Buffer.from(pixels), { raw: { width, height, channels: 4 } }).png().toBuffer();
  return `data:image/png;base64,${png.toString('base64')}`;
}
// app/books/[id]/page.tsx  (a Server Component)
import Image from 'next/image';
import { blurhashToDataUrl } from '@/lib/blurhash';

export default async function BookPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const book = await getBook(id);                       // { cover: { url, width, height, blurhash } }
  const blurDataURL = await blurhashToDataUrl(book.cover.blurhash);
  return (
    <Image
      src={book.cover.url}
      alt={book.title}
      width={book.cover.width}
      height={book.cover.height}
      sizes="(min-width: 60em) 20vw, 50vw"
      placeholder="blur"
      blurDataURL={blurDataURL}
    />
  );
}

The decoded size of 16 by 16 keeps the data URL small, since the same bytes are in every HTML page that shows the image. Next.js blurs the data URL, and you can cache the result with the backend response. Use the 'use cache' directive or a memoised function if the decode shows in a profile, see Next.js caching. Client-side alternative: a canvas, see BlurHash in the browser.

A custom loader for your CDN

A loader is a function that receives src, width and quality and returns the URL for that width. With pre-generated variants:

// lib/media-loader.ts
import type { ImageLoaderProps } from 'next/image';

const WIDTHS = [320, 640, 1280];

export default function mediaLoader({ src, width }: ImageLoaderProps): string {
  // src is the image id, for example "b81f"; choose the smallest variant that is at least `width`
  const chosen = WIDTHS.find((w) => w >= width) ?? WIDTHS[WIDTHS.length - 1];
  return `https://cdn.example.com/covers/${src}/w${chosen}.avif`;
}
// components/cover.tsx -- a loader is a function prop, so it needs a Client Component
'use client';

import Image from 'next/image';
import mediaLoader from '@/lib/media-loader';

export function Cover({ id }: { id: string }) {
  return <Image loader={mediaLoader} src={id} alt="Cover" width={640} height={960} sizes="50vw" />;
}

For an image CDN with URL transformations, the loader builds that URL. For Cloudinary, imgix and similar services:

export const cloudinaryLoader = ({ src, width, quality }: ImageLoaderProps) =>
  `https://res.cloudinary.com/<cloud>/image/upload/f_auto,q_${quality ?? 'auto'},w_${width}/${src}`;

export const imgixLoader = ({ src, width, quality }: ImageLoaderProps) =>
  `https://example.imgix.net/${src}?auto=format&w=${width}&q=${quality ?? 75}`;

Set a loader per component, which only works in a Client Component because functions cannot be passed from a Server Component, or globally with the images.loader option set to custom and images.loaderFile. The global setting also works in Server Components and is the better default. With a custom loader, Next.js does not optimise the image itself, so the formats setting does not apply: the CDN chooses the format. References: loader, Cloudinary documentation and imgix rendering API. The managed image-processing options of each cloud are on the cloud pages: AWS, Google Cloud and Azure.

Next.js 16: preload instead of priority

The LCP image used to take the priority prop. In Next.js 16 priority is deprecated. Use preload to insert a <link rel="preload"> for the image in the head, or loading="eager" or fetchPriority="high" when preloading is not what you want (for example when several images could be the LCP element depending on the viewport).

<Image src={hero.url} alt={hero.alt} width={1920} height={1080} sizes="100vw" preload />
<Image src={hero.url} alt={hero.alt} width={1920} height={1080} sizes="100vw" fetchPriority="high" />

Do not combine preload with loading or fetchPriority. The full rules, with the reasons, are in Images, Fonts and Scripts.

What happens to a request

The two strategies differ in who produces the bytes. With the built-in optimiser every distinct width, quality and format is a cache entry that the first visitor pays for. With a custom loader the browser talks to the CDN directly and Next.js only generates markup.

sequenceDiagram participant B as Browser participant N as Next.js image optimiser participant O as Origin or CDN B->>N: GET /_next/image?url=...&w=640&q=75 with Accept header alt cache hit N-->>B: cached AVIF or WebP else cache miss N->>O: fetch the original, checked against remotePatterns O-->>N: original bytes N->>N: resize, pick format from Accept and images.formats N-->>B: optimised image with Cache-Control end Note over B,O: With a custom loader the browser requests the CDN URL directly and this hop disappears

Choosing a strategy for the cover grid

The running example is a catalogue page: a hero image, a first row of covers that is visible without scrolling, and a long grid below. The decisions follow from where the bytes come from:

Part of the page Props Reason

Hero

preload (or fetchPriority="high"), sizes="100vw", no loading

It is the likely LCP element, so it must be requested from the document head, never lazily.

First row of covers

sizes only, default lazy loading

A lazy image that is already inside the viewport is fetched as soon as layout places it, so no extra prop is needed. Mark one fetchPriority="high" only if a cover is the LCP element.

Rest of the grid

sizes, placeholder="blur", blurDataURL

Lazy by default. The placeholder keeps the box filled while the file arrives.

// app/books/page.tsx (a Server Component)
import Image from 'next/image';
import { blurhashToDataUrl } from '@/lib/blurhash';

type Cover = { id: string; url: string; width: number; height: number; blurhash: string; title: string };

export default async function Catalogue() {
  const covers: Cover[] = await listCovers();
  const placeholders = await Promise.all(covers.map((cover) => blurhashToDataUrl(cover.blurhash)));
  return (
    <ul className="grid">
      {covers.map((cover, index) => (
        <li key={cover.id}>
          <Image
            src={cover.url}
            alt={cover.title}
            width={cover.width}
            height={cover.height}
            sizes="(min-width: 60em) 20vw, 50vw"
            placeholder="blur"
            blurDataURL={placeholders[index]}
          />
        </li>
      ))}
    </ul>
  );
}

Promise.all decodes the hashes concurrently, and the 16 by 16 decode keeps the HTML small for a long list: with 100 covers on the page the inline placeholders add roughly 100 times the size of one data URL, which is why the page keeps them at 16 pixels or fewer.

The loader contract, step by step

Next.js calls the loader once per width it puts in the srcset. With sizes set, the candidates come from the union of the deviceSizes and imageSizes configuration lists, so a loader receives widths such as 384, 640 or 750 that you do not have as files. The loader’s job is to map an arbitrary requested width to a real variant, rounding up so the image is never blurrier than requested:

// lib/media-loader.test.ts -- run with a TypeScript-aware runner, for example `npx tsx lib/media-loader.test.ts`
import assert from 'node:assert/strict';
import mediaLoader from './media-loader';

assert.equal(mediaLoader({ src: 'b81f', width: 384 }), 'https://cdn.example.com/covers/b81f/w640.avif');    // rounds up
assert.equal(mediaLoader({ src: 'b81f', width: 320 }), 'https://cdn.example.com/covers/b81f/w320.avif');    // exact match
assert.equal(mediaLoader({ src: 'b81f', width: 3840 }), 'https://cdn.example.com/covers/b81f/w1280.avif');  // capped at the largest

The cap means the browser upscales for very wide, high-DPR screens. If the largest variant is too small for them, add a 2560 variant to the pipeline rather than raising the cap. The same three assertions work against the Angular loader on the Angular page, since both implement one rule: the smallest variant that is at least as wide as the request.

A loader should also be cheap and pure. It runs during rendering for every image, so it must not read the network or the file system, and it must return the same URL for the same input so the HTML stays cacheable.

Route handlers and server actions

Uploads and variant URLs can also go through Next.js route handlers and server actions, which act as a backend for frontend, see route handlers and BFF. Keep heavy processing out of them (see upload pipelines), and use them to hand out pre-signed URLs and to read status.