Ignore GET parameters with cache everything

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

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)
}
1 Like

You can set your Caching Level to Ignore Query String.

1 Like

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

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.

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)?

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

1 Like

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?

@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.

https://support.cloudflare.com/hc/en-us/articles/200172266-What-do-the-custom-caching-options-mean-in-Page-Rules-

2 Likes

According to your documentation (What do the custom caching options mean in Page Rules?):

Ignore query string: Caches static content that has a query string and treats it as one file

In this case (and mine) the urls do not point to what you list as static content (.jpg, .pdf etc), so would the pages still be cached?

Hi @Ozy,

That’s a good point. In Page Rules the option to Ignore Query String and Cache Everything are distinct as they are separate options in the same setting.

This would prevent you from enabling both Ignore Query String and Cache Everything for extension not cached by default.

In this case a Worker is a good option to enable both behaviours, you can strip the query string from the request url using the URL API URL - Web APIs | MDN and define the Cache TTL: https://developers.cloudflare.com/workers/reference/Cloudflare-features/#override-cache-ttl

We’re you able to get a solution for cache everything and ignore query string w/o cache miss?

2 Likes

Works for me! I use a slightly modified version of the worker examples provided in this thread.