Worker on subpath is not triggered

Hi,

I have 2 site-workers.
One is mapped to https://domain.io/*
and the other to https://domain.io/docs/*

Per the documentation, requests to /docs supposed to trigger the second one since it’s more specific.
However, what happens is that requets to https://domain.io/docs/ do get to the second worker, but any asset within that folder, such as /docs/main.css goes to the first worker.

When I access the worker via its worker.dev subdomain everything works fine.

I also tried to change the mapping to https://domain.io/docs* but to no avail.

Thanks for any help

Would removing the wildcard * (apex) help so it stays https://domain.io/? :thinking:

Is the /* first and /docs/* the second?
What happens if you “switch” their order by removing first /* then re-adding it so it’s below the /docs/* on the list?

I guess due to some specific reason, you cannot use one for them and just re-route …

Thanks for the suggestions.

I don’t think there’s a meaning to the order of the routing in the list. If there were it would be a very fragile configuration because it might just break because of the order your deployment scripts run.
The documentation says that routing is set by most-specific-first.

The reason that I have 2 separate workers is that each one is a separate vuejs pwa application with different build scripts. For example the docs have to build search indices and markdown-to-html, while the site itself is more simple. The easiest way to achieve this is by having 2 separate services. The reason I want the docs mapped to /docs rather than docs.domain.io is because it will do better for seo.

1 Like

OK, I’ve found the issue.
keywords for anyone looking for this issue: how to run Cloudflare worker site on a sub folder

The original issue was that I should have mapped the docs worker to https://domain.io/docs* and not https://domain.io/docs/*, otherwise the root document `indeed goes to the main site’s worker.

The second issue was with assets. All documentation and articles that I’ve found only tells you how to route an origin site that is hosted elsewhere to a subpath, but I couldn’t find any doc about the worker itself being the site. When this is the case, it is not enough to set the correct path. You also need to change the key under which assets are stored in the kv store. This is because site-workers stores them without the /docs in the path, but the request itself comes with this prefix. So when the browser asks for https://domain.io/docs/main.css the worker script cannot find it.

To fix that what I had to do edit workers-site/index.js file like this:

//this line exists by default
req = mapRequestToAsset(req)

//this block was added
if (req.url.startsWith('https://domain.io/docs/')) {
  req = new Request(req.url.replace('https://domain.io/docs/', 'https://domain.io/'))
}

I guess there could be a more performant way to achieve this. If you know about one please let me know.

It was hard to troubleshoot this because:

  1. In Cloudflare realtime logs the response status appears as OK although in the browser network tab it appears as 500. I think the real-time logger should reflect the http status returned by the worker, not the fact that the worker itself ran
  2. In chrome, although you can see the status, you can’t see the response body. Only in firefox I was able to see the body was something like could not find file docs/main.css which made me think about the problem.
1 Like

Thank you for your feedback information.

I am happy to hear this :slight_smile:
Really good to know what to check and look for, and thanks for sharing :+1:

1 Like