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,
        });
    },
};


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
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones
Was this page helpful?