Skip to content

Instantly share code, notes, and snippets.

@jasoncartwright
Created May 18, 2023 05:02
Show Gist options
  • Save jasoncartwright/0734776f26bdcce1236370b4ac0c85c0 to your computer and use it in GitHub Desktop.
Save jasoncartwright/0734776f26bdcce1236370b4ac0c85c0 to your computer and use it in GitHub Desktop.
Cloudflare Worker Purge Cache
addEventListener('fetch', event => {
event.respondWith(purgeCache(event.request))
})
async function purgeCache(request) {
const url = new URL(request.url)
urls = new URLSearchParams(url.search).getAll("url")
urls.forEach(function (url, index) {
urls[index] = "https://www.YOURDOMAIN.com" + url
});
let content = {
'files': urls
}
let headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer APIKEY'
}
const init = {
method: 'POST',
headers: headers,
body: JSON.stringify(content)
}
const response = await fetch('https://api.cloudflare.com/client/v4/zones/ZONEID/purge_cache', init)
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment