Category: Next.js, React and TypeScript

Next.js Image fill without sizes fetches too large file

Read this guide in Polish

On the phone, the graphics take a small portion of the screen, but the browser downloads a wide image for desktop. The Image component with fill without appropriate sizes can allow the viewer to choose an unoptimal image variant.

How to identify the source of the problem

Check the physical size of the visible image in the various breakpoints and its downloaded size on the Network. Read generated srcset and sizes and check the Largest Contentful Paint report for the site.

Compare the message from the console with the result of the production compilation and the condition after the page has been refreshed.

Repair steps

Add sizes describing the actual width of the container, e.g. 100vw on mobile and part of the width on desktop. With fill set the parent position: relative and known dimension or aspect-ratio so that the layout does not collapse.

How to Test Solution

The browser selects an image variant matching the viewport without unnecessary transfer on the phone.

Security and restrictions

Do not set sizes="100vw" for a miniature covering 20% desktop width.