TUTORIAL5 MIN READ

Optimize Images in Next.js

Published

To optimize images in Next.js, use the built-in Image component so width, height, and format selection are set deliberately rather than left to defaults. The component can reserve layout space, serve responsive candidates through sizes, and prioritize the image that matters most for Largest Contentful Paint. None of this happens automatically for arbitrary markup; each prop needs to match your actual layout and breakpoints.

How to optimize images in Next.js with the Image component

The Image component differs from a plain img tag because Next.js runs an optimization step that resizes the source, selects a modern format such as WebP or AVIF when the browser's request allows it, and reserves space on the page using the width and height props before the file loads. Next.js's own documentation states that these props are what let the component reserve layout space, which is also why removing them removes that protection.

The same documentation notes that modern formats such as WebP and AVIF tend to produce smaller files than JPEG or PNG at a comparable visual quality, which is part of why the optimizer prefers a modern format automatically when the browser supports it.

How to prevent layout shift with proper image sizing

Width and height tell Next.js the image's intrinsic aspect ratio, not the exact pixel size it renders at. The component uses that ratio to reserve a box in the layout before the file arrives, and that reserved box is what actually prevents a layout shift. Shipping a smaller file does not do this by itself; the browser still needs a reserved box, or surrounding content can still jump when the image loads.

Illustrative example: a blog post hero declared as width={1200} height={630} reserves a 1200-by-630 box that CSS can then scale down, even though the rendered width on a phone might be 360px. If a layout shift still shows up, check whether a later CSS rule overrides the aspect ratio or sets an unrelated height on the wrapping element.

When using fill instead of width and height, give the parent element an explicit size and position:relative, because fill stretches to match the parent's box rather than defining its own dimensions.

When should you use the sizes prop for responsive images?

Without a sizes value, Next.js's documentation notes that the browser assumes the image spans the full viewport width, which can mean downloading a candidate larger than what the layout actually shows. The sizes attribute tells the browser how much of the viewport the image will occupy at each breakpoint, so it can pick a matching file from srcset before the layout is finished rendering.

Browsers choose a file from srcset using the sizes value together with the device's pixel density, according to web.dev's guide on image performance. Two phones with the same CSS width but different pixel ratios can legitimately request different files from identical markup.

Illustrative example: a product photo that fills half of a two-column desktop layout but the full width on mobile could use sizes equal to (min-width: 768px) 50vw, 100vw. Check the Network panel at both breakpoints to confirm a differently sized file is actually requested, not just a differently displayed copy of the same file.

How to choose between loading, fetchPriority, and preload

Images rendered through the Image component are lazy-loaded by default, deferring their fetch until they approach the viewport. That default suits secondary images but is wrong for whichever image is the page's Largest Contentful Paint candidate: it should not be lazy-loaded. Next.js's documentation describes setting priority on that one image so the browser fetches it earlier, similar to a preload hint. Newer Next.js releases rename this prop to preload, so check the documentation for the version you run.

PropWhat it doesWhen to use it
loadingControls native lazy or eager fetch timingLeave the default for images below the fold
priority (Image component)Marks one image as high priority, similar to a preload hintUse on the single image most likely to be the LCP element
fetchPriorityA direct browser hint for relative fetch priorityUse on plain img or picture elements outside the Image component

A priority or fetchPriority hint changes fetch ordering; it does not guarantee that file finishes downloading first, and it does not fix a slow server response for that specific request.

How to verify your image delivery in the browser

Open the browser's Network panel, reload the page, and find the image request. Check the Type column for the format actually delivered, such as webp, avif, or jpeg, and the Size column for the transferred bytes; the optimizer picks the format based on what the request's Accept header allows, not what the source file originally was. Resize the viewport, or use device emulation, to confirm a differently sized file loads at each breakpoint configured through sizes.

  • →Confirm the Network panel shows a modern format when the browser's Accept header allows it.
  • →Confirm width and height, or fill plus a sized parent, match the final rendered box.
  • →Confirm sizes produces a different file size at mobile and desktop breakpoints.
  • →Confirm the LCP image is not marked loading equal to lazy.

Remote images need their domain listed in Next.js's remotePatterns configuration before the optimizer processes them; a narrowly scoped pattern is safer than a broad wildcard. Optimization behavior for private or authenticated sources can differ from public ones, so test those paths separately instead of assuming the same pipeline applies everywhere. None of this pipeline exists in a plain React app without a framework: there, picture, srcset, and sizes only choose among files you have already generated, they do not create new ones.

Next step: open your largest above-the-fold image in the Network panel today, confirm its format, size, and the width, height, or sizes values actually match your layout, and adjust whichever prop the check reveals is wrong.

Ready to optimize your images?

TRY ZIPMYIMAGES FREE →

Related guides