Closed rachitpant closed 1 year ago
Here is a screenshot of 404 being returned for API requests.
@rachitpant did you ever find a workaround?
Nope.
On Fri, Dec 24, 2021 at 12:05 AM Joe Previte @.***> wrote:
@rachitpant https://github.com/rachitpant did you ever find a workaround?
— Reply to this email directly, view it on GitHub https://github.com/vitejs/vite/issues/6102#issuecomment-1000656439, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALH6363VGEXIWQ7ZTCYK63USP5P5ANCNFSM5J7AESVQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You are receiving this because you were mentioned.Message ID: @.***>
Vite experts any comments ?? At first I thought if there is something wrong in my proxy rewrite config.
proxy: {
'^/api/.*': {
target: SERVER_URL,
changeOrigin: true,
secure: false,
rewrite: path => path.replace(/^\/api/, ''),
ssl: {
pfx: PFX,
passphrase: PFX_PASSPHRASE
}
}
},
But as we can see if the logs above , the vite server is not even attempting to rewrite which is why the requests are not proxied.
vite:spa-fallback Not rewriting GET /api/getUserInfo because the client prefers JSON. +3ms
@patak-dev Please label as bug
@import-brain thanks for the heads up! If you tested and confirmed that it is a bug, you can say so without tagging a particular maintainer, everyone that is watching the repo will get the notification and the closest one will label it :)
@import-brain thanks for the heads up! If you tested and confirmed that it is a bug, you can say so without tagging a particular maintainer, everyone that is watching the repo will get the notification and the closest one will label it :)
Ah, I didn't realize you guys had your notifications set up like that, thanks for the heads up!
I get the error on my end, so it should be the same for you guys.
My bad .. this is not a bug , I had to account for the baseUrl in addition white configuring proxy. Here is the working config. Also this should have been inside the server tag , but i am not sure why i put it outside. Corrected that as well and its working fine.
proxy: {
'^/eventdirect/api/.*': {
target: SERVER_URL,
changeOrigin: true,
rewrite: path => path.replace('/api', ''),
ssl: {
pfx: PFX,
passphrase: PFX_PASSPHRASE
}
}
}
My only suggestion would be that in the doesProxyContextMatchUrl
function , we should probably check if request url
starts with the base
(already passed in the config ) , exclude that from testing the regex or string starts with.
Either that , or if we can't do that , we can improve the server proxy config documentation to tell that the base url has to be explicitly prepended.. ( if there is any ).
My bad .. this is not a bug , I had to account for the baseUrl in addition white configuring proxy. Here is the working config. Also this should have been inside the server tag , but i am not sure why i put it outside. Corrected that as well and its working fine.
proxy: { '^/eventdirect/api/.*': { target: SERVER_URL, changeOrigin: true, rewrite: path => path.replace('/api', ''), ssl: { pfx: PFX, passphrase: PFX_PASSPHRASE } } }
My only suggestion would be that in the
doesProxyContextMatchUrl
function , we should probably check if requesturl
starts with thebase
(already passed in the config ) , exclude that from testing the regex or string starts with.Either that , or if we can't do that , we can improve the server proxy config documentation to tell that the base url has to be explicitly prepended.. ( if there is any ).
Wow, I can't believe I missed that too, we should probably include this in the docs.
If want to use all "/" requests to let it only proxy the API requests,we can use vite plugin api “configureServer”. Combined with http-proxy-middleware to implement proxy filtering.
const { createProxyMiddleware } = require('http-proxy-middleware')
export default function serverProxy() {
return {
name: 'serverProxy',
configureServer(server) {
const filter = function (pathname, req) {
return typeof req.headers['x-ajax'] != 'undefined'
}
server.middlewares.use(
'/',
createProxyMiddleware(filter, {
target: 'http://localhost:3000',
changeOrigin: true
})
)
}
}
}
Hello Everyone ! I have the same issue with server proxy The requests returns 404 when user navigate in a nested route app.url/ => works fine app.url/test/tests => doesn't work Could you please help?
Describe the bug
We have an existing project with a vue (
vue-cli
) front end that we are trying to migrate tovite
, and a backend that only accepts secure https requests. We are using a wildcard certificate for*.companydomain.org
and most developers have updated theiretc/hots
local redirect URL withlocal.companydomain.org
. We do this because the backend only accepts strictly https requests.Everything was working fine in the vue-cli project. However as we are attempting to migrate it to vite , the one and only thing we aren't able to get done is proxying to the backend server ( no API requests are proxied ).
Here is the old
vue.config.js
file.As you can see we are proxying all
"/"
requests to the backend , and the vue.cli dev server was smart enough to only proxy the API requests. The javascript chunk requests were never proxied. This didn't work with vite dev server , so I attempted to add abaseUrl
toaxios
inmain.js
And here is the corresponding vite.config.js // These values are used to configure the vue devServer. They can be overriden using a .env file.
We are trying to make as little change as possible , the only change in the above vite config file ( compared to vue config) is that , the environment variables are changed , they start with a prepend of VUE_ , and 2 , the proxying doesn't work.
There are multiple bugs reported earlier , and I am pretty sure this is a bug, that just hasn't been addressed yet.
Reproduction
Please see the vite config above , can't upload our certificates to reproduce it. As mentioned earlier , the same backend worked for vue-cli , and vue cli config is also uploaded.
System Info
Used Package Manager
yarn
Logs
Validations