Next.js Image Optimization: A Complete Guide to next/image
How next/image resizes, compresses, and lazy loads images, how to set sizes and preload correctly, and how to allow remote image hosts in Next.js 16.
By UndieGravity, , 2 min read
Images are usually the heaviest part of a page. The next/image component fixes most problems automatically: it serves modern formats, resizes to the screen, lazy loads below the fold, and reserves space so the layout does not jump. You only need to give it the right information.
The basic usage
import Image from "next/image";
import hero from "./hero.jpg";
export function Hero() {
return <Image src={hero} alt="Team working at a desk" preload />;
}When you import a local file, Next.js reads its width and height at build time, so you do not need to set them. For remote URLs you must pass width and height, or use fill inside a positioned parent.
Always set sizes with fill or responsive layouts
sizes tells the browser how wide the image will be at each screen size, so it can pick the smallest file that still looks sharp. Without it, a card image in a three-column grid may download at full screen width.
<div className="relative aspect-video">
<Image
src={post.image}
alt=""
fill
sizes="(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw"
className="object-cover"
/>
</div>Preload only the main image
In Next.js 16 the priority prop is deprecated; use preload instead. Put it on the single image that is the Largest Contentful Paint, usually the hero. Preloading many images makes them compete for bandwidth and slows the one that matters.
Allow remote hosts
For images from a CDN or CMS, list the host in next.config.ts. The older images.domains option is deprecated; use remotePatterns.
const nextConfig = {
images: {
remotePatterns: [{ protocol: "https", hostname: "res.cloudinary.com", pathname: "/**" }],
},
};
export default nextConfig;Alt text that helps
- Describe what the image shows when it carries meaning: "Dashboard with a sales chart".
- Use an empty
alt=""for decorative images, so screen readers skip them. - Do not stuff keywords into alt text; describe the image honestly.
Common mistakes
- Using a plain
<img>for large photos and losing resizing and lazy loading. - Uploading a 4000 pixel photo for a 300 pixel avatar. Resize the source too; it saves build and cache time.
- Forgetting a positioned parent with
fill, which makes the image cover the whole page.
Static HTML sites can get similar results without Next.js: use loading="lazy", explicit width and height attributes, and WebP files.