Skip to content
codmaker

Next.js Image Optimization: A Practical next/image Guide

How to optimize images in Next.js: next/image sizing, priority for LCP, AVIF and WebP, remote patterns, and custom loaders for CMS and CDN images.

CodmakerUpdated 8 min read
Share

What Does next/image Actually Do?

The Image component from next/image is a wrapper around the HTML img tag that handles the boring parts of image performance for you. It resizes images on demand, serves modern formats when the browser supports them, lazy loads images below the fold, and reserves space so the layout doesn't jump.

Behind it sits the Next.js image optimizer. When a browser asks for an image, the optimizer generates a version at the requested width and quality, caches it, and serves it with the right content type. You ship one source image. Users get a file sized for their screen.

The defaults are good, but defaults are not a strategy. Most image problems on Next.js sites come from a few props used wrong, not from the component itself.

Why Images Hurt Your LCP Score

Images are usually the LCP culprit. Largest Contentful Paint measures when the biggest visible element finishes rendering, and on most marketing pages, blog posts and product pages, that element is a hero image.

next/image handles lazy loading automatically. That is great for images further down the page and bad for the one image the user sees first. A lazy-loaded hero waits until the browser decides it is needed, which pushes LCP back.

That is why priority is the property you need to master for above-the-fold content. Setting it tells Next.js to load that image eagerly and preload it, so the browser starts fetching it early. Use it on the LCP image and nowhere else. Marking every image as a priority means none of them are.

Newer Next.js releases have been reworking how eager loading is expressed, with options like preload and fetchPriority. Check the image docs for your version. The principle doesn't change: load the hero early, lazy load the rest.

How to Size Images Correctly with width, height, fill and sizes

next/image needs to know an image's dimensions to reserve space and avoid layout shift. For a static import, Next.js reads the width and height from the file at build time. For a remote image, you pass width and height yourself.

When the image should stretch to its container, use the fill prop instead. The parent element needs position relative (or absolute or fixed) and a defined size. Combine fill with the CSS object-fit property to control cropping.

The sizes prop is the one most people skip, and it matters. Without it, the browser can't tell how wide the image will render, so it may download a much larger file than it needs. For a responsive image, write sizes like a media query: full viewport width on small screens, half width on medium, a fixed width on large. The browser then picks the closest generated size.

  • Static imports get width and height for free
  • Remote images need explicit width and height, or fill
  • Always pair fill with a sizes value that matches your layout
  • Use object-fit to control cropping with fill

How to Enable AVIF and WebP in Next.js

Next.js serves WebP by default when the browser supports it. AVIF usually compresses smaller than WebP at similar quality, but it is slower to encode, so it is opt-in.

To turn it on, set the formats option under images in next.config, listing AVIF before WebP. The optimizer then checks the browser's Accept header and serves the first format it supports, falling back to WebP and then the original.

The trade-off is CPU time on the first request for each size. Encoded results are cached, so the cost is paid once per variant, not on every view. On image-heavy sites, watch the server or function time when you first switch it on.

How to Configure remotePatterns for External Images

By default, the optimizer won't touch images from other domains. That is a safety feature. Without it, anyone could use your server to resize arbitrary images from the internet.

To allow an external source, add an entry to remotePatterns under images in next.config. Each entry can specify protocol, hostname, port and pathname. Be specific. Allow the exact host and a path prefix for your bucket or CMS, not a wildcard for every domain.

If you see an error saying a hostname is not configured, this is the setting it is pointing at.

When to Use a Custom Loader Instead

If you use a CMS like Contentful or Sanity, don't let Next.js optimize the images again. These platforms already run image CDNs that resize, crop and convert formats through URL parameters. Running their output through the Next.js optimizer doubles the work.

Use a custom loader to lean on the CMS's CDN transformations instead. A loader is a small function that receives src, width and quality and returns a URL. For a CMS, that URL is the image URL with the CMS's own width and quality parameters added. next/image still generates the srcset and handles lazy loading, but the actual resizing happens on the CDN.

It saves build time and bandwidth costs. You can pass a loader per image with the loader prop, or set a project-wide loaderFile in next.config so every Image component uses it.

If an image needs no processing at all, such as a tiny SVG icon, the unoptimized prop skips the optimizer for that one image.

Common Next.js Image Mistakes

These show up in almost every performance audit of a Next.js site.

  • Lazy loading the hero image because priority was never set
  • Setting priority on every image, which removes the benefit
  • Using fill without sizes, so the browser downloads oversized files
  • Running CMS images through the Next.js optimizer instead of a custom loader
  • Allowing broad wildcard hosts in remotePatterns
  • Uploading huge source files and trusting the optimizer to fix everything

Frequently Asked Questions

Does next/image lazy load by default?

Yes. Images rendered with next/image are lazy loaded unless you tell Next.js to load them eagerly, for example with priority on your above-the-fold image.

Why is my Next.js image blurry?

Usually the sizes value is wrong, so the browser picks a smaller variant than the rendered width needs. It can also be a low quality setting or a small source file. Check sizes first, then quality.

How do I use external images in Next.js?

Add the image host to remotePatterns under images in next.config, then pass the full URL as src along with width and height or fill. If the host runs its own image CDN, consider a custom loader instead.

Does Next.js support AVIF?

Yes, but it is opt-in. Add AVIF to the formats option in your images config. Next.js serves it to browsers that accept it and falls back to WebP or the original format for the rest.

Should I use next/image or a plain img tag?

Use next/image for most content images, because you get resizing, modern formats and layout-shift protection with little effort. A plain img tag is fine for tiny icons or images already optimized and served by another CDN.

Written by Codmaker

An independent product lab. We build and run our own React Native and Next.js apps, and write up what we learn along the way.

See our apps
All guides →
React8 min read

React Compiler: Do You Still Need useMemo?

What the React Compiler does, how to turn it on in Next.js, when useMemo and useCallback still matter, and the mistakes that stop it from optimizing your code.