Error: Could not proxy request: TypeError: fetch failed

I recently upgraded to wrangler 2.12.0 and started getting [pages:err] Error: Could not proxy request: TypeError: fetch failed error all of a sudden. I thought this is an issue with the new version and so downgraded to the version (2.9.1) which I was using earlier but issue still persists. Changed the port of vite dev server to make sure that the port number is not in use already but issue still remains. What could be the reason behind this issue?
24 Replies
Torque
Torque16mo ago
Updated to 2.12.1 but still same error. Any idea why this is happening?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
I was facing this issue with a vuejs application containing pages functions and so I tried it with another application without any function which does not have fetch anywhere in the code and it is still throwing same error. How can I enable detailed logging to see what is going wrong?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
no that is not the case. I am using wrangler pages dev --port 8888 -- yarn preview command to run the vite preview server which starts the server and I can see it in the console. I am able to access the application directly after executing above command.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
No. but it is able to detect the correct port automatically.
[proxy]: $ vite preview --host 0.0.0.0 --port 8090

[proxy]: Comamnd : serve, Mode: production

[proxy]: ➜ Local: http://localhost:8090/

[proxy]: ➜ Network: http://192.168.56.1:8090/
➜ Network: http://192.168.0.218:8090/

No Instance(s) Available.
Automatically determined the proxy port to be 8090.
[proxy]: $ vite preview --host 0.0.0.0 --port 8090

[proxy]: Comamnd : serve, Mode: production

[proxy]: ➜ Local: http://localhost:8090/

[proxy]: ➜ Network: http://192.168.56.1:8090/
➜ Network: http://192.168.0.218:8090/

No Instance(s) Available.
Automatically determined the proxy port to be 8090.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
yes One reason could be that vite preview server is listening on localhost instead of 127.0.0.1.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
Because of command alias in package.json file -
"scripts": {
"install-custom": "yarn install --ignore-engines --ignore-platform",
"build": "vite build",
"preview": "vite preview --host 0.0.0.0 --port 8090",
"wrangler-dev": "wrangler pages dev --port 8888 -- yarn preview"
},
"scripts": {
"install-custom": "yarn install --ignore-engines --ignore-platform",
"build": "vite build",
"preview": "vite preview --host 0.0.0.0 --port 8090",
"wrangler-dev": "wrangler pages dev --port 8888 -- yarn preview"
},
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
This application proxies request to another application running on port 8091 which was not working with localhost. It started working after changing localhost to 127.0.0.1. I have a feeling that wrangler is failing due to same issue.
preview: {
proxy: {
'/microapp-vue-components': {
// Don't use localhost here as it will not work
target: 'http://127.0.0.1:8091',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/microapp-vue-components/, '')
}
}
},
preview: {
proxy: {
'/microapp-vue-components': {
// Don't use localhost here as it will not work
target: 'http://127.0.0.1:8091',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/microapp-vue-components/, '')
}
}
},
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
let me try
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
Mine is a windows machine and it does not have nc command installed.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
I spinned up a simple nodejs application which is working fine directly as well as through wrangler. So it seems to be an issue with the vite preview server which is listening on localhost instead of 127.0.0.1. Is there any way to specify proxy host in wrangler?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
did that and still same error. I don't think it is going to work unless vite preview server listens on 127.0.0.1 or there is a way in wrangler to specify proxy host. Even though I am starting vite preview server with --host 0.0.0.0 flag, it is not listening on 127.0.0.1 unlike wrangler dev server which is listening on all the IPs.
[mf:wrn] Service bindings are experimental. There may be breaking changes in the future.
[mf:inf] Worker reloaded! (322B)
[mf:inf] Listening on 0.0.0.0:8888
[mf:inf] - http://192.168.56.1:8888
[mf:inf] - http://192.168.0.218:8888
[mf:inf] - http://127.0.0.1:8888
[mf:inf] Updated `Request.cf` object cache!
[mf:err] Error: Could not proxy request: TypeError: fetch failed
[mf:wrn] Service bindings are experimental. There may be breaking changes in the future.
[mf:inf] Worker reloaded! (322B)
[mf:inf] Listening on 0.0.0.0:8888
[mf:inf] - http://192.168.56.1:8888
[mf:inf] - http://192.168.0.218:8888
[mf:inf] - http://127.0.0.1:8888
[mf:inf] Updated `Request.cf` object cache!
[mf:err] Error: Could not proxy request: TypeError: fetch failed
Host name is hardcoded in wrangler and so it seems we can't specify hostname -
url.host = `localhost:${options.proxyPort}`;
url.host = `localhost:${options.proxyPort}`;
Anyway thanks for all your help.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Torque
Torque16mo ago
sorry didn't get you. what you mean by own server?
Torque
Torque16mo ago
Reported this issue in wrangler cli repo - https://github.com/cloudflare/workers-sdk/issues/2872
GitHub
🐛 BUG: Wrangler is using localhost instead of 127.0.0.1 to proxy re...
Which Cloudflare product(s) does this pertain to? Wrangler What version of Wrangler are you using? 2.12.1 What operating system are you using? Windows Describe the Bug I am using wrangler to proxy ...