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.
const clearCache = await axios.post(`https://api.cloudflare.com/client/v4/zones/${env.CLOUDFLARE_ZONE_ID}/purge_cache`, {
files: [https://my.customr2domain.com/filename],
}, {
headers : {
'X-Auth-Email': env.CLOUDFLARE_EMAIL,
'X-Auth-Key': env.CLOUDFLARE_GLOBAL_API_KEY,
'Content-Type': 'application/json'
}
})
.catch(e => console.log(e.response.data.errors[0]))
const clearCache = await axios.post(`https://api.cloudflare.com/client/v4/zones/${env.CLOUDFLARE_ZONE_ID}/purge_cache`, {
files: [https://my.customr2domain.com/filename],
}, {
headers : {
'X-Auth-Email': env.CLOUDFLARE_EMAIL,
'X-Auth-Key': env.CLOUDFLARE_GLOBAL_API_KEY,
'Content-Type': 'application/json'
}
})
.catch(e => console.log(e.response.data.errors[0]))
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
Chaika
Chaika3mo ago
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?
Caleb
CalebOP3mo ago
No description
Caleb
CalebOP3mo ago
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
Chaika
Chaika3mo ago
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
Caleb
CalebOP3mo ago
there was a url normalization rule, but ive turned it off and am still getting this error
Caleb
CalebOP3mo ago
No description
Caleb
CalebOP3mo ago
is this incorrect for caching r2 content?
Chaika
Chaika3mo ago
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
Caleb
CalebOP3mo ago
No description
Caleb
CalebOP3mo ago
You don't need the domain name im assuming
Chaika
Chaika3mo ago
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
Chaika
Chaika3mo ago
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
Caleb
CalebOP3mo ago
how do I drop the origin header? It's put there by r2 no?
Chaika
Chaika3mo ago
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
Caleb
CalebOP3mo ago
ok let me try this rq @Chaika what exactly do I need to write is it the access-control-allow-origin header
Chaika
Chaika3mo ago
Do the same request you did to get this, and look at the Request Header "Origin"
Caleb
CalebOP3mo ago
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?
Chaika
Chaika3mo ago
No, Workers can use the Cf API no issue. Does the response body indicate success?
Caleb
CalebOP3mo ago
yeah it does says 200 status code and gives an id Im using wrangler dev --remote, not sure if this effects it
Chaika
Chaika3mo ago
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)?
Caleb
CalebOP3mo ago
i can try prod yeah that worked I can just turn caching off for development ig @Chaika thx again sm
Want results from more Discord servers?
Add your server