Workers and cache hits

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.

Cloudflare doesn´t cache HTML files by default.

You should probably change return fetch(modifiedRequest) for:

// Force response to be cached for 60 seconds for 200 status
// codes, 1 second for 404, and do not cache 500 errors.

let response = await fetch(modifiedRequest, {
	cf: {
		cacheEverything: true,
		cacheTtlByStatus: { '200-299': 60, 404: 1, '500-599': 0 }
	}
})


// Override origin cache headers for 60 senconds for successful response

if(response.ok)
{
	response = new Response(response.body, response)
	response.headers.set('Cache-Control', 'maxage=60')
}

return response
1 Like

Hi escribeme,

Thanks for proposing a solution. But I am a bit confused. Isn’t this something that should be taken care of automatically by EDGE CACHE TTL? I forgot to mention that in my original question, but I do have it set to 30 minutes.

Pargo.

Now I see you are using Page Rules!

For a moment I thought you were trying to set cache from the worker.

Cache Rules run behind the Worker so, fetch() should be affected by these rules.

Change new Request(url, request) for new Request(url.toString(), request). The first parameter of the Request constructor must be a USVString or another Request Object, not a URL object.