Single file cache not working
I have been stuck on this for a long time. I cannot get the single file cache purge to work. My files are from an r2 bucket.
this simply does not work. When I purge on the cloudflare dashboard, it does not work. I have 0 custom transform rules in place. I have a single cache rule in place on the r2 custom domain.
The cache header is always showing HIT unless I purge everything then it is EXPIRED and new content is served. What am I doing wrong? I've read the docs, read the community help pages, nothing is working.
I have also asked 3 times in the r2 channel about this and no one from the Cloudflare team has said anything.
21 Replies
When I purge on the cloudflare dashboard, it does not work. I have 0 custom transform rules in place. I have a single cache rule in place on the r2 custom domain.If it doesn't work even through the single purge url in the dashboard then ignore your api stuff for now. You just have a simple R2 Custom Domain for a bucket, and you're trying to purge it, no query params or anything special at all, just a path? What are the exact headers being returned on HIT, is there an age header?
also no query params, just the r2 domain and the file path to the item
To ensure this, I copied the exact url from the request
the same one that returns HIT
I'd use the account level trace option(https://dash.cloudflare.com/?to=/:account/trace/search) with the same URL and see what you're hitting. Particularly watching out for any Rules/etc & Workers
there was a url normalization rule, but ive turned it off and am still getting this error
is this incorrect for caching r2 content?
would need to see the actual rule, although I think only really the custom cache key setting could mess with this. You should just need custom cache eligibility and an Edge TTL, no other settings
You don't need the domain name im assuming
I was avoiding asking because you seemed hesistant to share lol. I see what your issue is though. That request you sent above is a CORS request, you've got Vary: Origin on there. From the limitations page of Single Purge URL: https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-single-file/
A single-file purge performed through your Cloudflare dashboard does not clear objects that contain any of the following: Origin header ↗was trying to see what the best way around it would be, might be even able to just drop the Origin header and not fragment your cache and handle your own CORS with transform rules as an option
Easiest may just be, if you're already wanting to use the api, you should be able to just specify the same origin header in the purge request: https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-url
Cloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
how do I drop the origin header? It's put there by r2 no?
No, your client/web browser is sending because cross-origin
this is unrelated to r2 and just normal cf caching stuff
If you're only going to access it from one origin/site, including the origin header in the purge request is probably the easiest way out
You can see in the client request headers what that would be
ok let me try this rq
@Chaika what exactly do I need to write
is it the access-control-allow-origin header
Do the same request you did to get this, and look at the Request Header "Origin"
sorry mb I was looking and response headers
one sec
holy sh*t thank you so much @Chaika
I cannot tell you how long I spent banging my head against a wall😭
One question @Chaika , when I do this via curl it is working now. But when I do this via cloudflare worker, it is still not working. Are there limitations that i'm not aware of?
No, Workers can use the Cf API no issue. Does the response body indicate success?
yeah it does
says 200 status code and gives an id
Im using wrangler dev --remote, not sure if this effects it
It's possible the remote preview is being weird. If it's not working already any harm in deploying to prod and testing there? Alternatively, does it work in wrangler dev without --remote (so local)?
i can try prod
yeah that worked
I can just turn caching off for development ig
@Chaika thx again sm