JP
JP Codes
Change theme
Next.js

Images in Next.js

How to use Next.js's built-in next/image component — covering usage, the benefits of automatic optimisation, and the limitations you should know about before deploying.

James Platt

James Platt

November 11, 2020 · 4 min read

4 min read
Images in Next.js

Introduction

Next.js has built-in support for its own Image component via next/image. When you use it in place of the standard HTML <img> tag, Next.js automatically handles image optimisation, responsive sizing, and lazy loading — all without any extra configuration.

Basic Usage

In a page or component file, import and use the Image component directly:

import Image from 'next/image'

function Home() {
  return (
    <>
      <h1>My Homepage</h1>
      <Image
        src="/me.png"
        alt="Picture of the author"
        width={500}
        height={500}
      />
      <p>Welcome to my homepage!</p>
    </>
  )
}

export default Home

The width and height props are required when using local or sized images. They tell Next.js the intrinsic dimensions, which helps it prevent Cumulative Layout Shift (CLS).

Usage in Markdown and MDX

If you're using a Next.js blog with MDX, the standard Markdown image syntax is automatically replaced by the Image component at build time. Assuming you have a file at public/static/images/ocean.jpg:

![An ocean photo](/static/images/ocean.jpg)

This renders an optimised image with automatic WebP conversion where supported.

You can also use the Image component directly inside MDX files:

<Image alt="ocean" src="/static/images/ocean.jpg" width={256} height={128} />

Note: If you save an image rendered via next/image, it will be in WebP format if your browser supports it — resulting in significantly smaller file sizes than JPEG or PNG.

Benefits

Using next/image instead of a plain <img> tag gives you:

Limitations

The next/image component has some important limitations to be aware of:

Vercel dependency

The image optimisation feature relies on a serverless function behind the scenes. If you're not hosting on Vercel, you'll need an external image CDN (like Cloudinary or Imgix) or you'll need to disable image optimisation. You can do this by removing the relevant remark plugin from your MDX configuration, or by setting unoptimized: true in your Next.js image config for static exports.

External images require allowlisting

Images loaded from external URLs are not optimised through next/image by default. You must explicitly allowlist external domains in next.config.js:

// next.config.js
module.exports = {
  images: {
    domains: ['images.unsplash.com', 'cdn.example.com'],
  },
}

Public folder requirement

All locally stored images must be placed inside the public/ directory. The path you reference in src is relative to public/, so /static/images/photo.jpg maps to public/static/images/photo.jpg on disk.

Summary

For most Next.js projects, swapping <img> for <Image> is a straightforward win — you get better performance, smaller payloads, and CLS prevention with minimal effort. Just be aware of the Vercel dependency if you're deploying elsewhere, and allowlist any external image domains you need.

Tags

James Platt

James Platt

Web Developer

James is a Microsoft-qualified C# .NET developer with extensive experience building robust, data-rich web applications. He writes about web development, software architecture, and best practices at JP Codes.

Read more about James

More articles