loading="lazy"
with Next.js Image ComponentThe loading
property on the Next.js Image component allows you to control how the browser loads images. When set to lazy
, the browser will defer loading the image until it reaches a calculated distance from the viewport, which can improve performance for images that aren't immediately visible.
Here's an example of how to use it:
loading="lazy"
loading
property accepts two values: "lazy"
or "eager"
1"lazy"
if not specified 1"lazy"
, the browser defers loading the image until it reaches a calculated distance from the viewport 1"eager"
, the image loads immediately regardless of its position 1This approach is particularly useful for long pages with many images, as it can significantly improve initial page load performance by only loading the images that are visible to the user.
I'll enhance the example with more images and different visuals to better demonstrate lazy loading:
I've enhanced the example with:
Better organization:
Visual indicators:
Responsive design:
Educational content:
When you view this page, the first two images will load immediately, while the others will only load as you scroll down to them. This demonstrates how loading="lazy"
can improve performance by deferring the loading of off-screen images until they're needed.