|  | 
|  | 1 | +--- | 
|  | 2 | +description: Next.js supports built-in image optimization, as well as third party loaders for Imgix, Cloudinary, and more! Learn more here. | 
|  | 3 | +--- | 
|  | 4 | + | 
|  | 5 | +# Image Optimization | 
|  | 6 | + | 
|  | 7 | +<details open> | 
|  | 8 | +  <summary><b>Examples</b></summary> | 
|  | 9 | +  <ul> | 
|  | 10 | +    <li><a href="https://github.com/vercel/next.js/tree/canary/examples/image-component">Image Component</a></li> | 
|  | 11 | +  </ul> | 
|  | 12 | +</details> | 
|  | 13 | + | 
|  | 14 | +Since version **10.0.0** Next.js has a built-in Image Component and Automatic Image Optimization. | 
|  | 15 | + | 
|  | 16 | +The Next.js Image Component (`next/image`) is an extension of the HTML `<img>` element, evolved for the modern web. | 
|  | 17 | + | 
|  | 18 | +The Automatic Image Optimization allows for resizing, optimizing, and serving images in modern formats like [WebP](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types). This avoids shipping large images to devices with a smaller viewport. | 
|  | 19 | + | 
|  | 20 | +## Image Component | 
|  | 21 | + | 
|  | 22 | +To add an image to your application, import the `next/image` component: | 
|  | 23 | + | 
|  | 24 | +```jsx | 
|  | 25 | +import Image from 'next/image' | 
|  | 26 | + | 
|  | 27 | +function Home() { | 
|  | 28 | +  return ( | 
|  | 29 | +    <> | 
|  | 30 | +      <h1>My Homepage</h1> | 
|  | 31 | +      <Image | 
|  | 32 | +        src="/me.png" | 
|  | 33 | +        alt="Picture of the author" | 
|  | 34 | +        width={200} | 
|  | 35 | +        height={200} | 
|  | 36 | +      /> | 
|  | 37 | +      <p>Welcome to my homepage!</p> | 
|  | 38 | +    </> | 
|  | 39 | +  ) | 
|  | 40 | +} | 
|  | 41 | + | 
|  | 42 | +export default Home | 
|  | 43 | +``` | 
|  | 44 | + | 
|  | 45 | +- `width` and `height` are required to prevent [Cumulative Layout Shift](https://web.dev/cls/), a [Core Web Vital](https://web.dev/vitals/) that Google is going to [use in their search ranking](https://webmasters.googleblog.com/2020/05/evaluating-page-experience.html) | 
|  | 46 | +- `width` and `height` are automatically responsive, unlike the HTML `<img>` element | 
|  | 47 | + | 
|  | 48 | +## Configuration | 
|  | 49 | + | 
|  | 50 | +You can configure Image Optimization by using the `images` property in `next.config.js`. | 
|  | 51 | + | 
|  | 52 | +### Sizes | 
|  | 53 | + | 
|  | 54 | +You can specify a list of image widths to allow using the `sizes` property. Since images maintain their aspect ratio using the `width` and `height` attributes of the source image, there is no need to specify height in `next.config.js` – only the width. You can think of these as breakpoints. | 
|  | 55 | + | 
|  | 56 | +```js | 
|  | 57 | +module.exports = { | 
|  | 58 | +  images: { | 
|  | 59 | +    sizes: [320, 420, 768, 1024, 1200], | 
|  | 60 | +  }, | 
|  | 61 | +} | 
|  | 62 | +``` | 
|  | 63 | + | 
|  | 64 | +### Domains | 
|  | 65 | + | 
|  | 66 | +To enable Image Optimization for images hosted on an external website, use an absolute url for the Image `src` and specify which | 
|  | 67 | +`domains` are allowed to be optimized. This is needed to ensure that external urls can't be abused. | 
|  | 68 | + | 
|  | 69 | +```js | 
|  | 70 | +module.exports = { | 
|  | 71 | +  images: { | 
|  | 72 | +    domains: ['example.com'], | 
|  | 73 | +  }, | 
|  | 74 | +} | 
|  | 75 | +``` | 
|  | 76 | + | 
|  | 77 | +### Loader | 
|  | 78 | + | 
|  | 79 | +If you want to use a cloud image provider to optimize images instead of using the Next.js' built-in image optimization, 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. | 
|  | 80 | + | 
|  | 81 | +```js | 
|  | 82 | +module.exports = { | 
|  | 83 | +  images: { | 
|  | 84 | +    loader: 'imgix', | 
|  | 85 | +    path: 'https://example.com/myaccount/', | 
|  | 86 | +  }, | 
|  | 87 | +} | 
|  | 88 | +``` | 
|  | 89 | + | 
|  | 90 | +The following Image Optimization cloud providers are supported: | 
|  | 91 | + | 
|  | 92 | +- Imgix: `loader: 'imgix'` | 
|  | 93 | +- Cloudinary: `loader: 'cloudinary'` | 
|  | 94 | +- Akamai: `loader: 'akamai'` | 
|  | 95 | +- Vercel: No configuration necessary | 
|  | 96 | + | 
|  | 97 | +## Related | 
|  | 98 | + | 
|  | 99 | +For more information on what to do next, we recommend the following sections: | 
|  | 100 | + | 
|  | 101 | +<div class="card"> | 
|  | 102 | +  <a href="/docs/basic-features/built-in-css-support.md"> | 
|  | 103 | +    <b>CSS Support:</b> | 
|  | 104 | +    <small>Use the built-in CSS support to add custom styles to your app.</small> | 
|  | 105 | +  </a> | 
|  | 106 | +</div> | 
|  | 107 | + | 
|  | 108 | +<div class="card"> | 
|  | 109 | +- When using `next start` or a custom server image optimization works automatically. | 
|  | 110 | +- [Vercel](https://vercel.com): Works automatically when you deploy on Vercel | 
|  | 111 | +- [Imgix](https://www.imgix.com): `loader: 'imgix'` | 
|  | 112 | +- [Cloudinary](https://cloudinary.com): `loader: 'cloudinary'` | 
|  | 113 | +- [Akamai](https://www.akamai.com): `loader: 'akamai'` | 
0 commit comments