Understanding worker routing

Hello. I have silly question about workers and routing. How will routing work in this example:

I have domain name on cloudflare, lets say it’s https://test.example.com/ that is proxied do server x.x.x.x

Now I add worker with the following code:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});
        
async function handleRequest(request) {
  let response = await fetch(request);
  response = new Response(response.body, response);
  return response;
}

After adding worker, I add route: https://test.example.com/*

As you can see, the only thing the worker is doing is to “proxy” requests.
My question is, what will be called and in what order when I open http://test.example.com/ in my browser?

I can see there posibilities:

  1. Request will be proxied by my DNS settings first and worker will be ignored.
  2. Request will go to worker, which will call the same address again, but calling it will again hit worker. This will create infinite loop of worker calling itself
  3. Request will go to worker, but worker routing is smart enough so it will not call itself again, but request from worker will go through Cloudflare DNS

Of course I want third option to happen as I want to create middleware for my address, but I still want it to go through cache, firewall etc.

To use workers, you have to go to your current domain provider, and somewhere in the options, value the server names to those of cloudflare. This way, all requests will go to cloudflare and your worker will work.

Thank you Denis for your reply.

Maybe I clarify things a bit:

I already delegated my domain to Cloudflare. DNS for my domain are pointing to Cloudflare name servers. All traffic is going through Cloudflare, and Cloudflare is proxing all the traffic to my origin server. When I put my domain address in web browser, I see cached version of my web site served by Cloudflare. So everytings works.

My question is what’s gonna happen when I’ll attach worker to the same domain. When I add route exactly the same as my domain that is currently configured in Cloudflare DNS page.

Let’s say I have domain https://test.example.com/

In Cloudflare DNS settings I have https://test.example.com/ proxied and pointing to my origin server.

Now I want to add worker and attach it to route exactly the same address - https://test.example.com/ - and wonder what will happend. The code and possibilities that go through my mind are in my previous post.

Ok your question is about priority.

I would say you define a specific worker so it overload the default behavior, but I didn’t try and the best is to test by adding a console.log(‘test’) to your worker, do some hits on your main url, and look after the result of the command “wrangler tail” to see if there is anything logged.