Proxy does not work, both requests to backend and http://localhost:3000/
See original GitHub issueDescribe the bug
https://github.com/vitejs/vite/issues/1014#issue-735504721
Same like this issue.
my vite.config.ts
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()],
server: {
proxy: {
'/api': {
target: 'http://localhost:8000',
changeOrigin: true
}
}
}
})
and the React code
import React, { useState } from 'react'
import logo from './logo.svg'
import './App.css'
function App() {
const [count, setCount] = useState(0)
function request(params:any) {
fetch('/api').then(response => response.json());
}
return (
<div className="App">
<header className="App-header">
<button onClick={request}>
Request
</button>
</header>
</div>
)
}
export default App
Reproduction
Create a blank project with vite, yarn create @vitejs/app
Add vite.config.ts
with proxies from the doc
Issue request to/api
or anything using fetch
Observe that requests are still sent to the dev server http://localhost:8000/
and http://localhost:3000/
in my case.
System Info
vite: ^2.3.0 => 2.3.2 OS: Linux 4.19 Ubuntu 20.04.2 LTS (Focal Fossa) node: v16.0.0 yarn -v: 1.22.10
Logs
vite v2.3.3 dev server running at:
> Local: http://localhost:3000/
> Network: use `--host` to expose
ready in 223ms.
vite:time 0ms / +0ms
vite:spa-fallback Rewriting GET / to /index.html +0ms
vite:time 20ms /index.html +38ms
vite:resolve 1ms /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/client -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/client.js +0ms
vite:resolve 3ms /@vite/client -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/client.js +1ms
vite:resolve 1ms /src/main.tsx -> /home/hz-wsl2/vite-pp/src/main.tsx +3ms
vite:load 4ms [fs] /@vite/client +0ms
vite:resolve 1ms ./env -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/env.js +17ms
vite:resolve 0ms /node_modules/vite/dist/client/env.js -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/env.js +2ms
vite:transform 16ms /@vite/client +0ms
vite:time 31ms /@vite/client +70ms
vite:load 26ms [fs] /src/main.tsx +25ms
vite:load 0ms [plugin] /@react-refresh +229ms
vite:rewrite 1ms [no imports] /@react-refresh +0ms
vite:transform 1ms /@react-refresh +236ms
vite:time 3ms /@react-refresh +235ms
vite:resolve 0ms react -> /home/hz-wsl2/vite-pp/node_modules/.vite/react.js?v=18fa1454&es-interop +244ms
vite:resolve 1ms /node_modules/.vite/react.js?v=18fa1454&es-interop -> /home/hz-wsl2/vite-pp/node_modules/.vite/react.js?v=18fa1454&es-interop +2ms
vite:resolve 0ms react-dom -> /home/hz-wsl2/vite-pp/node_modules/.vite/react-dom.js?v=18fa1454&es-interop +5ms
vite:resolve 0ms /node_modules/.vite/react-dom.js?v=18fa1454&es-interop -> /home/hz-wsl2/vite-pp/node_modules/.vite/react-dom.js?v=18fa1454&es-interop +1ms
vite:resolve 0ms ./index.css -> /home/hz-wsl2/vite-pp/src/index.css +1ms
vite:resolve 0ms /src/index.css -> /home/hz-wsl2/vite-pp/src/index.css +1ms
vite:resolve 1ms ./App -> /home/hz-wsl2/vite-pp/src/App.tsx +2ms
vite:resolve 0ms /src/App.tsx -> /home/hz-wsl2/vite-pp/src/App.tsx +1ms
vite:resolve 0ms /node_modules/.vite/react.js?v=18fa1454 -> /home/hz-wsl2/vite-pp/node_modules/.vite/react.js?v=18fa1454 +1ms
vite:resolve 0ms /node_modules/.vite/react-dom.js?v=18fa1454 -> /home/hz-wsl2/vite-pp/node_modules/.vite/react-dom.js?v=18fa1454 +1ms
vite:transform 243ms /src/main.tsx +21ms
vite:time 282ms /src/main.tsx +22ms
vite:load 3ms [fs] /node_modules/vite/dist/client/env.js +33ms
vite:rewrite 0ms [no imports] node_modules/vite/dist/client/env.js +34ms
vite:transform 2ms /node_modules/vite/dist/client/env.js +14ms
vite:time 10ms /node_modules/vite/dist/client/env.js +13ms
vite:load 3ms [fs] /node_modules/.vite/react.js?v=18fa1454 +13ms
vite:resolve 2ms ./chunk-JF75KDUF.js -> /home/hz-wsl2/vite-pp/node_modules/.vite/chunk-JF75KDUF.js +28ms
vite:resolve 0ms /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 -> /home/hz-wsl2/vite-pp/node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +2ms
vite:transform 7ms /node_modules/.vite/react.js?v=18fa1454 +17ms
vite:time 15ms /node_modules/.vite/react.js?v=18fa1454 +17ms
vite:load 18ms [fs] /node_modules/.vite/react-dom.js?v=18fa1454 +17ms
vite:transform 31ms /node_modules/.vite/react-dom.js?v=18fa1454 +83ms
vite:time 113ms /node_modules/.vite/react-dom.js?v=18fa1454 +102ms
vite:load 4ms [fs] /src/index.css +120ms
vite:load 14ms [fs] /src/App.tsx +13ms
vite:resolve 0ms ./App.css -> /home/hz-wsl2/vite-pp/src/App.css +204ms
vite:resolve 0ms /src/App.css -> /home/hz-wsl2/vite-pp/src/App.css +1ms
vite:hmr [self-accepts] src/App.tsx +0ms
vite:transform 67ms /src/App.tsx +127ms
vite:load 12ms [fs] /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +70ms
vite:rewrite 1ms [no imports] node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +233ms
vite:transform 2ms /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +6ms
vite:time 18ms /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +115ms
vite:time 94ms /src/App.tsx +3ms
vite:hmr [self-accepts] src/index.css +21ms
vite:transform 91ms /src/index.css +10ms
vite:time 106ms /src/index.css +6ms
vite:load 0ms [fs] /src/App.css +124ms
vite:hmr [self-accepts] src/App.css +112ms
vite:transform 1ms /src/App.css +112ms
vite:time 3ms /src/App.css +111ms
vite:time 4ms /src/favicon.svg +298ms
vite:proxy /api -> http://localhost:8000 +0ms
vite:time 17ms /api +10s
Issue Analytics
- State:
- Created 2 years ago
- Reactions:17
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Could not proxy request /pusher/auth from localhost:3000 to ...
Try start both Back end and Front end server concurrently. Just simply run npm start in both back end and front end. Share....
Read more >Proxying API Requests in Development - Create React App
The development server will only attempt to send requests without text/html in its Accept header to the proxy. Conveniently, this avoids CORS ...
Read more >API proxy configuration reference | Apigee Edge
A TargetEndpoint functions as client to a backend service or API -- it sends requests and receives responses. An API proxy needn't have...
Read more >Reverse Proxy Guide - Apache HTTP Server Version 2.4
In such scenarios, httpd itself does not generate or host the data, ... The simplest example proxies all requests ( "/" ) to...
Read more >Access to xmlhttprequest has been blocked by cors policy ...
2. htaccess following code:Well, the problem was that I was using the API ... How to bypass Cross-Origin Request Blocked CORS request did...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m facing the same issue. Too bad there hasn’t been an appropriate answer to solve posted yet. In my case, I’m facing this issue on Windows and Linux (Ubuntu).
It’s difficult to solve without reproducing it and there was many updates so it might be already solved. I’m closing this issue for now but please create a new discussion if it is still happening.