Worker conditional route

Hi,

I’m using the worker below to strip query strings from the URL, deliver the cached page, then re-attach the query strings to the URL.

The challange that I’m facing is that if I add a general route like https://domain.com/* it will apply to all requests, even local resources which are not required to go through the worker. I’m using WordPress so that will be everything under https://domain.com/wp-content/

So I would like the worker to apply to all the pages, but not the resources loaded.
How would I do that? Thanks!

urlRegex = new RegExp('^(label|refreshce|gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|hsa_[a-z]+|fbid|fbclid|mr:[A-z]+|ref(id|src))$');


addEventListener('fetch', event => {
    event.passThroughOnException()
    event.respondWith(handleRequest(event.request))
})
  
  
async function handleRequest(request) {
    
    let url = new URL(request.url)
    
    url = await normalizeUrl(url)
    
    let modifiedRequest = new Request(url, request)
  
console.log(modifiedRequest.url)

    return fetch(modifiedRequest)
}

async function normalizeUrl(url) {
    let deleteKeys = []
    
    for(var key of url.searchParams.keys()) { 
        if(key.match(urlRegex)){
            deleteKeys.push(key)
        }
    }

    deleteKeys.map(k => url.searchParams.delete(k))
    return url
}

In terms of billing, there’s no way to exclude a worker from a specific prefix or path, sadly. The only thing you could do is check if the URL is correct inside the worker, but that will still use your request quota/count towards billing.

I don’t mind that the resources go through the filters, I mind they are counted towards billing.

Any idea for a more efficient delivery of cached pages when URL query string are involved?

Hey @catalin.oanca - you should be able to visit the Workers UI for your domain and create a specific route pattern with no script attached. Try something like the below screenshot:

Note: you won’t be able to get the specificity here of running arbitrary JS and saying “Run Workers for these things, but not these things”, but if you have a general route pattern you’d like to exempt from Workers, that approach would handle it.

Thanks @signalnerve for the update.
I’ve tried that and added a console.log in the worker that outputs the URL (you can see it in the original post) and it still outputs for wp-content resources.

This is what I have in routes: http://prntscr.com/ojqh8h
Does the order matter?

I’ll keep it on for a few days to see if it makes any difference.

@signalnerve , I tried your suggest and unfortunatelly it still counts all requests, even local ones.
Is there anything I’m doing wrong? Anything else I can try?