BareProxy Image Optimizer Plugin: Resizing and WebP and AVIF Conversion on the Fly
Status: planned, number 20 of 21 in BareProxy’s build order. The plugins are built easiest first, and this one is about a week of coding: the plugin plus an image encoder running beside BareProxy. It comes after the AI crawler payment gate plugin. This post describes what it will do, and it will be updated as it is built.
Images are most of the bytes on most pages. A site’s folder usually holds one large JPEG or PNG per picture, and every visitor downloads it whole, whether the screen is a phone or a 4K monitor. Modern browsers can take WebP and AVIF, which are often a third to half the size for the same picture. Few sites make those copies, because it means a build step, a toolchain and twice the files.
The image optimizer does it at the proxy, the first time an image is asked for.
What It Will Do
- Convert. When a browser says it accepts AVIF or WebP, it gets that, made from the original. Others get the original.
- Resize. A width in the query, such as
?w=640, gets a copy at that width, from a list of sizes the config allows so nobody can ask for a million variants. - Keep the copies. Each converted image is kept in the plugin’s store, so the work happens once per image and size, and the next visitor gets it straight from disk.
The Heavy Part
Encoding AVIF is slow work, too slow to do inside a sandboxed plugin on the request path. So the plan follows the same pattern as RenderCache: the plugin decides and serves copies from its store, and a separate encoder process next to BareProxy makes them. WebP may be fast enough to make inside the plugin itself. That gets measured before it’s decided.
A config will look something like this:
plugin images /etc/bareproxy/plugins/image-optimizer.wasm
config /etc/bareproxy/plugins/images.json
store 5GB
body response
allow-http 127.0.0.1:9300
site example.com
use images
route /* -> files /var/www/example/public
Every image request will carry a note in its record: which format and size went out, and whether it came from the store or was made just then.
The whole program is on the plugins page.