If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
In addition to statically imported images, you can even use the next JS image component with URLs. This gives us a number of the same optimization benefits we've previously covered with static images, and additionally, it even works if the image is hosted on remote servers. The first thing to be aware of when using URLs for images is that a predefined width and height is required. This is used for image optimization and for preventing layout shifts. We already know that you can still use the standard image tag if you want it to, and indeed it does work, but you can see that the image is quite large.
00:34
Currently this image file sits at three megabytes, so let's replace this image tag with the next day's image component. We bring in image from next image and replace the IMG tag with the image component. If you run this example, you will see an error that the image is missing the required width property, and this might mislead you into thinking that only the width is required. But if we do provide that property, we will see a new error that it is missing the required height property. So in order to use the image component with the source pointing to A URL, we need to provide both the widths and the height.
01:08
With these values provided, you can see that the image renders as expected. By using the next year's image component, we are getting an automatic image optimization. You can see that the image has compressed from three megs down to 94 kilobytes. We can even optimize the image further by providing a smaller width in height value. For example, if you scale it down to 480 pixels, you can see that the image has compressed even further from 94 kilobytes down to 36 kilobytes. When you are providing the width and the height values, it is really important that you get the aspect ratio correct.
01:41
So if you provide values that do not match the aspect ratio of the original image, which in this case is a square image, there will be a layout shift as the final destination image is loaded. This can be observed in this example as a nice doggy text moves. When the browser realizes that the final image is of a different aspect ratio, it is okay to scale the image by providing values for width and height. However, you have to preserve the original aspect ratio if the width and height of the source image is not known to you. The image component also provides a fill prop, which allows you to scale the image
02:12
with the dimensions determined by its container. We've added a fill prop to our image and placed it in a containing div. The fill prop requires the parent to have a position which should either be relative, fixed, or absolute as the image will be scaled according to this parent. In order to scale the containing image, we provide a width and height on this parent element, and now even though we don't provide a width and height on the image itself, next JS is happy to render the image thanks to that fill prop. One thing to be aware of and using the fill prop is that the dimensions of the parent are not taken into account
02:46
in the image optimization. So right now the optimized image sits at 94 kilobytes, and if you scaled on the parent even further from four 80 down to two 40, you can see that the rendered image is scaled down. However, the optimized image source is still at 94 kilobytes. Additionally, by default, if you get the aspect ratio wrong in the containing parent, the child image is still going to fill out the entire space and it will result in the image getting skewed. If you don't want to skew an image placed in a container that doesn't provide a matching aspect ratio, you can scale the image instead
03:19
by using the CSS property called object fit. To better demonstrate this, we add a light background to the containing element so that we can see what happens with the extra space. A simple way to scale the image is by using object fit contain. With this property added, instead of skewing the image, the extra space provided by the parent is left empty and the image is scaled down to be contained within the parent. Right now, the parent is providing too much width, but let's modify it so that the parent provides too much height, and once more, you can see that the extra space is left empty
03:51
and the image is scaled to be contained within the parent. In addition to object fit contain, you can also use object fit cover, and now instead of being scaled down, the image will be scaled up to cover the entire parent with object fit cover. As the image is scaled up, portions of it might get TriMed. Currently the left and the right sides of the image are getting TriMed, but if we contain it within a parent that has too much width instead of too much height, then the top and the bottom are going to get TriMed. So with the fill prop, you have three options.
04:23
By the default, it'll skew the image with object fit contain, it'll scale down the image, leaving the extra space, and as shown here, will object fit cover, it will scale up the image trimming the excess content. The optimizations and features we've looked at for local URL images even work for remote URLs. But in order for next years to download and optimize images from remote servers, we need to explicitly add the remote domains to the images allow list. As an example, instead of using relative URLs, let's provide an absolute URL to an image that is currently hosted on bullion art.
04:56
If you run this application, next years will complain that the host name bullion art.com is not configured for images. We can add any remote domains for image optimization. Using unex config, we provide an images section, and one simple way to do this is to provide the valid domains into the allow list. For example, av edit bullion art.com. With this configuration update, the image download successfully. Additionally, this image is also getting optimized as indicated by the size. It is only 94 kilobytes and it is a format web,
05:28
whereas the original image is three megabytes and it is in PNG format.