cloudflare-docs icon indicating copy to clipboard operation
cloudflare-docs copied to clipboard

[Workers] Document that only standard ports are supported by fetch()

Open KianNH opened this issue 3 years ago • 6 comments

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?

KianNH avatar May 05 '22 13:05 KianNH

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.

jfsiii avatar May 05 '22 13:05 jfsiii

I think we should rather change the behaviour of Miniflare and Preview to match the Workers runtime.

eidam avatar May 12 '22 14:05 eidam

Even if miniflare and preview are changed to match, it's still weird that the fetch documentation page doesn't mention port

koeninger avatar May 12 '22 15:05 koeninger

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?

KianNH avatar May 17 '22 23:05 KianNH

wrangler now logs a warning as of https://github.com/cloudflare/wrangler2/pull/992

jfsiii avatar Jun 28 '22 13:06 jfsiii

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

jfsiii avatar Jun 29 '22 11:06 jfsiii

Superseded by https://github.com/cloudflare/cloudflare-docs/issues/5998

KianNH avatar Nov 13 '22 17:11 KianNH