localhost:5173 / vite-cors
CORS errors around localhost:5173
The Vite dev server only accepts cross-origin requests from localhost, 127.0.0.1 and ::1 by default — anything else has to be allowed explicitly, and setting cors to true opens it to every website.
Which error you actually have
Two different problems get called "a CORS error", and they need opposite fixes.
- Your app on 5173 calls a backend elsewhere, and the backend rejects it. The backend decides — or you sidestep it with a dev server proxy.
- Something else calls the Vite dev server on 5173 and gets rejected. That is server.cors, and it is the case below.
The default
Requests from localhost, 127.0.0.1 and ::1 are accepted, on any port. Everything else is not. This used to be permissive and was tightened, because a dev server that answers any origin will hand your source code to any page you happen to have open.
{ origin: /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/ }Allowing one more origin
Name it. The option takes the same shape as the cors middleware everyone already knows.
export default defineConfig({
server: {
cors: {
origin: ['https://studio.example.com'],
},
},
})Not true
cors: true accepts every origin. Any page in your browser can then read from your dev server — source, env values, whatever it serves. It is the same class of mistake as allowedHosts: true, and it appears in the same forum answers.
The case that needs no CORS at all
If your frontend calls your own backend, route it through the dev server instead. Same origin, no preflight, no headers to negotiate — and nothing to undo before deploying.
server: {
proxy: { '/api': 'http://localhost:8080' },
}# faq
Questions
Which origins may call the Vite dev server by default?
localhost, 127.0.0.1 and ::1, on any port.
Does server.cors affect calls from my app to a backend?
No. That is the backend’s decision. server.cors only governs requests made to the dev server.
Why did CORS start failing after an upgrade?
The default was tightened for security. Origins that used to be accepted now have to be listed.
Is cors: true acceptable locally?
No — the risk comes from pages open in your own browser, not from the network.
# next