Created
May 18, 2023 05:02
-
-
Save jasoncartwright/0734776f26bdcce1236370b4ac0c85c0 to your computer and use it in GitHub Desktop.
Cloudflare Worker Purge Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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