Skip to content
Styling & OptimizationChapter 26 / 34Beginner5 min read

Image Optimization

Use the Next.js image component (next/image) to resize, lazy load and serve images in modern formats, including the new Next.js 16 config defaults.

On this page

1. What is image optimization?

You provide one large 3000px JPG, and Next.js sends a smaller WebP or AVIF version to a mobile user. The built-in Next.js image optimizer does this work.

2. Why does it matter?

Images are often the heaviest part of a page. A wrongly sized image slows the page down, and an image without width / height makes the layout jump.

Plain img tagnext/image
Every device gets the same large fileA resized version for each device
Old JPG or PNG formatWebP or AVIF where the browser supports it
All images download immediatelyImages below the fold are lazy loaded
Layout shift (CLS) on loadSpace is reserved up front using width and height
No caching controlOptimized images are cached

3. Where is it used?

PlaceExample
Hero bannerThe biggest image on the page, the LCP element
Product cardsMany images in an e-commerce grid
Blog thumbnailsRemote images coming from a CMS
AvatarsSmall fixed-size profile pictures
Background coverAn image that covers its parent using fill

4. How does it work?

The journey of an image request
The Image component generates srcset and sizes
The browser picks one URL to match the screen
The request goes to /_next/image
The optimizer resizes it and creates WebP or AVIF
The result is cached and delivered to the browser

When you import a local image, Next.js reads its width, height and blur placeholder at build time. For a remote image you have to provide these values yourself, and you must allow the host in next.config.ts.

5. Code example

Local image (static import)

tsx
import Image from "next/image";
import hero from "./hero.jpg";

export default function Home() {
  return (
    <Image
      src={hero}
      alt="A night view of Lahore"
      placeholder="blur"
      preload
    />
  );
}

Remote image and responsive fill

tsx
import Image from "next/image";

export function ProductCard({ url, name }: { url: string; name: string }) {
  return (
    <div className="relative aspect-square">
      <Image
        src={url}
        alt={name}
        fill
        sizes="(max-width: 768px) 100vw, 33vw"
        className="object-cover"
      />
    </div>
  );
}

Images config in next.config.ts

ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  images: {
    remotePatterns: [new URL("https://cdn.example.com/products/**")],
    localPatterns: [{ pathname: "/assets/**", search: "?v=2" }],
    qualities: [60, 75],
    minimumCacheTTL: 14400,
  },
};

export default nextConfig;

How does the code work?

ItemMeaning
src={hero}Static import, size and blur data are read at build time
preloadPreloads the LCP image in the head (replaces priority in Next 16)
fillThe image fills its parent, and the parent needs position: relative
sizesTells the browser how wide the image will be on screen
remotePatternsOnly remote images matching these URLs will be optimized
localPatternsAllows paths and query strings for local images
qualitiesThe list of allowed quality values

What changed in Next.js 16?

SettingNext.js 16 behavior
qualitiesThe default is now [75], and any other quality value is rounded to the nearest allowed value
minimumCacheTTLThe default is 4 hours (14400 seconds)
Local image + query stringAn image like /logo.png?v=2 needs images.localPatterns
priority propDeprecated, use preload or loading="eager"
images.domainsDeprecated, use remotePatterns

6. Common mistakes

Mistake: Not adding the remote image host to the config

Why? Without remotePatterns, Next.js refuses to optimize the remote URL and throws an error.

Better: In next.config.ts, write the exact protocol, hostname and pathname inside images.remotePatterns.

Mistake: Leaving out sizes when using fill

Why? Without sizes, the browser assumes the image is as wide as the whole screen and downloads a file that is larger than needed.

Better: Give sizes that match your layout, for example 100vw on mobile and 33vw on desktop.

Mistake: Adding preload to every image

Why? If every image is preloaded, the real LCP image gets no priority and bandwidth is wasted.

Better: Keep preload only on the above-the-fold hero or LCP image, and leave the rest on default lazy loading.

Mistake: Leaving alt text empty or useless

Why? Screen reader users cannot tell what the image means, and image SEO becomes weaker too.

Better: Briefly describe what the image shows, and keep alt empty only for purely decorative images.

Interview tip

For the question "How does next/image help reduce CLS?", the answer is: with width and height (or a static import) the browser knows the aspect ratio in advance, so space is reserved before the image loads and the content does not move.

7. What to learn before and after

Final quick revision

PointDetail
Componentimport Image from "next/image"
Required propssrc, alt, and width / height or fill
LCP imagepreload prop
Remote imagesimages.remotePatterns
Next 16 defaultsqualities: [75], minimumCacheTTL of 4 hours