In my single page application I'm developing I'm using Vite and in my vite.config.ts
file I have the following proxy:
proxy: {
'/v1': {
target: 'https://127.0.0.1:8080',
changeOrigin: true,
secure: false
}
}
Is there a way to change this target depending on whether it is in the production environment? Something like:
proxy: {
'/v1': {
target: isDev ? 'https://127.0.0.1:8080' : 'https://api.example.com',
changeOrigin: isDev,
secure: !isDev
}
}
That is, in my local environment I want to develop against my local server, such that my fetch API calls like fetch("/v1/get-posts")
get forwarded to https://127.0.0.1:8080/v1/get-posts
, but in my production build (which I create via vite build
), they will instead be forwarded to: https://api.example.com/v1/get-posts
Can this be done, and if so, how?