Worker return original response on query param


I’m using cloudflare workers to inject analytics and ad snippets.

This works awesome on html responses. Problem is, it screws with JSON responses which my GitLab instance relies on and the worker is breaking it.

Is there a way to make a worker completely ignore JSON responses or just return the original response?

The request always has ?format=json in it for json responses if that helps

Okay let me rephrase: I only want the worker to trigger on actual html page being requested

URL query parameters are meaningless to a browser, a browser only looks at Content-Type header from origin.

Route patterns may not contain any query parameters

For example, is not a valid route pattern.

Cloudflare HTTP front end doesnt know what ‘?’ character is, you can’t include a ‘?’ in a route. Fix your JS code in the worker to not touch the response body if its html, or you need to separate API/JSON into a separate URL path ‘directory’ from HTML. Then you can use CF routes to never have HTML files go through the worker code. TBH, fix the JS code in your CFW to not manipulate HTML files (or any other unknown file types) instead of figuring out how not to have the worker be called in the first place. Performance/Quota should be the only reason a HTTP request skips your worker with a route pattern, not some kind of bug fix to the customer eyes by skipping the worker.

I’m not worried about the route whatsoever. I want the script to not act if that paramter is present and return the original response.

In addition as I have already said it’s gitlab: I can’t control paths, source, etc and the json response has a ContentType of text/html.

The ONLY clue I have is that parameter in the url. I’m NOT trying to ignore via routes but in the script itself.

I want normal html to go through it but not the json that gitlab labels as html. If it touches the JSON API response, it breaks the page.

You have to show us the code of your worker. Are you really sure the origin server is returning all JSON files as text/html?

I will add that some API servers demand X-requested-with header or a special user agent to prove it’s XHR/fetch and not a navigate. Text/plain for all content unless otherwise whitelisted is a common generic security trend nowadays.