[Workers] Document that only standard ports are supported by fetch()
Product
- Workers
- Presumably Pages Functions
Subject Matter
When using a local development environment like Miniflare or the Workers Preview available through Quick Edit in the dashboard, using fetch() on non-standard ports is supported. As an example, fetch("https://example.org:8080") would fetch on port 8080.
However, when published into production, non-standard ports are disregarded. fetch("https://example.org:8080") would fetch on port 443 as that is the default port for the https:// scheme and fetch("http://example.org:8080") would fetch on port 80 as that is the default port for the http:// scheme.
This isn't currently documented and causes a lot of confusion when people are developing for endpoints that are on non-standard ports and since it works locally and in previews, expect it to work when published into production.
Content Location
Uncertainty around this is why I decided to make an issue rather than a PR - where do we put it?
Is it a known issue that'll be fixed, is it a known limitation that won't be changed or do we put it under the fetch or request API docs as an aside?
I ran into this yesterday and @KianNH helped me in the Cloudflare Discord.
A worker fetches data from an API running on AWS. I changed the API from one address to another and was surprised by 10+ second delays and 522 responses when I deployed to production. The original address was like https://api.example.com and the new was like https://feature-branch.other.example.com:8080
Here's a matrix of my experience
| Miniflare | Wrangler | Preview | Workers | |
|---|---|---|---|---|
| no port | ✅ | ✅ | ✅ | ✅ |
:8080 |
✅ | ❌ | ✅ | ❌ |
I suspected that 8080 might be an issue (it was the only real difference I could see between the success & failure), but I didn't see any warnings/errors and both seemed OK based on https://developers.cloudflare.com/workers/runtime-apis/fetch/
I realize this might be more a feature request, but what about adding something in the wrangler logs as well? If something is logged already please let me know because I didn't see anything.
I think we should rather change the behaviour of Miniflare and Preview to match the Workers runtime.
Even if miniflare and preview are changed to match, it's still weird that the fetch documentation page doesn't mention port
I’ve been told that you can fetch on non-standard ports if the request is going to the same zone - which if true, might be worth clarifying?
wrangler now logs a warning as of https://github.com/cloudflare/wrangler2/pull/992
Based on https://github.com/cloudflare/wrangler2/issues/975#issuecomment-1169126913 this is a bug that will be fixed and is tracked in https://github.com/cloudflare/wrangler2/issues/1320
Superseded by https://github.com/cloudflare/cloudflare-docs/issues/5998