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 tag | next/image |
|---|---|
| Every device gets the same large file | A resized version for each device |
| Old JPG or PNG format | WebP or AVIF where the browser supports it |
| All images download immediately | Images below the fold are lazy loaded |
| Layout shift (CLS) on load | Space is reserved up front using width and height |
| No caching control | Optimized images are cached |
3. Where is it used?
| Place | Example |
|---|---|
| Hero banner | The biggest image on the page, the LCP element |
| Product cards | Many images in an e-commerce grid |
| Blog thumbnails | Remote images coming from a CMS |
| Avatars | Small fixed-size profile pictures |
| Background cover | An image that covers its parent using fill |
4. How does it work?
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)
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
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
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?
| Item | Meaning |
|---|---|
src={hero} | Static import, size and blur data are read at build time |
preload | Preloads the LCP image in the head (replaces priority in Next 16) |
fill | The image fills its parent, and the parent needs position: relative |
sizes | Tells the browser how wide the image will be on screen |
remotePatterns | Only remote images matching these URLs will be optimized |
localPatterns | Allows paths and query strings for local images |
qualities | The list of allowed quality values |
What changed in Next.js 16?
| Setting | Next.js 16 behavior |
|---|---|
qualities | The default is now [75], and any other quality value is rounded to the nearest allowed value |
minimumCacheTTL | The default is 4 hours (14400 seconds) |
| Local image + query string | An image like /logo.png?v=2 needs images.localPatterns |
priority prop | Deprecated, use preload or loading="eager" |
images.domains | Deprecated, 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
| Stage | Chapters |
|---|---|
| Before | Styling: CSS Modules & Tailwind, Server & Client Components |
| Alongside | Font Optimization, next.config & Turbopack |
| After | Performance & Core Web Vitals, Sitemap, Robots & OG Images |
Final quick revision
| Point | Detail |
|---|---|
| Component | import Image from "next/image" |
| Required props | src, alt, and width / height or fill |
| LCP image | preload prop |
| Remote images | images.remotePatterns |
| Next 16 defaults | qualities: [75], minimumCacheTTL of 4 hours |