Should you use the cache API when requesting from R2?

I have a super simple worker wrapping R2. This is the code so far:
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
const key = url.pathname.slice(1);


const object = await env.MODRINTH_CDN.get(key);

if (object === null) {
return new Response(JSON.stringify({ error: 'not_found', description: 'the requested file does not exist' }), {
status: 404,
headers: {
'content-type': 'application/json',
},
});
}

const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set('etag', object.httpEtag);

return new Response(object.body, {
headers,
});
},
};
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
const key = url.pathname.slice(1);


const object = await env.MODRINTH_CDN.get(key);

if (object === null) {
return new Response(JSON.stringify({ error: 'not_found', description: 'the requested file does not exist' }), {
status: 404,
headers: {
'content-type': 'application/json',
},
});
}

const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set('etag', object.httpEtag);

return new Response(object.body, {
headers,
});
},
};
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
Chaika
Chaika•8mo ago
👋
Do I have to use the Cache API to use the cloudflare CDN to cache the assets on R2? Or is that automatic
Yes, you need to use the Cache API
Or should I put a page rule in front of this saying to cache things
Cache 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
Geometrically
Geometrically•8mo ago
Yup that's what I mean Thanks!
Chaika
Chaika•8mo ago
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
Geometrically
Geometrically•8mo ago
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?
Chaika
Chaika•8mo ago
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
Geometrically
Geometrically•8mo ago
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?
Chaika
Chaika•8mo ago
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/
Geometrically
Geometrically•8mo ago
gotcha, is there an easy way to write to my local "r2 bucket"?
Chaika
Chaika•8mo ago
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
Geometrically
Geometrically•8mo ago
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