Skip to the solution
MediumFrontend5 min read

How does next/image optimize images in Next.js?

780 views

The problem

Question presented to candidate: "Explain what the Next.js Image component does for you compared with a plain img tag, and how you would make a hero image load fast."

What a strong answer should cover:

  • It resizes and converts images on demand through /_next/image, choosing WebP or AVIF from the browser Accept header, and caches the results.
  • It generates srcset and uses sizes so the browser downloads only the width it needs.
  • It lazy-loads by default and reserves space with width and height (or the imported file size), preventing layout shift.
  • For the LCP image use preload (Next.js 16 deprecates priority), loading="eager" or fetchPriority="high".
  • Remote images must be allowed in images.remotePatterns; qualities are limited to images.qualities (only 75 by default in 16).

Clarifying questions expected:

  • "Which image is the largest thing above the fold?" β€” that one gets preload.
  • "How wide is the image on each screen size?" β€” that determines sizes.

Code / implementation expected: Yes β€” a lazy responsive image and a preloaded hero.

  • nextjs
  • next-image
  • optimization
  • performance

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Read the code

A responsive lazy image and a preloaded hero
// next.config.ts
const nextConfig = {
  images: {
    remotePatterns: [{ protocol: "https", hostname: "cdn.example.com", pathname: "/products/**" }],
    formats: ["image/avif", "image/webp"],
  },
};
export default nextConfig;

// app/page.tsx
import Image from "next/image";
import hero from "@/public/hero.jpg";

export default function Home() {
  return (
    <>
      <Image src={hero} alt="Summer collection" preload placeholder="blur" sizes="100vw" />
      <Image
        src="https://cdn.example.com/products/shoe.jpg"
        alt="Trail shoe"
        width={800}
        height={600}
        sizes="(max-width: 768px) 100vw, 33vw"
      />
    </>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 50 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextMedium

How do you handle file uploads in Next.js with Server Actions and Route Handlers?

Continue