© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•3y ago•
21 replies
Geometrically

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 …
Using the Cache API · Cloudflare Workers docs
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Caching R2 using worker cache api
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
Using Cache API with gzipped R2 object
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
Cache access token requested from an API and use it in the subsequent requests
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago
Confused with how to use R2 S3 API.
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago