Learn how to reduce bandwidth and processing costs when using different layouts.
This example shows how to reduce bandwidth and processing costs when using different layouts.
Using layout=fill
in next/image
is one of the most common patterns as it let us use responsive parents and (along with the objectFit
prop) our images will resize to it perfectly. But this leads to a common problem; as we don't know how large our parent might be, we can't serve an optimized image.
Our card is represented by this code:
<Card><Image layout="fill" src="..." /></Card>
But wait, given this code we end up with this π
Our element has a width of 256px but we are serving a 1000px image!
The sizes
prop provides information about how wide the image will be at different breakpoints when using layout="responsive"
or layout="fill"
. In this case our card limits the width of the card to a maximum of 256px
. So if we update our code to use sizes like this:
<Card><Image layout="fill" sizes="256px" src="..." /></Card>
Now we are being served with an optimized image.
We also have a lot of images available for different viewport sizes that will be generated (and cached) on demand just when needed. By default, a variant will be available for every device size
configured. But we can also specify image sizes
that will be concatenated to the variants generated by device sizes when using layout="responsive"
or layout="fill"
.
This layouts force us to define a width and height of the image so its easier to determine the size of the variants that has to be generated so we don't have to define any extra properties to get an optimized image. So, given this code:
<Card><Image src="..." width={256} height={256} /></Card>
"intrinsic" is the default layout We get back a correct optimized image
https://solutions-reduce-image-bandwidth-usage.vercel.sh
You can choose from one of the following two methods to use this repository:
Deploy the example using Vercel:
Execute create-next-app
with pnpm to bootstrap the example:
pnpm create next-app --example https://github.com/vercel/examples/tree/main/solutions/reduce-image-bandwidth-usage reduce-image-bandwidth-usage
Next, run Next.js in development mode:
pnpm dev
Deploy it to the cloud with Vercel (Documentation).
Learn how to reduce bandwidth and processing costs when using different layouts.
This example shows how to reduce bandwidth and processing costs when using different layouts.
Using layout=fill
in next/image
is one of the most common patterns as it let us use responsive parents and (along with the objectFit
prop) our images will resize to it perfectly. But this leads to a common problem; as we don't know how large our parent might be, we can't serve an optimized image.
Our card is represented by this code:
<Card><Image layout="fill" src="..." /></Card>
But wait, given this code we end up with this π
Our element has a width of 256px but we are serving a 1000px image!
The sizes
prop provides information about how wide the image will be at different breakpoints when using layout="responsive"
or layout="fill"
. In this case our card limits the width of the card to a maximum of 256px
. So if we update our code to use sizes like this:
<Card><Image layout="fill" sizes="256px" src="..." /></Card>
Now we are being served with an optimized image.
We also have a lot of images available for different viewport sizes that will be generated (and cached) on demand just when needed. By default, a variant will be available for every device size
configured. But we can also specify image sizes
that will be concatenated to the variants generated by device sizes when using layout="responsive"
or layout="fill"
.
This layouts force us to define a width and height of the image so its easier to determine the size of the variants that has to be generated so we don't have to define any extra properties to get an optimized image. So, given this code:
<Card><Image src="..." width={256} height={256} /></Card>
"intrinsic" is the default layout We get back a correct optimized image
https://solutions-reduce-image-bandwidth-usage.vercel.sh
You can choose from one of the following two methods to use this repository:
Deploy the example using Vercel:
Execute create-next-app
with pnpm to bootstrap the example:
pnpm create next-app --example https://github.com/vercel/examples/tree/main/solutions/reduce-image-bandwidth-usage reduce-image-bandwidth-usage
Next, run Next.js in development mode:
pnpm dev
Deploy it to the cloud with Vercel (Documentation).