Repository navigation
design: cache media files in the route cache - #4735
Conversation
|
|
@swissky thanks for this! it solves most of what i raised in #4214 I wanted to ask about this
It seems to me that not limiting what variants can be generated/cached would easily lead to both unintentional and malicious cache bloat, which could result in real user-costs for the site operator. Is there a reason you are hesitant to include something about that in this design? I wonder if it could actually just fit into a cache normalization phase, eg |
|
Thanks @alibosworth, that's a fair point and worth settling before this moves forward. Some of this exists today, without the cache: On cost: Cloudflare Images bills per unique transformation per month, and format and quality don't count towards uniqueness. Width, height and Where caching does add a new risk is For normalization, I'd like to add this to the plan: build the cache key only from the known parameters ( Snapping to preset widths ( Does that work for you, or would you rather have the width limits in this plan as well? |
Approval statusThe current revision meets the approval requirements.
This comment updates as reviews arrive. See MAINTAINING.md and the proposal process for the rules. |
|
|
||
| When the site configures an Astro cache provider (`cacheCloudflare()`, `memoryCache()` or another adapter's provider), EmDash stores image responses in the route cache and purges them when the image changes: | ||
|
|
||
| - A whole `200` image response from the media file route, and every successful variant from the image endpoints (Node and the Cloudflare Images binding), calls `context.cache.set({ maxAge: 86400, tags: ["emdash:media:<storageKey>"] })`. |
There was a problem hiding this comment.
Why just a day? If we're invalidating it on change, we could send a year-long expiry.
There was a problem hiding this comment.
Thanks @ascorbic!
Lifetime: I went with a day to be on the safe side for the first version, but you're right: the purge is what keeps it correct, so a year is the better choice. To make that safe, deletes through the plugin API and the MCP tool will purge the tag in this plan too, instead of a follow-up. The one case left is a purge that Workers Cache rejects (rate limit). I'll document that and point to manual invalidation (#4587) as the way out.
Normalization: I like this better than skipping unknown params. While checking, I noticed that Astro's getURL also emits position and background (my own site's URLs end in &fit=cover&position=center), so the plan's current allowlist would have left those variants uncached. I'll normalize to the order getURL produces (href, w, h, q, f, fit, position, background), drop unknown and repeated params with a 301, and cache only the canonical form. Astro's own URLs are already canonical, so they never hit the redirect, and the open question about Workers Cache parameter order goes away.
I'll push the update and ping you for a fresh look.
| When the site configures an Astro cache provider (`cacheCloudflare()`, `memoryCache()` or another adapter's provider), EmDash stores image responses in the route cache and purges them when the image changes: | ||
|
|
||
| - A whole `200` image response from the media file route, and every successful variant from the image endpoints (Node and the Cloudflare Images binding), calls `context.cache.set({ maxAge: 86400, tags: ["emdash:media:<storageKey>"] })`. | ||
| - A variant is only stored when its query contains nothing but `href`, `w`, `h`, `f`, `q` and `fit`, each at most once. A request with any other parameter (for example `&x=1`) is still served, but not cached, so extra parameters can't create new cache entries. |
There was a problem hiding this comment.
We could normalise the params, doing a permanent redirect to remove any unknown params (and possibly sort the params if we can be sure they're always generated in the same order.) That would let us cache everything.
|
|
||
| ## Open questions | ||
|
|
||
| - Does Workers Cache key entries by the full query string? Normalization makes the answer irrelevant for correctness, but it decides whether the redirect is also needed to avoid duplicate entries on Cloudflare. |
Design proposal
Proposal: proposals/media-route-cache.md
Uploaded images and their
/_imagevariants are served withmax-age=0and no route-cache intent, so on Cloudflare every view is aBYPASSthat runs the Worker, reads R2 and transforms variants again. The short lifetime exists because replacing an image keeps its storage key and nothing purges caches.This plan stores image responses in the configured Astro route cache for one day, tagged
emdash:media:<storageKey>, and purges that tag on replace and delete. Browsers keep revalidating with the existing validators. A prototype on a throwaway Worker went fromBYPASStoHIT(about 50 ms), and replace and delete took effect immediately for the file and its variants. Audio and video stay uncached becausememoryCache()ignoresRange.Proposal type
Related work
Discussion: #3159
Readiness
accepted(check only when ready to merge)AI assistance