Lazy load images with Next.js (video tutorial)
Last updated: Feb-17-2025
On this page:
Overview
Improve performance in your Next.js app by lazy loading images using the Cloudinary Next.js SDK.
Video tutorial
Tutorial contents
This tutorial presents the following topics. Click a timestamp to jump to that part of the video.
Introduction
Lazy loading minimizes loading times, helping retain visitors on your website. | |
How lazy loading works
Lazy loading is a built-in attribute of the HTML img and iframe tags. Setting the loading attribute to "lazy" prevents the browser from loading resources until the user scrolls to them or interacts in some way. |
|
Preventing layout shifts
To avoid layout shifts when images load, it's best to define the width and height of images either through CSS or inline attributes. Additionally, only images that don't contribute to the Largest Contentful Paint (LCP)—such as banner and hero images—should be lazy loaded. | |
Lazy loading with Cloudinary’s CldImage
component
With Cloudinary’s CldImage component, lazy loading is enabled by default. This means images are only requested when needed, saving bandwidth and ensuring that unnecessary calls aren’t made. If an image is never seen by the user, it’s never loaded. |
|
When to use eager loading
For LCP images, such as prominent header images, you can override lazy loading by adding the priority attribute to the CldImage component. This tells the browser to load the image immediately, just like the priority attribute in Next.js’s Image component. However, using this attribute incorrectly can impact performance, so it should only be applied to critical LCP images. |
|
Keep learning
Related topics
- Watch more Dev Hints videos on the Cloudinary YouTube channel.
- Try out the community-developed Next Cloudinary library that provides components to enable you to upload and apply popular Cloudinary transformation and optimization features to your media when building Next.js applications.
If you like this, you might also like...
Check out the Cloudinary Academy for free self-paced Cloudinary courses on a variety of developer or DAM topics, or register for formal instructor-led courses, either virtual or on-site.
✖️