description |
---|
Next.js supports built-in image optimization as well as 3rd party loaders for Imgix, Cloudinary, and more! Learn more here. |
Examples
Since version 10.0.0, Next.js provides an Image component that will automatically optimize the source image based on the browser. The optimization includes resizing the image based on the browser's viewport as well as selecting the best format, such as WebP, based on the browser's Accept
header.
The objective of the Image component is to improve performance of Next.js applications, with a particular focus on improving their Core Web Vitals scores.
To add an image to your application, import the Image component on your page.
import Image from 'next/image'
function Home() {
return (
<>
<h1>My Homepage</h1>
<Image src="/me.png" width={200} height={200} />
<p>Welcome to my homepage!</p>
</>
)
}
export default Home
If you want to configure Image Optimization you can do so by using images
in next.config.js
.
You can specify which size widths you wish to allow using the sizes
property. Since images will maintain aspect ratio using the width
and height
of the source image, there is no need to specify height, only the width. You can think of these as breakpoints.
module.exports = {
images: {
sizes: [320, 420, 768, 1024, 1200],
},
}
If you want to optimize images hosted on an external website, you would use an absolute url for the Image src
and specify which
domains
are allowed to be optimized.
module.exports = {
images: {
domains: ['example.com'],
},
}
If you want to use a cloud provider to optimize images instead of using the Next.js API, you can configure the loader and path prefix. This allows you to use relative urls for the Image src
and automatically generate the correct absolute url for your provider.
module.exports = {
images: {
loader: 'imgix',
path: 'https://example.com/myaccount/',
},
}
The following image optimization cloud providers are supported:
- Imgix:
loader: 'imgix'
- Cloudinary:
loader: 'cloudinary'
- Akamai:
loader: 'akamai'
- Vercel: no configuration necesary
For more information on what to do next, we recommend the following sections: