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.


#11

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?


#12

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 https://developer.mozilla.org/docs/Web/API/URL and define the Cache TTL: https://developers.cloudflare.com/workers/reference/cloudflare-features/#override-cache-ttl


#13

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


#14

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