Server.proxy didn't work after npm run build. #8043
Replies: 8 comments 5 replies
-
|
same issue here :( |
Beta Was this translation helpful? Give feedback.
-
|
Any updates on this? |
Beta Was this translation helpful? Give feedback.
-
|
I fixed this issue by axios itselt! Vite does not provide proxy for build, but only for developement! |
Beta Was this translation helpful? Give feedback.
-
|
Unlike create-react-app, Vite does not provide the proxying configurations after the build, but only for development. So, in order to support the build, you have to manually check for the environment variable to see if you're in production or development as follows: for example, this is the baseUrl property in redux-toolkit-query, or fetch(), or even axios(), whatever you're using // for example, this is the baseUrl property in redux-toolkit-query, or fetch(), or even axios(), whatever you're using
import.meta.env.MODE === 'development' ? `api/v1` : HOST + '/v1'Assuming this is your vite.config.js file (as an example, it doesn't have to be exactly like mine): process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
return defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': {
target: process.env.VITE_mainAPI_host,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''), |
Beta Was this translation helpful? Give feedback.
-
|
I had the same issue when I first started my Vite application. I'm using multiple microservice APIs and I also have security for the APIs where my JWT needs see the same origin. This is why I needed proxy-paths for my Vite "DEV" server. When I build, I pull the /dist directory into a Docker build for NGINX. This is what I host as "app:1.0.0" on Kubernetes. I setup a NGINX configuration prior to the Docker build for Production. So, for local dev, I use the I also use a bash script to prepare my application for either environment. The bash script maintains quality once you get it right. https://github.com/virtualyou/vytools/blob/main/bin/setup.sh P.S. Figure it out and then use scripts to remove the hand-work each time you are ready to switch from local to production. Remember, the production /dist is nothing like the local environment. You're taking all the code, assets, styling, etc. and mashing it down into static files. I'm an enterprise Java professional and it still gives me pause. Keep at it, I think Vite has something here. |
Beta Was this translation helpful? Give feedback.
-
|
const cors = require("cors") I tried this code, and it runs fine on both localhost and deployment. However, there's an issue: it works perfectly on Mozilla Firefox, but on Chrome and Edge, it only works on localhost. In deployment, the code breaks. const corsOptions = {
origin: "your website link",
credentials: true,
optionSuccessStatus: 200,
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"],
};
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "tour website link");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
// res.setHeader("Access-Control-Allow-Headers", "X-Requested-With")
next();
});
app.use(cors(corsOptions)); |
Beta Was this translation helpful? Give feedback.
-
|
I'm pretty suprised that this issue hasn't been addressed. By the way, I know that it's not the best way to do it, but it just works 🙏🏽 Your instance should be of this sort: I added an interceptor to the request, so that i can transform the url to whatever shape i want. more about the interceptor... Here's a sample of how you'd use it to make a request: Let me know if this helps. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @hauchu1998 Kindly check #17381 (comment) , i believe this could solve your issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey guys. I'm new to building a front-end application. Here are some questions related to the proxy I encountered. The problem is that I can test my deployed backend server (url like https://my-backend-server.com) twith proxy during the development mode (http://localhost:3030). However, after I deployed the dist folder after
npm run buildwith Apache, I can no longer access the backend server.Here is my packages version:
When I was testing the API in the development mode. I use Axios to send the request. In order to solve to CORS problem, I set proxy in the vite-config.js., which is shown below.
This is how I write axios request.
This is the config I wrote in vite.config.js:
The configs work fine while
npm run dev, I can successfully get what I want from localhost:30303, and the server will show "GET /api/get_token". However, after I build the project and deployed it on the same domain with my backend server and have a url, lets called https://my-frontend-server.com. While I send the "get_token" request, it failed to access. The server will show "GET /api/api/get_token".Is there something wrong setting the proxy configs, or whether the proxy don't work after I build and deploy the site.
Its seem very basic, but I can't solve the problem which is really driving me crazy.
Beta Was this translation helpful? Give feedback.
All reactions