Ray
CDCloudflare Developers
•Created by Ray on 1/25/2025 in #general-help
NextJS worker cannot call another worker
Cloudflare Workers do not allow communication between my Workers via HTTP requests within the same domain. I found two possible solutions:
1. Use service bindings.
2. Use different subdomains.
I gave up on option 1 because I couldn’t find a way to access the Cloudflare env. Most of the tutorials focus on how one Worker server calls another, where there’s always a fetch entry point injected into the env. However, Next.js Workers don’t seem to work this way. I found some examples suggesting that you can install a custom entry point on the page to potentially access the env, but since my Next.js is deployed on Workers, I couldn’t find any relevant resources. Eventually, I gave up on this path.
So, I purchased a domain and deployed Next.js to www.mydomain.io and the API to api.mydomain.io. I was able to successfully test the API locally using curl, and the URL https://xxxx.ooo.workers.dev could also fetch data from api.mydomain.io successfully.
However, tragedy struck. Requests from www.mydomain.io to api.mydomain.io failed. The backend didn’t receive anything, and the frontend got error code 522. Could it be that Cloudflare has some default rule that prevents this setup? I am completely lost at this point. Does anyone have any advice on how to resolve this?
2 replies