Image Optimization
Master next/image — automatic WebP conversion, lazy loading, CLS prevention, responsive images, and the configuration options for production image delivery.
Last reviewed: January 2025
🔍 What Is It?
The Next.js Image component (next/image) provides:
- →Automatic format conversion — serves WebP or AVIF instead of PNG/JPEG (30–50% smaller)
- →Responsive sizing — serves the right resolution for each screen size
- →Lazy loading — images below the fold load only when near the viewport
- →CLS prevention — reserves space for the image before it loads (no layout shift)
- →Priority loading —
priorityprop for above-the-fold images (LCP optimisation) - →Blur placeholder — shows a blurred preview while the full image loads
🎯 Why Do We Need It?
Images are the #1 cause of poor Core Web Vitals scores:
- →LCP (Largest Contentful Paint) — slow hero images delay the main content
- →CLS (Cumulative Layout Shift) — images without dimensions cause layout jumps
- →Bandwidth — serving 4K images to mobile devices wastes data
next/image solves all three automatically. Using plain <img> in Next.js means manually solving problems that next/image handles for free.
🏗️ What Happens in Real Projects?
next/image in production:
- →Hero images —
priorityprop to preload,filllayout for full-width - →Product images — fixed width/height, blur placeholder
- →User avatars — small, rounded, lazy loaded
- →Blog thumbnails — responsive sizes for different grid layouts
- →CMS images — external domains configured in next.config.ts
⚙️ How It Works
Image optimisation pipeline:
- Browser requests
/_next/image?url=...&w=800&q=75 - Next.js fetches the original image (from public/ or external URL)
- Converts to WebP/AVIF, resizes to requested width
- Caches the optimised image on disk
- Serves from cache on subsequent requests
- On Vercel: served from global CDN edge
💻 Example
Image Component Patterns — All the next/image patterns you need for production.
🤔 Common Confusions
❓ When should I use width/height vs fill?
Use width and height when you know the exact dimensions (product images, avatars, thumbnails). Use fill when the image should fill its container (hero images, card thumbnails where the container controls the size). With fill, the parent must have position: relative and a defined size.
❓ What is the sizes prop and why does it matter?
The sizes prop tells the browser how wide the image will be at different viewport widths. Without it, the browser assumes the image is 100vw wide and downloads the largest version. With sizes="(max-width: 768px) 100vw, 50vw", mobile devices download a smaller image. Always set sizes for fill images.
❓ Should I use priority on all images?
No — only on the LCP element (the largest above-the-fold image). priority disables lazy loading and adds a preload link tag. Using it on many images defeats the purpose — all those images compete for bandwidth. One priority image per page is the typical pattern.
🚨 Common Production Issues
Missing alt text — next/image requires alt. Empty alt ("") is valid for decorative images. Meaningful alt text is required for content images. Next.js will warn in development but not block builds.
External images not configured — using an image from an external domain without adding it to remotePatterns in next.config.ts throws a runtime error. Always add external domains before deploying.
CLS from fill images without container height — a fill image in a container with no defined height collapses to 0px. Always set a height on the container (aspect-ratio, h-64, etc.).
✅ Best Practices
Use priority only on the LCP image — typically the hero or first above-the-fold image
Always set sizes for fill images — prevents downloading oversized images on mobile
Use blur placeholder for images that take time to load — better UX
Configure remotePatterns in next.config.ts for all external image sources
Use aspect-ratio CSS on containers for fill images to prevent CLS
🏛️ Architect's Note
Image optimisation is one of the highest-ROI performance improvements in any web app. A 30–50% reduction in image file size directly translates to faster LCP scores and lower bandwidth costs. Make next/image usage a team standard — add an ESLint rule (@next/next/no-img-element) to prevent plain <img> tags.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Next.js Technology Guide
Return to the Next.js learning hub.
Frontend Engineer Career Path
A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Next.js references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Next.js learning path
Browse the full Next.js topic collection and learning order on SplashRide.
Next.js Interview Prep
Practice Next.js interview questions and scenario-based answers.
Frontend Engineer Career Path
A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Lazy Loading
Master lazy loading in Next.js — dynamic imports, component lazy loading, third-party script deferral, and reducing initial bundle size for faster page loads.
Metadata API
Master the Next.js Metadata API — static and dynamic metadata, Open Graph, Twitter cards, JSON-LD, and the SEO patterns used in production applications.