Next.js Curriculum
MediumAsset Optimization100% Free30 min read

Next.js Image Optimization (next/image) Internals

The 30-Second Interview Answer

The next/image component automatically optimizes images on-demand: converting formats to modern WebP/AVIF, resizing based on device viewport sizes, preventing Cumulative Layout Shift (CLS) via intrinsic aspect ratios, and deferring off-screen images using native lazy loading.

Learning Objectives

Explain how next/image resizes, compresses, and transcodes images on-demand.

Configure the sizes attribute accurately to prevent downloading oversized desktop images on mobile.

Prevent Cumulative Layout Shift (CLS) using width/height and fill layouts.

Use the priority attribute on above-the-fold hero images to optimize Largest Contentful Paint (LCP).

Prerequisites & Background Knowledge
Core Web Vitals (LCP, CLS)Responsive image markup (srcset, sizes)Image formats (AVIF, WebP)

Why Interviewers Care

Standard FAANG frontend performance screening topic. Images typically account for >60% of total webpage payload weight.

The Challenge

Explain how the Next.js image optimization pipeline works under the hood. Implement a responsive hero banner that avoids CLS and optimizes Largest Contentful Paint (LCP) using sizes and priority.

Code Architecture & Implementation

1// app/components/HeroBanner.tsx
2import Image from 'next/image';
3
4export function HeroBanner() {
5 return (
6 <div className="relative w-full h-[500px] overflow-hidden rounded-xl">
7 {/*
8 CRITICAL FOR LCP & CLS:
9 1. priority: Preloads image immediately, bypassing lazy-loading!
10 2. fill: Fills the relative container without manual pixel dimensions.
11 3. sizes: Informs the browser what image width to download across breakpoints.
12 */}
13 <Image
14 src="https://images.unsplash.com/photo-1579546929518-9e396f3cc809"
15 alt="Modern engineering architecture"
16 fill
17 priority
18 sizes="(max-width: 768px) 100vw, (max-width: 1200px) 80vw, 1200px"
19 className="object-cover"
20 quality={80} // 80 provides optimal balance of visual fidelity and byte size
21 />
22 <div className="absolute inset-0 bg-black/40 flex items-center p-8">
23 <h1 className="text-white text-4xl font-extrabold">Scalable Next.js Architecture</h1>
24 </div>
25 </div>
26 );
27}

Architectural Explanation

When <Image> is rendered, Next.js generates an HTML <img> tag with a dynamically calculated srcset pointing to /_next/image?url=...&w=...&q=80. When requested, Next.js uses the high-performance sharp library to resize the image to the exact width needed and transcodes it to modern formats like AVIF or WebP. The resulting asset is cached at the edge CDN. The sizes attribute tells the browser before CSS is parsed how wide the image will render, preventing it from downloading high-res desktop images on mobile phones.

Common Mistakes & Anti-Patterns

Omitting `sizes` when using `fill`, causing mobile devices to download full desktop 4K images.

Using `priority` on 20 images on a single page, bottlenecking the network pipeline.

Using unoptimized `<img>` tags for external images, causing massive layout shifts and huge payload downloads.

Interview Follow-Up Traps

The interviewer asks: "What happens if sharp is not installed in a self-hosted Docker container?" Next.js falls back to Squoosh (WebAssembly), which is significantly slower and can bottleneck production CPU.

Frequently Asked Questions

Why does AVIF take longer to generate than WebP?

AVIF compression is computationally intensive, but yields 20-30% smaller file sizes than WebP. Subsequent requests hit CDN cache.

No. SVGs are vector graphics; Next.js serves them as-is without resizing or format transcoding.

Import them directly: `import hero from "./hero.png"`. Next.js automatically calculates intrinsic width, height, and blurDataURL.