Can't access Strapi CMS inside worker if custom route is used

We are trying to fetch data from our Strapi CMS inside Cloudflare Workers. But we are facing an issue.

Cloudflare Workers is unable to fetch data from the CMS. response.json() is throwing an error, Unexpected token < in JSON at position 0. So, I thought it’s returning HTML instead of JSON.

And response.text() returns the following HTML:

<!DOCTYPE html><html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Page not found</title>\n</head>\n<body style=\"overflow: hidden; background-color: #2B4B50;\">\n  <div style=\"display: flex; flex-wrap: wrap; justify-content: center; align-items: center; height: 100vh;\">\n  \t<div role=\"main\">\n  \t\t<div style=\"display:flex; justify-content:center;\">\n  \t\t\t<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 252.1 173.4\" width=\"50%\"><path d=\"M202.4,61.9a78.1,78.1,0,0,0-152.8,0,55.9,55.9,0,0,0,6.3,111.5H196.2a55.9,55.9,0,0,0,6.2-111.5Zm-41.8,64.3a8.4,8.4,0,0,1,0,11.9,8.7,8.7,0,0,1-6,2.5,8.5,8.5,0,0,1-5.9-2.5L126,115.4l-22.6,22.7a8.7,8.7,0,0,1-6,2.5,8.5,8.5,0,0,1-5.9-2.5,8.4,8.4,0,0,1,0-11.9l22.7-22.7L91.5,80.9A8.4,8.4,0,0,1,103.4,69L126,91.7,148.7,69a8.4,8.4,0,0,1,11.9,11.9l-22.7,22.6Z\" transform=\"translate(0 0)\" style=\"fill:red\"></path></svg>\n  \t\t</div>\n  \t<h1 style=\"width: 100%; text-align: center; color: #fff;\">This page can not be found.</h1>\n  \t</div>\n  </div>\n</body></html>

IDK why the worker is not able to find the URL. The exact same code works everywhere I tried.

The code is private, so it’s not possible to share the code. But I can the share the structure of the code:

  const query = `
    {
       # a graphql query 
    }
  `

  const response = await fetch('https://cms.example.com/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      query,
    }),
  })

  const { data } = await response.json()

I have tried converting the GraphQL query to REST API calls. But it returns the exact same HTML response.

The CMS is also running on the standard port, 443. So, it’s not an issue. I have checked if adding :443 after the URL solves the issue. But it throws the same error.

Any help would be much appreciated.

I just tested and the code is working fine in the Dashboard > Workers > Overview > Quick Edit screen too.

We have identified the problem’s root cause; it’s the custom route we have used to trigger the worker. When we send requests to the worker’s default route (or a custom domain), no errors are returned; however, when we send requests to the custom route, Cloudflare receives an HTML response that reads, This page cannot be found. Why might this happen?

We are waiting for your response. Thanks in advance.

I’ll try to create an example worker to better illustrate this issue.

The problem is now more clear to us and we were able to identify the actual bug. We couldn’t realize that the HTML 404 response the worker was receiving when attempting to access the CMS was actually the HTML for the 404 Page of our primary website.

We actually have a Page Rule enabled that redirects traffic from all the subdomains to the main domain. Our CMS is hosted at https://cms.dilmahtea.me/. When we try to access an API endpoint, such as https://cms.dilmahtea.me/api/i18n/locales, the worker is getting redirected to https://dilmahtea.me/api/i18n/locales, but this route doesn’t exist, so the worker is receiving that 404 response.

But it’s obviously an issue. The rule shouldn’t redirect the worker because the URL already exists. And only the custom routes are impacted. The worker’s default route or custom domains are unaffected by this problem.

Even though it took us some time to figure out the issue, it’s not the first time we are facing it. We have previously encountered this problem many times with CF Workers. However, since the issue was with the worker URL itself, we were able to find the issue right away.

But those issues affected both custom domains and custom routes. However, they weren’t permanent like this problem. They disappeared after disabling the custom routes or domains for a while, possibly 24 hours.

We suspect that there may be an internal bug causing this issue. The sooner Cloudflare triages and resolves this issue, the better for everyone.

Example:
Default Route: https://custom-route-issue.dilmah.workers.dev/custom-route
Custom Route: Page not found - 404 - Dilmah Tea Europe