Worker - only run on urls with query parameter

Hi,

I’m working on a worker that replaces elements with kv stored data, but I’m having some issues with the routing.

Is it possible to only load the worker when there is for example &adid= in the url? When I tried setting this up via the toml file I got a error stating this wasn’t allowed.

And we only need it on some specific pages but the urls are quite dynamic, like prefix/// and we also have urls like prefix///*/ which should remain unaffected. Is that possible?

And last question, if the targeting to query parameter isn’t possible and the routing can’t be specific enough to only target the proper pages, how do I return the right content via the worker? This content can be anything from a PDF to CSS, so I can’t let everything go through HTMLRewriter?

Thanks!

Based on the following error message I’d assume the worker path is actually not supposed to contain a query string, hence one wouldnt be able to filter for query string parameters.

{
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 10022,
      "message": "Route pattern should not have query parameters example.com/?*"
    }
  ],
  "messages": []
}

That question is not very clear. Your worker needs to decide what it returns and then simply returns whatever it decided.

Yeah exactly, that’s the error I’m getting too. I was wondering if there was a work around.

Yeah, but what is the proper way to do this in the worker?

I saw an example that checks for the content-type but I have no idea how I can respond with the right content?

I want to do something like this:

if(contenttype === ‘text/html’ && url.searchParams.get(“var”) is set’)
{
modify
}
else
{
do nothing - normal output.
}

But I can’t find in the docs how to do this.

Not as far as I can tell, but you might want to clarify this via a support ticket.

There is no proper way. Based on your decision what you return you return that along with an appropriate mime type. But thats it. Whats exactly is unclear?

The worker simply takes the request and performs whatever action you tell it to do.