Worker Image CDN
A Cloudflare Worker that acts as an image CDN proxy. It fetches images from an origin server, automatically converts them to modern formats, resizes on the fly, and caches the results in R2.
Features
- Automatic format conversion — Serves AVIF or WebP based on the browser's
Acceptheader - On-the-fly resizing — Resize via query parameters, preserving aspect ratio
- Quality control — Adjust output quality with
?quality=80 - R2 caching — Transformed images are cached in Cloudflare R2
- SVG passthrough — SVGs are served as-is without conversion
- Cache observability —
X-Cache: HIT/MISSheader
How it works
Request → Check R2 cache → [HIT] → Serve cached image
→ [MISS] → Fetch from origin
→ Resize (if requested)
→ Convert to AVIF/WebP
→ Store in R2
→ Serve image Quick start
npm install
npx wrangler r2 bucket create worker-cdn-images
npm run dev Then request an image:
http://localhost:8787/photo.jpg?w=800&quality=80