How to set R2 Cache-Control: public

Hi,

I’m new to Cloudflare and I’m trying to set my files in Cloudflare R2 to have Cache-Control: public. Is there some documentation on how I can set this?

Thank You!

Greetings,

Thank you for asking.

What comes to my mind at first sight without testing, I’d try out with Transform Rules → Modify Response Header, but firstly I’d add and connect my custom sub-domain like cdn.example.com to my R2 bucket :thinking:

Create an R2 bucket:

Connect custom (sub)domain to your R2 bucket:

Create a Transform Rule to Modify the Response Header if request URI Path contains sub.example.com, Set Static: Cache-Control: public:

Regarding the cache:

The idea what with the Cache API could be done, which we could try to make with Transform Rule to modify the Response Header:

If not, then using Workers we could achieve it.

I tried adding a transform rule like this for the cache control, is this correct?

Yes, we could try it out if that would work at all :thinking:

However, maybe you’d like to try to setup if Hostname contains r2.example.com and URI Path contains directory-name (where your assets like JS, CSS, images are) to cache it.

That way, it wouldn’t and shouldn’t set it for all the requests coming to your Website, and only for the sub-domain using the R2 bucket (with custom domain name).

Since, some things are a good read and to keep in mind as follows from below:

Other helpful article:

Okay now cache-control: public is showing up in headers. But for some reason even with max-age=31536000, I’m still seeing resources showing MISS, despite seeing it being HIT a few hours ago and accessing from the same location. Is this normal? Shouldn’t the same resource show up as HIT if a few hours ago it was HIT?

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.