Hi everyone,
I’m using the following worker in combination with CACHE EVERYTHING to strip tracking query parameters from URLs because they create cache misses which overloads the servers especially in situations when a newsletter is being sent out.
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let url = new URL(request.url)
const regex = new RegExp('fb(?:cl)?id|gclid|msdynttrid|msclkid|utm_[a-z]+', 'i')
Array.from(url.searchParams.keys(), (key) => {
if (key.match(regex)) {
url.searchParams.delete(key)
}
})
let modifiedRequest = new Request(url, request)
return fetch(modifiedRequest)
}
Basically once the parameters are removed, every request apart from the first one should return a HIT no matter the different values assigned to the specific tracking parameter. And that does work in the CF worker sandbox, but not when deployed. With every changed value of a tracking parameter I get a MISS on the first request.
I’m sure I must be missing something here, but can’t seem to find what exactly so any help is highly appreciated.
Best,
Gjoko.