| name | Vite: Configuring a Development Server Proxy |
|---|---|
| description | A procedure for setting up a proxy in vite.config.ts to solve CORS issues during local development. |
| tier | technology |
| layer | |
| schema | procedure |
To avoid Cross-Origin Resource Sharing (CORS) errors during local development, you MUST configure a proxy in your vite.config.ts file to route API requests from the Vite dev server to your backend server.
- Open
vite.config.ts: Locate and open your Vite configuration file. - Add the
serverKey: Inside thedefineConfigobject, add aserverkey if one does not already exist. - Configure the
proxyObject: Within theserverobject, add aproxyobject. For each path you want to proxy (e.g.,/api), create a key. - Set Proxy Options: The value for the path key MUST be an object with at least the following properties:
target: The origin of your backend API server (e.g.,http://localhost:8080).changeOrigin: Set totrue.rewrite: A function that removes the proxy prefix from the path (e.g.,(path) => path.replace(/^\/api/, '')).
- This proxy configuration is for development only and MUST NOT be relied upon for production builds.
- The
targetURL must be the correct address of your running backend server.