DEV Community

RAMO
RAMO

Posted on

Lazy loading a product grid: which images should never get loading="lazy"

Adding loading="lazy" to every <img> on a category page feels like free performance. It mostly is. The catch is the handful of images where it does the opposite of what you want, and on a store those are usually the most important ones.

Here's the short version of how I decide, image by image.

The rule of thumb

Lazy loading tells the browser: "don't fetch this until it's about to scroll into view." That's great for row 6 of a product grid. It's bad for anything the visitor sees the moment the page opens, because the browser now waits until layout is done to decide the image is "near the viewport" before it even starts the request.

So the question for each image is simply: is it visible without scrolling on a typical screen?

  • Yes: load it normally. If it's the largest thing on screen, give it a priority hint too.
  • No: lazy load it.
  • Not sure: lean towards eager for the first few items, lazy for the rest.

The images people lazy load by mistake

The hero banner. If a category page opens with a big banner, that banner is very often the Largest Contentful Paint element. Lazy loading it delays your LCP for no benefit.

The first row of product cards. On desktop the first row is usually fully visible. On mobile, the first one or two cards are. These should load eagerly.

The main product photo on a product page. This one is almost always the LCP element. It should be eager and, ideally, have fetchpriority="high".

Logos and icons in the header. They're small, but they're above the fold on every single page.

What it looks like in plain HTML

<!-- Hero / main product photo: the one thing you want first -->
<img src="/img/hero.webp" alt="..." width="1200" height="600"
     fetchpriority="high">

<!-- First visible product cards: default loading, no hint needed -->
<img src="/img/p1.webp" alt="..." width="300" height="300">
<img src="/img/p2.webp" alt="..." width="300" height="300">

<!-- Everything further down -->
<img src="/img/p9.webp" alt="..." width="300" height="300"
     loading="lazy" decoding="async">
Enter fullscreen mode Exit fullscreen mode

Two details that matter more than they look:

  1. Always set width and height (or a CSS aspect-ratio). Lazy images that arrive without reserved space push the layout around, and that shows up as layout shift.
  2. Use fetchpriority="high" on one image, maybe two. If everything is high priority, nothing is.

In a grid component, decide by index

You usually render cards in a loop, so the simplest approach is to pass the index and let the card decide:

function ProductGrid({ products }) {
  const EAGER_COUNT = 4; // roughly the first row on desktop

  return (
    <ul className="grid">
      {products.map((p, i) => (
        <li key={p.id}>
          <img
            src={p.image}
            alt={p.imageAlt}
            width={300}
            height={300}
            loading={i < EAGER_COUNT ? "eager" : "lazy"}
            decoding="async"
          />
        </li>
      ))}
    </ul>
  );
}
Enter fullscreen mode Exit fullscreen mode

EAGER_COUNT is a judgment call, not a magic number. Pick it by looking at your own layout at a couple of common screen sizes. A 4-column desktop grid and a 2-column mobile grid suggest something around the first 2 to 4 items.

If you're on Next.js

next/image lazy loads by default, which is the right default and exactly the trap above. For the hero or the main product photo, opt out:

import Image from "next/image";

<Image
  src={product.image}
  alt={product.imageAlt}
  width={800}
  height={800}
  priority   // in newer versions you may see `preload` instead
/>
Enter fullscreen mode Exit fullscreen mode

Check the docs for the version you're on, since the prop name for this has changed over time. Whatever it's called, the idea is the same: one or two images on the page get it, the rest stay lazy.

And give sizes a real value on responsive images. Without it the browser may pick a much larger source than the card actually needs, which hurts more than the lazy/eager choice ever will.

How to check you got it right

You don't need any special tooling:

  • Open DevTools, run a Lighthouse or Performance panel trace, and look at which element is reported as LCP. If it has loading="lazy" on it, that's your fix.
  • In the Network panel, filter by images and reload. The hero and first-row images should start early, not after a long gap.
  • Throttle to a slow connection and scroll. Lower rows should appear as you approach them, without the page jumping.

Background images and carousels

Two things that loading="lazy" doesn't cover:

  • CSS background images aren't lazy loaded by the attribute at all. If a hero is a background-image, the browser only discovers it after the CSS is parsed. For the main visual, a real <img> is easier to prioritise.
  • Carousels often render every slide up front. Make the first slide eager and the rest lazy, otherwise you're downloading images nobody has swiped to yet.

The one-line summary

Lazy load what's below the fold, prioritise the one image that's biggest above it, and reserve space for all of them.

I ran into all of these while working on the category pages at RAMO, a hardware store where pages like plumbing are long grids of small product photos, which is exactly where getting the first row right is noticeable.

Top comments (1)