Next.js image optimization uses the built-in <Image> component from next/image to resize, compress, and serve images automatically. Instead of shipping oversized PNG or JPEG files, Next.js converts assets into lightweight WebP or AVIF formats per device.
A standard HTML image element often causes layout jumps while loading and downloads full desktop dimensions on mobile phones. The <Image> component reserves space ahead of time and serves responsive sizes for every screen.
<Image> for Local AssetsImport Image from next/image and provide src, alt, width, and height props. Add priority on hero banners so they load right away above the fold.
// app/profile/page.tsx
import Image from 'next/image';
export default function ProfilePage() {
return (
<section className="p-6">
<h2 className="text-2xl font-bold mb-4">Account Profile</h2>
<Image
src="/avatar-developer.jpg"
alt="Profile photo of lead engineer"
width={160}
height={160}
className="rounded-full shadow-md"
priority
/>
</section>
);
}
srcset Sizing: Generates multiple width variants automatically for mobile, tablet, and desktop screens.For security, Next.js blocks external image URLs unless you register allowed hostnames inside next.config.ts.
// next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'images.unsplash.com',
pathname: '/**',
},
],
},
};
export default nextConfig;
width and height on string src paths: Remote or public/ path strings require width and height (or fill inside a relative container) to prevent layout shift.priority to every image on the page: Reserve priority only for the main above-the-fold Largest Contentful Paint image.remotePatterns in next.config.ts: External images throw a runtime error unless their host matches an entry in remotePatterns.fill prop on <Image>?Use fill when you do not know an image's exact pixel dimensions ahead of time, and give its parent container position: relative with an explicit height or aspect ratio.
width and height props?No, when you write import heroPic from './hero.png' and pass src={heroPic}, Next.js reads the width and height automatically at build time.
next/image optimize SVG files?By default Next.js skips resizing vector SVGs because they scale cleanly; leave SVG icons unoptimized or enable dangerouslyAllowSVG carefully.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With