JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
PrevNext

Learn the concept

Image & Font Optimization

nextjs
junior
optimization

What is next/image and why should you use it over the regular img tag?

nextjs
image
optimization
performance
lazy-loading
webp
preload
Quick Answer

The `next/image` component automatically optimizes images with lazy loading, responsive sizing, modern formats (WebP/AVIF), and prevents Cumulative Layout Shift (CLS), improving both performance and Core Web Vitals.

Detailed Explanation

Benefits of next/image:

  1. Automatic Optimization

    • Converts to modern formats (WebP, AVIF)
    • Resizes based on device
    • Compresses images
  2. Lazy Loading

    • Images load only when entering viewport
    • Reduces initial page load time
  3. Prevents Layout Shift

    • Reserves space for images before they load
    • Improves CLS (Core Web Vital)
  4. Responsive Images

    • Automatically serves correct size
    • sizes prop for responsive behavior
  5. Blur Placeholder

    • Shows blurred version while loading
    • Better user experience

Required Props:

  • src - Image source (local or remote)
  • alt - Accessibility text
  • width & height - Required for remote images (or use fill)

Next.js 16 Changes:

  • The priority prop is deprecated — use preload instead to eagerly load above-the-fold images
  • The domains config is deprecated since v14 — use remotePatterns for remote images
  • The qualities config is required in next.config.js (default [75])

Code Examples

Basic Image usageTSX
import Image from 'next/image';

// Local image (auto width/height detection)
import profilePic from '../public/profile.jpg';

export default function Profile() {
  return (
    <div>
      {/* Local image - width/height inferred */}
      <Image
        src={profilePic}
        alt="Profile picture"
        placeholder="blur" // Built-in blur for local images
      />
      
      {/* Remote image - must specify dimensions */}
      <Image
        src="https://example.com/photo.jpg"
        alt="Remote photo"
        width={500}
        height={300}
      />
    </div>
  );
}

Real-World Applications

Use Cases

Image Gallery

Build a photo gallery with lazy-loaded thumbnails that only fetch full-resolution images when scrolled into view, using `sizes` for responsive grid layouts and `placeholder='blur'` for smooth loading.

E-Commerce Product Images

Display product images with automatic WebP/AVIF conversion for faster loads, `fill` mode for consistent aspect ratios across product cards, and `preload` for hero product images.

User Avatars

Render user avatars across the app with consistent sizing, automatic optimization, and `remotePatterns` configured for your CDN or cloud storage provider.

Mini Projects

Responsive Image Gallery

beginner

Build a Masonry-style image gallery using next/image with blur placeholders, responsive sizes, fill mode, and lazy loading. Include a lightbox that uses `preload` for the selected image.

Image Performance Audit Tool

intermediate

Create a page that displays the same images using regular `<img>` tags and `next/image` side by side, with a performance comparison showing load times, file sizes, and Core Web Vitals impact.

Industry Examples

Unsplash

Unsplash serves millions of optimized images, using techniques similar to next/image — responsive srcsets, modern formats (WebP/AVIF), and blur-up placeholders for progressive loading.

Vercel

Vercel's showcase and template gallery uses next/image with automatic optimization to serve project screenshots at optimal quality across devices.

Resources

Next.js Image Component

docs

Image Optimization

docs

Related Questions

What performance optimization techniques does Next.js provide?

senior
performance

What is Next.js and how does it differ from React?

junior
basics
Previous
How do you navigate between pages in Next.js?
Next
What's the difference between SSR and SSG in Next.js?
PrevNext