We cut one hero image in half and the first screen got faster than it had after a month of JavaScript cleanup. Nothing about the code changed. The thing making people wait wasn't our logic — it was a PNG the designer exported at three times the size the phone could ever display.
We were slow to notice this: on most mini-program screens, images are the heaviest thing on the page, and they're the last thing anyone profiles. We spent weeks tuning render paths and roughly no time asking whether a 2x asset was being decoded on a 1x screen. Once we did, the wins were embarrassingly cheap.
Where the bytes actually go
Format is the first cut. For the same photo, WebP is usually much smaller than PNG or JPEG, and AVIF smaller still; but not every host runtime can decode them, so you need a fallback. Our rule now is boring: modern format first, with an old format anyone can open sitting right next to it. Don't assume one format covers every host. It sounds tidy right up until you ship it.
The second cut is serving the right size. Decoding an asset prepared for a 3x screen on a small one is the most common and most pointless waste there is. A srcset-style set of variants, generated at build time, lets the device take only the file it needs. The third cut is lazy loading: images below the fold get fetched when they're near, so first paint decodes less — but the placeholder has to hold the space, or the layout jump annoys people more than the wait did.
Weak networks need their own plan. While the real image is in flight, a blurred placeholder or a flat colour block makes the screen look alive instead of empty; if it never arrives, don't wait forever, serve something usable. A CDN handles resizing and format negotiation at the edge, saving origin and transfer, at the price of putting that logic on your critical path.
| Technique | What it saves | What it adds | When it isn't worth it |
|---|---|---|---|
| WebP instead of PNG/JPEG | A large slice of transfer bytes on photos | An older host runtime may not decode it; keep a fallback | A tiny icon or a one-colour shape |
| AVIF for hero art | Even smaller files at the same visual quality | Slower encode, spottier support, another build variant | When your pipeline can't afford a second format |
| Size variants for one image | No more decoding a 3x asset on a small screen | Several files per image and a build step to produce them | A static asset that only ever renders at one size |
| Lazy loading below the fold | Fewer bytes and fewer decodes at first paint | Placeholder logic, and a risk of layout shift | Above-the-fold content, where it only delays the obvious |
| CDN with device-aware transforms | Resizing and format negotiation at the edge | A bill, cache rules, and a vendor in your critical path | Low-traffic internal screens where a local file is fine |
| Blur placeholder on weak networks | A screen that looks alive before the real image lands | Extra markup and a low-quality asset to maintain | When the image is tiny and arrives instantly anyway |
The parts that bite back
Compatibility is the annoying one. AVIF looks best on paper, but the moment an older host runtime can't open it you need a separate variant and a way to decide which to serve. Every extra format is another build branch, another cache key, and another way for production to produce a this-user-sees-a-blank-image report. We ended up keeping AVIF for the hero and WebP with a fallback for everything else — not because AVIF is bad, but because the gain doesn't pay for that complexity.
Build complexity is the second cost. Generating several sizes sounds like a few lines of config, but in practice you're now naming derived assets, cleaning them up, and watching artefact size in CI. Design fidelity pushes back too: compress harder and gradients and fine text blur first, and the person frowning in review is the designer. We argued about it and settled on higher quality for the hero, harder compression on thumbnails, rather than measuring every image with the same ruler.
CDN cost isn't something to gloss over either. Edge transforms are billed per request, and when cache hit rate slips, the invoice and the first screen get worse together; it's also a third party on your critical path, so when it sneezes you get a blank page. That's why low-traffic internal screens just use a local file. And one that won't be popular: a CDN isn't the end of the story. It only moves the bytes somewhere else. The image still has to be small.
Start with the one image that matters
So now every screen starts with one question: which single image is the person waiting for? We export that one at the size it'll actually be displayed, give it a modern format with a fallback, and a placeholder that holds the layout. Everything below the fold can wait.
The rest of the techniques are optional. That one isn't. If you only have an afternoon, spend it on the hero asset and the cold-start request that fetches it — that's where the weight is, and no amount of code splitting will move it.
Sources
- MDN — Image file types — what WebP and AVIF actually support, and where fallbacks are needed.
- MDN — The img element — srcset, sizes and native lazy loading in one place.
- MDN — Intersection Observer API — the usual way to defer off-screen images yourself.
- HTTP Archive — how much of a typical page is images, measured rather than guessed.
- Cloudflare Learning Center — what edge resizing and format negotiation actually do.