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)
}
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 and others come looking for an answer and not looking to help you when it’s not needed)?
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?
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.