How to Optimize Images for Delivery
Serve images fast: pick modern formats with fallbacks, size and density per client, lazy-load below the fold, and cache fingerprinted variants.
On this page
Introduction
Images are usually the largest bytes on a page and the easiest to over-serve. A full-resolution JPEG sent to a narrow phone is downloaded and decoded at a size the screen cannot show; a modern format the browser supports is not used; a below-the-fold image loads before the first paint and delays it. Each of these is a straightforward win, and together they often cut the image weight of a page by more than half.
This tutorial covers serving modern formats with fallbacks, generating and selecting responsive sizes and densities, lazy-loading off-screen images, and caching each variant immutably. The examples use HTML attributes and HTTP headers, and the techniques apply with or without a build pipeline.
Serve modern formats with fallbacks
AVIF and WebP compress images better than JPEG at the same quality, so a client that supports them downloads fewer bytes for the same picture. The browser advertises support in the Accept header, and a server or a CDN can respond with the best format the client accepts. For a static build, emit multiple files and use a picture element so the browser chooses, or negotiate at the origin or edge by Accept.
Always keep a fallback. Some clients still cannot decode the newest format, and a missing image is worse than a larger one. The fallback can be WebP for clients that support it and JPEG for the rest. Because the format is part of the delivery, set Vary: Accept when the response format depends on Accept, so a cache does not serve one format to a client that cannot decode it.
<picture>
<source srcset="/hero.avif" type="image/avif">
<source srcset="/hero.webp" type="image/webp">
<img src="/hero.jpg" alt="Hero image" width="1200" height="600">
</picture>
Generate and select responsive sizes
An image displayed at a few hundred CSS pixels does not need a source at several thousand physical pixels. Generate a small set of sizes, and use srcset with a sizes attribute so the browser picks the smallest that fits the current layout and the device pixel ratio. For high-density screens, provide a 2x variant so the image is crisp without sending 2x bytes to a standard screen.
Do not set width and height to arbitrary values; set them to the image’s intrinsic aspect ratio so the browser reserves the space and does not shift the layout when the image loads. That reservation is what keeps CLS low, and it costs nothing. Choose a handful of breakpoints that match the layout, not a separate size for every pixel width.
<img
src="/photo-800.jpg"
srcset="/photo-400.jpg 400w, /photo-800.jpg 800w, /photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 600px"
width="1200" height="800" alt="Product photo" loading="lazy" decoding="async">
Lazy-load below the fold
Images below the initial viewport are not needed to render what the user first sees, so loading them eagerly delays the first paint and wastes bytes if the user never scrolls. The loading=lazy attribute tells the browser to defer an image until it is near the viewport, which reduces the initial download and improves the largest contentful paint for the image above the fold.
Load eagerly only the image or images in the initial viewport, and mark the rest lazy. The eager image should also be preloaded or prioritized so it is not queued behind other resources. For a landscape of images, lazy loading is the difference between downloading the visible few and downloading everything, and it is a one-attribute change. Do not lazy-load the hero image, or it competes with nothing and the first paint waits.
Cache each variant immutably
Every format and size is a distinct file, and each should be fingerprinted and cached immutably, so a changed image produces a new URL and no purge is needed. A versioned variant URL, such as hero.800.6f3a1b2c.avif, can be cached for a year, and a change to the image or its encoding produces a new hash and a new URL.
Serve the variants from a CDN so they are close to users and off your origin. Because they are immutable, the CDN caches them indefinitely, and the only fresh asset is the HTML that references them. Combine the format negotiation with the CDN’s Vary handling so a cached AVIF is never sent to a client that cannot decode it.
// variant URL encodes format, size, and content hash: no purge ever needed
// /img/hero.800.6f3a1b2c.avif
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
res.setHeader('Vary', 'Accept'); // format chosen by Accept-Encoding-like negotiation
Test formats, sizes, and weight
Test that a client advertising AVIF support receives an AVIF and one that does not receives the fallback, and that Vary: Accept is set when the format depends on Accept. Test that a srcset with sizes causes the browser to fetch a smaller variant on a narrow viewport. Test that below-the-fold images carry loading=lazy and the hero does not. Test that each variant URL is fingerprinted and served immutably.
# a client that accepts avif gets the avif variant; a plain client gets jpeg
curl -sD - -o /dev/null -H 'Accept: image/avif,image/webp,*/*' http://localhost:3000/hero | grep -i -E 'content-type|vary'
curl -sD - -o /dev/null -H 'Accept: image/jpeg' http://localhost:3000/hero | grep -i content-type
Common pitfalls
Serving a full-resolution image to a small viewport, using only JPEG when a client supports a modern format, missing the fallback so some clients see nothing, lazy-loading the hero so the first paint waits, omitting width and height so the layout shifts, and caching variants under a fixed URL so a change is never picked up are the recurring mistakes. Format negotiation with a fallback, srcset and sizes, a lazy policy that spares the hero, reserved dimensions, and fingerprinted variants address them.
Conclusion
Image optimization is choosing the right format, size, and moment for each client. Offer a modern format with a fallback, generate responsive sizes and densities, lazy-load below the fold while loading the hero eagerly with reserved dimensions, and cache every fingerprinted variant immutably. Self-check by asserting the negotiated format and Vary, a smaller variant on a narrow viewport, and lazy on off-screen images. Then the browser downloads the smallest image it can actually show.
Sources: MDN: Responsive images, web.dev: Image formats and compression, MDN: Lazy loading for images.
FAQ
- AVIF, WebP, or JPEG?
- AVIF often compresses best, WebP is broadly supported and good, JPEG is the universal fallback. Serve AVIF or WebP to clients that advertise support via Accept, and a JPEG fallback for the rest, so every client gets the smallest format it can decode.
- Do I need responsive sizes?
- Yes for images that appear at different widths. Serving a full-width image to a phone wastes bytes it cannot show. Generate a few sizes, use srcset with sizes so the browser picks one, and the browser downloads the smallest that fits the display.
Report a problem with this articleOpens a pre-filled issue on GitHub.