Ignore GET parameters with cache everything


#1

Hi, I’m wondering if it’s possible with Cloudflare Workers to ignore certain GET parameters with cache everything.

For example, the following three pages would all be in the same cache, with mkt_tok being ignored:

https://dynamiccatholic.com/bestlentever/lent-reflections-2018/the-happiness-myth
https://dynamiccatholic.com/bestlentever/lent-reflections-2018/the-happiness-myth/?mkt_tok=GGGZZGG
https://dynamiccatholic.com/bestlentever/lent-reflections-2018/the-happiness-myth/?mkt_tok=ZZZFFFZF

#3

If I got the question right the solution is to basically rewrite the URL without the parameters. Then perform the request to the new URL which would pass through the cache (the worker is in front of the cache).

EDIT

addEventListener('fetch', event => {
  event.respondWith(fetchAndChange(event.request)
})
    
async function fetchAndChange(request) {
  var url = new URL(request.url)

  // modify URL as you wish
  // newURL is the modified URL

  var newRequest = new Request(newURL, request)
  return fetch(newRequest)
}

#4

You can set your Caching Level to Ignore Query String.


#5

Awesome, thanks for the help! I’ve got it working now.


#6

This wouldn’t work because the “ignore query string” cache level only applies to images, etc. It wouldn’t apply to real pages. Matteo’s solution worked for me.


#7

If it helped and the problem was solved, would you mind accepting it as a solution (so I am happy :blush: and others come looking for an answer and not looking to help you when it’s not needed)?


#8

Yes, I didn’t realize you could do that here. Done!


#9

Here’s my goal:

I created this worker:

addEventListener('fetch', event => {
  event.respondWith(handle(event.request));
})

async function handle(request) {
  let newUrl = myRewriteFunction(request.url)
  let response = await fetch(newUrl, request)

  // Copy the response so that we can modify headers.
  response = new Response(response.body, response)

  // Shove our rewritten URL into a header to find out what it was.
  response.headers.set("X-Debug", newUrl)

  return response
}

function myRewriteFunction(url) {
  n = url.indexOf('?');
  url = url.substring(0, n != -1 ? n : url.length);
  return url
}

This still seems to “miss” cache on each initial request for the same base URL with a different token. Any suggestions?


#10

@matteo’s worker based solution will do the job here.

I want to highlight for the other readers that @hklcf’s suggestion to set the Caching Level to Ignore Query String will also achieve the same outcome.

You can also use Page Rules to target this setting to only some parts of your website.