The response I’m getting is totally ok and I get success: true.
For some reason this doesn’t work 100% of the times though. I thought it was a matter of waiting but when it works the url is purged immediately. When it fails the url is not purged even after 10 minutes of calling the API.
You want to purge the cache Cloudflare-wide, not just in the datacentre where the Worker is running, correct? In that case the API is the right approach.
Purging can take a couple of seconds, but it shouldnt take ten minutes. Did you check all caching relevant headers? Are you absolutely sure the file didnt get cached and maybe re-added?
If you are sure, open a support ticket and let them know the result.id of that particular purge request. They might be able to tell you more.
My question was rather directed towards the headers of the resources. But if you have double checked everything a support ticket will be the best approach.
But if it was purged how come it’s still showing the old content?
The page should be showing some new content based on some value from KV. I just checked and the content on the KV is indeed updated and it has a timestamp 1590434851270 which is:
GMT: Monday, May 25, 2020 7:27:31.270 PM
Your time zone: Monday, May 25, 2020 2:27:31.270 PM
So almost 3 hours ago.
The page is 10414 seconds old now so it seems just a couple of minutes before the new content was created and the purging request was mande.
So you are populating the cache from Cloudflare’s database? In that case you better start a JavaScript debugger
At this point I’d probably refrain from opening a ticket, as the purge seems to work and there is some other issue with outdated data getting into the cache. From your description I’d mostly think of some JavaScript issue but that’s something you will need to debug
I’m using the Cache API from a Worker and then using fetch to purge via the REST API.
The HTML is generated in the worker and put into the Cache API using some data from KV.
It’s not really a matter of debugging. I receive a correct response from the API and the result is the same when purging that URL from cloudflare’s dashboard. Sometimes it’s purged, sometimes it’s not.
I think you are right. I’ve counted the seconds more precisely and the content change and the purging happened with a second or two of difference.
I know what is going on.
I’m going to test something and get back to you.
Edit:
Nah, it didn’t work. I have no idea what’s going on.
I’m writing to KV and purging at the same time inside a Promise.all() and thought that could be a problem but in fact the cache should be refreshed when visiting the url later on.
Purging and writing at the same time might not be the best idea. Only write to the cache if you cant find any data in the cache, otherwise only fetch or purge.
Well, if you dont want to post the code it is difficult to say anything. Purging does seem to work, so I’d stick with the assumption of a race condition. You’ll have the debug that code, something is not writing the right data.
The data is being written to KV when it should. I have triple checked it.
The API response from the purge returns OK.
The same problem seems to happen randomly from Cloudflare’s dashboard too.
I’m not writing to the cache API when I’m doing the purge. The cache API is written when the URL is refreshed, just like a Workers Site project does. It doesn’t matter if you refresh 1 minute after the purge or 1 hour.
Where do you think the race condition could be happening?