Image Optimization

Cook automatically optimizes raster images during the build. Every <img> tag referencing a local JPG, PNG, or WebP file is rewritten to a <picture> element with AVIF and WebP sources at multiple responsive widths.

How it works

The image pipeline runs in two phases:

  1. Generate -- Cook scans dist/ for raster images (.jpg, .jpeg, .png, .webp) and creates responsive derivatives using sharp. Each image gets AVIF and WebP versions at every configured width that does not exceed the original dimensions (no upscaling).

  2. Rewrite -- Cook finds <img> tags in HTML pages, looks up each image in the generated manifest, and replaces the tag with a <picture> element containing <source> sets for modern formats and a fallback <img> with srcset, sizes, width, height, and loading="lazy".

Before and after

Given this source image tag:

Cook produces:

Opting out per image

Add data-no-optimize to skip a specific image:

SVGs, GIFs, data URIs, and external URLs are always skipped automatically.

Caching

Cook uses a content-hash disk cache (default: .cache/images/) to avoid regenerating unchanged images. On subsequent builds, only new or modified images are processed. The cache stores an MD5 hash of each source file and verifies that all derivative files still exist on disk.

Configuration

OptionDefaultDescription
enabledtrueEnable image optimization
widths[320, 640, 960, 1280, 1920]Responsive widths to generate
formats['avif', 'webp']Modern formats to produce (in addition to original)
quality{ avif: 60, webp: 75, jpeg: 80, png: 80 }Quality per format (1-100)
sizes'(min-width: 60rem) 960px, 100vw'Default sizes attribute
loading'lazy'Default loading attribute
cache'.cache/images'Disk cache directory for generated derivatives