Should you use the cache API when requesting from R2?
I have a super simple worker wrapping R2.
This is the code so far:
Do I have to use the Cache API to use the cloudflare CDN to cache the assets on R2? Or is that automatic. This cache API: https://developers.cloudflare.com/workers/examples/cache-api/. Or should I put a page rule in front of this saying to cache things
Using the Cache API · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones …
10 Replies
👋
Do I have to use the Cache API to use the cloudflare CDN to cache the assets on R2? Or is that automaticYes, you need to use the Cache API
Or should I put a page rule in front of this saying to cache thingsCache doesn't run in front of workers, no matter how many page/cache rules you set Well, to clarify: The assets are already on R2, you wouldn't be "caching on R2", but instead caching on the edge
Yup that's what I mean
Thanks!
Any requests which hit edge cache don't cost you anything, no Class Bs, other then the Worker invocation cost
If you can use R2's Custom Domain feature, you can even get away from those.
If you need to use a worker, you could look into this library here: https://github.com/kotx/render which does cache/etc for you, can hook into it like a library
thanks!!
yeah we use custom domains rn but we're doing some more complex stuff now so converting it to a worker
@Chaika Sorry for the ping, but does stuff like polish run in front of workers?
I don't remember the exact details, but it looks like my R2 Worker does use polish after the image gets cached and a few hits, it's a function of cache I guess
gothca thx
does cache work in local testing? it's not working for me. i read somewhere you need a custom domain under cloudflare, is that also true for local?
Requires Custom Domain yea, I would assume it doesn't work for --remote dev either, not sure about local local though
However, any Cache API operations in the Cloudflare Workers dashboard editor, Playground previews, and any *.workers.dev deployments will have no impact. For Workers fronted by Cloudflare Access , the Cache API is not currently available. Only Workers deployed to custom domains have access to functional cache operations.https://developers.cloudflare.com/workers/runtime-apis/cache/
gotcha, is there an easy way to write to my local "r2 bucket"?
You can use wrangler r2 commands like wrangler r2 object put <OBJECT_PATH> [OPTIONS] with the --local flag to interact with local
https://developers.cloudflare.com/workers/wrangler/commands/#r2-object
it's a bit weird since you don't need --local for dev but you do for the commands but it is there lol
himm for me it says --local doesnt exist
wrangler r2 object put modrinth-cdn-staging/test.txt --file wrangler.toml --local
this is my command
oh nvm
my wrangler was out of date