Seven posts in, and I'd just been uploading images as-is. Take a screenshot, upload it, it gets saved exactly as captured. No size cap in place.
When I finally counted, the media folder was 2,904KB. A single cover image alone was 104KB.
Setting a cap is the easy part
Just add a rule to the upload config of whatever CMS you're using.
upload: {
mimeTypes: ['image/png', 'image/jpeg', 'image/webp', 'image/gif'],
formatOptions: {
format: 'jpeg',
options: { quality: 82, progressive: true, mozjpeg: true },
},
resizeOptions: { width: 1600, height: 1600, fit: 'inside', withoutEnlargement: true },
imageSizes: [{ name: 'card', width: 800, fit: 'inside', withoutEnlargement: true,
formatOptions: { format: 'jpeg', options: { quality: 78, progressive: true, mozjpeg: true } } }],
}
Cap the long edge at 1600px, re-encode as JPEG, make it progressive. Also generate an 800px card variant for list views.
The withoutEnlargement flag is there so small images don't get blown up. Without it, a 300px icon would get stretched to 1600px and end up bigger than it started.
But what's already uploaded stays as-is
This is where it got me. Upload rules apply only to files added going forward. The 2,904KB already in the folder is untouched.
I had to write a separate backfill script. And that forced a decision.
I decided not to rename the files
Giving the newly processed files new names would be cleaner. But that would mean rewriting every image URL referenced in the body of all seven already-published posts.
Post bodies are stored as markdown, with URLs baked in as literal text. Get even one character wrong and that spot turns into a broken image. That's a dozen-plus manual edits.
I went with overwriting the same filenames instead. The post bodies didn't need to be touched at all.
Results
cover-s7.jpg 1792x1024 104KB → 1600x914 23KB + card 800px 9KB
...
Total 2904KB → 1505KB
| Before | After | |
|---|---|---|
| Media folder total | 2,904KB | 1,505KB |
| One cover image | 104KB | 23KB |
| Card variant for lists | none | 9KB |
List views now fetch a 9KB card instead of the 23KB full image. The difference showed up most on the homepage, where multiple cards render at once.
Two things that tripped me up
Changing the schema doesn't create the column
After adding the card variant, saves started failing. The database in use is SQLite, and schema changes weren't being applied automatically.
I had to add the six needed columns by hand.
ALTER TABLE media ADD COLUMN sizes_card_url text;
ALTER TABLE media ADD COLUMN sizes_card_width numeric;
ALTER TABLE media ADD COLUMN sizes_card_height numeric;
ALTER TABLE media ADD COLUMN sizes_card_mime_type text;
ALTER TABLE media ADD COLUMN sizes_card_filesize numeric;
ALTER TABLE media ADD COLUMN sizes_card_filename text;
That's when I learned one variant adds six columns.
The image processing library only existed on one runtime
The backfill script died with Cannot find module 'sharp'. Two Node versions were installed, and it was only installed on one of them. The build was running on that version, but I'd run the script under the other one.
Not the kind of thing you get stuck on for long, but the first few minutes of "wait, this was working a second ago" are genuinely confusing.
What's left
Post images still don't have width and height set. Without those, the browser can't reserve space before the image loads, so the layout jumps down the moment the image appears.
That's the thing where the text shifts while you're reading. It's a separate problem from file size, so it needs its own fix.



