-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SSR: Axios request fails with 404 when using a proxy #91
Comments
After a few days, I still couldn't find a solution to my problem so I have prepared a demo project. |
I'm having the same issues, tried everything and nothing worked too. |
After a few days and a lot of wasted time, I found out that the environment variable in .env is changing Axios's baseURL. With empty .env:
API_URL="https://domain.tld/api/" in .env
Maybe this helps someone. If anyone knows how env variable changes baseUrl... I'm all ears :) |
The Axios docs in the Nuxt docs mention this: https://axios.nuxtjs.org/options/#baseurl
I'm still working through exactly how all the pieces fit together myself, which is how I found this issue. More examples of usage (and associated Nuxt configs) would go a long way here. |
@beporter exactly this was the problem, found out later... I was obviously very lucky with the name of the .env variable. :) |
stuck on the same issue. |
i'm also facing this issue, then i've resolved it. axios: {
proxy: true,
prefix: '/api/'
},
proxy: {
'/api/': {
target: API_URL,
pathRewrite: { '^/api/': '' }
}
}, i proxied request |
@bagaskarala .env variable named API_URL was causing me problems since it overrides baseURL config of '@nuxtjs/axios'. I prepared a demo project when I was looking for help and updated it with a "solution": @xlanex6 if you also named your .env variable API_URL maybe you are facing the same problem. |
You can also set the base url for axios to avoid the name conflict @juretopolak mentioned and then you can use API_URL in your .env: |
Had the same issue, thanks for all the help here I got my setup working. The In my setup I only defined # .env
API_URL=http://0.0.0.0:8000/
PROXY_API_URL=http://0.0.0.0:3000
HOST=0.0.0.0
PORT=8000 // nuxt.config.js
axios: {
proxy: true
},
proxy: {
'/api/': {
target: process.env.PROXY_API_URL,
pathRewrite: { '^/api/': '' }
},
'/api2/': {
target: 'https://pokeapi.co/api/v2/',
pathRewrite: { '^/api2/': '' }
},
'/api3/': {
target: 'http://shibe.online/api/',
pathRewrite: { '^/api3/': '' }
}
} All requests working nicely on both client and server. The big issue here is that enviroment variables are being used by both modules and is a bit hard to figure out what happens in each case, specially when debuging on server side is awful. |
I'm using Laravel Lumen for the backend, which is on a different domain, so I have to use a proxy to be able to share the cross-origin resource.
Everything works fine on the client-side, but when enabling universal mode and trying to get a prerendered site with the first request products are not rendered because Axios request fails with status code 404.
After enabling Axios debugging I have noticed that 'api' string is still in the requested URL, but it shouldn't be, because of the pathRewrite rule:
When modifying code like this (removed 'api' in the request path and making fetch async), Axios request works, but then of course doesn't on the client.
What can I do to make the request work on the client and server side?
Nuxt.js v2.12.1
The text was updated successfully, but these errors were encountered: