Hi,
I’m using the worker below to strip query strings from the URL, deliver the cached page, then re-attach the query strings to the URL.
The challange that I’m facing is that if I add a general route like https://domain.com/* it will apply to all requests, even local resources which are not required to go through the worker. I’m using WordPress so that will be everything under https://domain.com/wp-content/
So I would like the worker to apply to all the pages, but not the resources loaded.
How would I do that? Thanks!
urlRegex = new RegExp('^(label|refreshce|gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|hsa_[a-z]+|fbid|fbclid|mr:[A-z]+|ref(id|src))$');
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let url = new URL(request.url)
url = await normalizeUrl(url)
let modifiedRequest = new Request(url, request)
console.log(modifiedRequest.url)
return fetch(modifiedRequest)
}
async function normalizeUrl(url) {
let deleteKeys = []
for(var key of url.searchParams.keys()) {
if(key.match(urlRegex)){
deleteKeys.push(key)
}
}
deleteKeys.map(k => url.searchParams.delete(k))
return url
}