How to optimize images with the Next.js Image Component

Published on August 8, 2024

How to optimize images with the Next.js Image Component

The Next.js Image Component allows you to optimize the images on your website for faster page load times and a better user experience. It optimizes images by resizing, compressing, and converting them to modern formats like AVIF and WebP, ensuring the file sizes are reduced. 

That's not the only trick up the Next.js Image Component's sleeve. It can automate the display of responsive images for different screen sizes, and allows for images to be lazy loaded, meaning any slow-loading content won't affect initial page load times (improving SEO). 

This post explains what the Next.js Image Component is and how to use it to optimize your web applications to be faster and more responsive.

What is the Next.js Image Component and why should you use it?

The Next.js <Image> Component extends the functionality of the standard HTML <img> element — and compiles to a standard HTML <img> element when you compile your Next.js app, so it's compatible with all of your users' devices. 

Its advantages over plain HTML image elements include:

  • Responsiveness: The Next.js Image Component helps you efficiently manage and deliver responsive images. It automatically creates multiple copies of each image at multiple sizes and delivers the most appropriate one depending on the user's screen size.

  • Faster page load times: The Next.js Image Component can automate the handling of responsive images, so your users with smaller viewports don’t need to waste time waiting for large images to be served. Also, its ability to lazy load images means that images only load when entering the viewport, which will give you faster initial page load times.

  • Improved user experience: Aside from faster page load times, which are a key part of UX, the Next.js Image Component helps to prevent cumulative layout shifts. Elements that constantly shift around on the page lead to decreased readability, cause users to make accidental errors (when buttons shift around the page it's easy to click the wrong one), and make your site a nightmare to navigate for users who rely on screen readers.

  • Improved SEO: Google favors fast page load times and low Cumulative Layout Shift scores, so you'll get a boost to your SEO rankings.

  • Scalability: This responsive image process can be scaled across many devices without compromising quality, and as the process is automated, it can scale to large sizes without increasing the risk of manual error.

  • Reduced admin and code complexity: When working with responsive images, it's tedious to have to create multiple versions of the same image in multiple sizes, and then define the srcset, which explains which image should be served for different screen widths. The Next.js Image Component does all of this for you, auto-generating differently sized images for you and adding them to the srcset property of the HTML img element.

How to use the Next.js Image Component

The Next.js Image Component can be used to display a single image (with no variants), or responsive images with different variations optimized for different screen sizes. 

Displaying an image using the Next.js Image Component 

Assuming your image is stored in the public directory of your Next.js app, you should use its relative path in the component’s src property:

Note the use of the <Image> tag to use the Next.js Image Component, instead of the default HTML <img> tag. 

If you're displaying an image stored remotely, you need to provide the full path to the image instead:

<Image src="https://example.com/path/to/trees.jpg" alt="trees" />

You'll also need to add any remote domains that host your images to your next.config.js, otherwise your app will not be able to access them, resulting in a Bad Request error.

Displaying responsive images for different screen sizes  

You can use the fill property to ensure your Next.js Image Component fills the parent container, but using fill alone will cause your image to stretch, and does not preserve the original aspect ratio:

The image appears stretched, as no aspect ratio is set.

To make your image fully responsive, you need to set position: relative on its parent element, as well as either a width and height or an aspectRatio:

Thus, the aspect ratio of the image is preserved using the aspectRatio property of the Next.js Image Component.

The aspect ratio of the image is preserved using the aspectRatio property of the Next.js Image Component.

There are many different properties available to customize your images on the Next.js Image Component, but we cover the basics to get up and running here.

How to use the Next.js Image Component to optimize your images

There are several optimization techniques you can use to make your web pages more responsive and load faster for your users.

Serve smaller images to mobile devices

The Next.js Image Component serves smaller images to mobile devices by creating multiple sizes for the same image and serving the most appropriately sized image for the user's device. Using the fill property is one way it achieves this, but only works if you want an image to fill the parent container. The browser will always assume that any image will be the full width of the viewport, so if this is not the case, you need to inform it by using the sizes property.

You can use the sizes property to tell the browser what percentage of the viewport's width the image should fit. You can set different values for this at different breakpoints for different screen sizes. A common example of when to do this is when you want to display images at full width on mobile screens, in a two-column layout on tablets, and in a three-column layout on desktops. In this case, you could set the sizes property as follows:

sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"

Automatically compress your images 

You can specify what percentage to compress images by using the quality property. Seventy-five percent is the default quality setting, however, depending on the complexity of the image, you may be able to drop it even lower without any noticeable effect on its quality.

quality={50}

Convert images to modern formats like WebP and AVIF

You can specify your preferred formats in your next.config.js file, and your images will be served in that format. Modern formats like WebP and AVIF allow you to compress your file to smaller sizes without losing image quality.

Other useful features of Next.js Image

The Next.js Image Component doesn't just help you optimize your images and make them responsive; it also adds interactivity that improves the user experience:

Lazy loading

The Next.js Image Component improves initial page load times by only loading images once they come into the viewport. This comes as standard and is part of its default behavior. If you want to turn this off for a particular image, you can do so by setting loading="eager".

Preload images above the fold

Set the priority property on critical images. This is for critical images needed at initial page load, like a hero image. This is a convenient way to preload an image, without having to use the <link rel="preload"> HTML tag.

Placeholder images

You can display a blurred lower-quality placeholder image while your higher-resolution images are loading with the placeholder and blurredDataURL properties. This improves the UX of your site, as your user perceives that the page is loading faster.

Built-in caching

Images are cached automatically by Next.js in <distDir>/cache/images — you don't need to do anything to make this happen.

Speed up your site with the Next.js Image Component and Contentful

The Next.js Image Component offers many advantages over standard HTML image elements, including improved page load times, an enhanced user experience through reduced cumulative layout shifts and responsive images, and the ability to scale your responsive images across different devices and screen sizes.

To further enhance these benefits, you can pair the Next.js Image Component with Contentful — a composable content platform with a headless CMS, that also helps improve page load times, the user experience of your site, and the ability to scale your content. Contentful can resize and deliver your images and content from its high-speed CDN, ensuring mobile users get the best experience possible, and making sure that your apps, online shops, blogs, and customer portals are always available and fast loading.

Sign up for Contentful today and follow our guide on how to integrate Contentful with Next.js to see how well the two work together. If it's a full tutorial you need, we also teach how to build a blog with Next.js, Tailwind, and Contentful.

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Related articles

An illustration of a shopping basket filled with abstract shapes representing how microservices architecture unites content and ecommerce
Guides

Create a composable commerce site with Contentful and Medusa

July 7, 2022

This guide will show you how to create a Next.js application using App Router to fetch knowledge base articles from Contentful via React Server Components.
Guides

How to Integrate Contentful and Next.js App Router

December 13, 2023

How to use Redux to manage application state and data-fetching logic in a React project, and how to apply these techniques to data coming from Contentful.
Guides

Redux tutorial with Contentful

April 7, 2023

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started