Building an Image Optimization Pipeline: WebP, AVIF, and Beyond

Philip Rehberger Dec 3, 2026 7 min read

Automate format selection, resizing, and quality tuning. Covers build-time vs request-time pipelines.

Images are usually the largest payload on a web page. A site that ships 4 MB of images and 200 KB of JavaScript has its bandwidth bottleneck in the wrong place if it spends weeks optimizing JS. Yet image optimization rarely gets serious attention until a Core Web Vitals review surfaces the cost. The fix is not manual work per image; it is a pipeline that handles format, resolution, and quality automatically.

This is a guide to building that pipeline: the formats that matter, the resizing strategies that work, and the choice between doing this at build time or per request.

The Format Landscape

Three modern formats matter, plus the legacy ones.

WebP (Google, 2010). Lossy and lossless modes. Better compression than JPEG and PNG for the same quality. Universal browser support since 2020.

AVIF (AOMedia, 2019). Based on AV1 video codec. Smaller files than WebP at the same perceived quality, especially for photographic content. Broad but not universal browser support — older Safari versions, some embedded browsers, don't decode it.

JPEG XL (ISO, 2022). Promising successor with both lossy and lossless modes, fast encoding, and progressive decoding. Browser support is uneven; Chrome temporarily added then removed it. Used in some pipelines but not yet as deployment-ready as AVIF.

Legacy. JPEG and PNG remain the fallback. Every browser decodes them. Every camera produces them.

A typical modern image pipeline serves AVIF to clients that support it, WebP to those that support WebP but not AVIF, and JPEG/PNG as the universal fallback.

Negotiating the Format

HTTP Accept headers carry browser capability. A request from a modern Chrome includes image/avif, image/webp, image/apng, image/svg+xml, image/*, */*. The server (or CDN) picks the best supported format.

Two deployment patterns:

<picture> element with explicit sources. The browser's HTML parser picks based on supported formats. No server-side negotiation needed.

<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img src="hero.jpg" alt="..." width="1600" height="900">
</picture>

Works for static content. Requires three encoded files. Browser picks the best one it supports.

Server-side negotiation. One URL; the server returns the best format based on Accept. Cleaner HTML, easier to add new formats later.

GET /images/hero.jpg
Accept: image/avif, image/webp, image/jpeg

→ HTTP 200
Content-Type: image/avif
Vary: Accept

Requires the server or CDN to be aware of formats and to encode on-demand or pre-encode all variants. Modern CDNs (Cloudflare Images, Cloudinary, Imgix) handle this natively.

For most teams, the CDN-based pattern is simpler operationally. For teams that already pre-build their static assets, the <picture> pattern adds no infrastructure.

Resizing

Serving a 4000x3000 image to a phone with a 320-pixel-wide viewport is wasteful — the phone scales it down for display anyway. Responsive images solve this with srcset:

<img src="hero-800.jpg"
     srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w, hero-3200.jpg 3200w"
     sizes="(max-width: 600px) 100vw, 50vw"
     alt="...">

The browser picks the size that best fits its viewport and pixel density. The cost is producing multiple resized versions of every image. The pipeline handles that.

A reasonable set of sizes for a modern responsive image: 400, 800, 1200, 1600, 2400 pixels wide. Five resizes per source image. Combined with three formats (AVIF, WebP, JPEG), each source image produces 15 output files.

Quality Settings

Quality is a tradeoff between file size and visual fidelity. The choice is per-format because the perception curves differ.

Reasonable starting points:

  • JPEG quality 80 — the legacy default. Above 85, diminishing returns. Below 75, visible artifacts.
  • WebP quality 80 — comparable to JPEG 80 with smaller files.
  • AVIF quality 50-65 — AVIF's quality scale is different. Mid-range values produce smaller files than WebP at equivalent perceived quality.

These are starting points. The right values for a given site depend on content (photo vs illustration vs UI) and on how the images are displayed (full-screen vs thumbnail). A perceptual quality tool (Butteraugli, SSIMULACRA) gives more objective comparison.

For most sites, picking conservative defaults (JPEG 80, WebP 80, AVIF 55) and adjusting based on observed file sizes catches 90% of the value.

Build-Time vs Request-Time

The biggest pipeline architecture decision: encode images at build time or at request time.

Build-time. Static site generators or build steps process every image during the build. Output is committed to source control or uploaded to storage. Serving is just a static file. Predictable, cacheable, no runtime cost.

Works when:

  • The set of images is bounded and known at build time.
  • The build can run on the volume of images without taking forever.
  • New image variants (sizes, formats) require a rebuild.

The cost is build time. A site with thousands of images and 15 variants per image is producing tens of thousands of encoded files. AVIF encoding is slow; this can add minutes per build.

Request-time. A service generates the requested variant on demand, caches the result, and serves cached versions thereafter. URLs encode the variant parameters.

GET /images/hero.jpg?w=800&fm=webp&q=80

Works when:

  • The image set is unbounded (user uploads) or the variants are open-ended.
  • The team prefers operating an image service to managing a big static asset set.
  • New variants should appear without a rebuild.

The cost is operational. The image service is a piece of infrastructure to keep running. The first request for a variant is slow (encoding time).

Most mid-size sites benefit from a hybrid: build-time for the main marketing pages and known assets, request-time for user-uploaded content. Many CDNs blur the line by offering on-demand transforms over their cache.

CDN-Based Pipelines

The simplest pipeline for many teams is to offload everything to a CDN that supports image transforms.

  • Cloudflare Images — upload originals, request variants via URL parameters. Format negotiation automatic.
  • Cloudinary — most powerful transforms (effects, AI cropping). Generous free tier but pricier than alternatives at scale.
  • Imgix — focused on the basics; high reliability, simple pricing.
  • AWS CloudFront with Lambda@Edge — DIY, but flexible and low cost. More operational work.

The cost-benefit shifts at scale. A small site is best served by a managed image CDN. A site with hundreds of millions of image requests per month often hits the cost ceiling and moves to a custom Lambda@Edge or origin-side pipeline.

Lazy Loading

No discussion of image optimization is complete without lazy loading. The native loading="lazy" attribute defers loading images that are below the fold:

<img src="hero.jpg" alt="..." loading="lazy" width="1600" height="900">

Browser-native, no JS required, works in every major browser. The single highest-leverage performance improvement after format optimization.

The width and height attributes are not optional. Without them, the browser doesn't know how much space to reserve, causing layout shift when the image loads (poor CLS). Always include explicit dimensions.

Measuring Impact

After a pipeline is in place, measure to confirm it is working:

  • LCP element. What's the largest contentful paint? Is it an image? Is it being served in a modern format?
  • Total image weight per page. Sum of image bytes for a typical page load. Should be a fraction of pre-pipeline numbers.
  • Cache hit rate. For request-time pipelines, the ratio of cached to recomputed responses. Should be very high after a warm-up period.
  • Format distribution. What fraction of clients are receiving AVIF, WebP, or JPEG? Aligns with browser support data.

A good pipeline reduces image bytes by 50-80% compared to baseline JPEG. If the numbers are smaller, the pipeline is not exercising its capabilities — usually a format negotiation issue or a missed resize step.

A Starting Pipeline

For a site with no image optimization today:

  1. Add a CDN with image transforms in front of the origin. Configure for automatic format and quality.
  2. Migrate marketing pages to use <picture> with srcset for explicit responsive variants.
  3. Add loading="lazy" to all below-the-fold images. Add explicit width and height.
  4. Measure. Confirm LCP improvement, total page weight reduction, no CLS regression.
  5. Tune quality settings based on observed file sizes and visual fidelity for specific content types.

This is one sprint for a typical marketing site and produces page-weight reductions in the range that move Core Web Vitals scores meaningfully.


Building an image pipeline that scales with your traffic? We help teams design image delivery that matches their content and audience. scopeforged.com

Share this article

Related Articles

Need help with your project?

Let's discuss how we can help you build reliable software.