Eager Image Loading

Eager image loading tells the browser to fetch an image immediately rather than intentionally deferring it. It is most useful for important above-the-fold images when postponing the request could hurt perceived or measured loading performance.

Browsers commonly defer off-screen images so network and decoding work can be reserved for content the user is likely to see first. For an important image that is already visible or is needed immediately, explicitly requesting eager loading can avoid unnecessary delay. In HTML, this is commonly expressed with `loading="eager"`.

Eager loading is especially relevant to prominent images such as a hero image or another visually important above-the-fold asset. The decision should be based on the image's role in the initial viewport, not simply on its file size or whether it appears near the top of the DOM.

Eager loading does not mean the browser must download the image before every other resource. Browser prioritization, responsive-image selection, network conditions, and other scheduling rules still affect when bytes arrive. If an image is critical to the initial render, complementary techniques such as `fetchpriority="high"`, appropriate responsive sources, compression, and efficient formats may also matter.

Using eager loading indiscriminately can increase network contention. Images that are below the fold or unlikely to be needed immediately generally benefit more from deferred loading, allowing critical CSS, JavaScript, fonts, and visible content to compete less aggressively for bandwidth.

Key points

Interview questions

How would you optimize a hero image for performance?

First, I would make sure the image is appropriately sized and compressed for the displayed dimensions. I would use responsive image techniques such as `srcset` and `sizes` where appropriate, and choose an efficient image format supported by the target browsers. For a critical above-the-fold image, I would consider `loading="eager"` and potentially `fetchpriority="high"`. I would also reserve its layout space with dimensions or an appropriate aspect ratio to reduce layout shifts.

When would you use `loading="eager"` on an image?

I would use it when an image is important to the initial viewport and delaying its request could make the page appear slower. A common example is a hero image that users see immediately. I would avoid applying it globally because unnecessary eager requests can consume bandwidth and compete with other critical resources.

Does `loading="eager"` guarantee that the image loads first?

No. It means the browser should not intentionally defer the image through the lazy-loading mechanism. It does not guarantee that the request will receive the highest network priority or finish before other resources. For a particularly important image, `fetchpriority="high"` can provide an additional priority hint, although browser scheduling still controls the final behavior.

What are the risks of making all images eager?

Making all images eager can create unnecessary requests and increase bandwidth competition during the most resource-sensitive part of page loading. Off-screen images may consume network and decoding resources before they are needed, potentially delaying more important work. It can also undermine the browser's ability to prioritize resources based on what is immediately relevant to the user. The better approach is to classify images by their role in the initial experience and defer non-critical content.