Skip to content
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

Distinguishing empty string vs undefined for options.browserBaseURL #303

apple12347678 opened this issue Oct 28, 2019 — with CMTY · 1 comment · Fixed by titonobre/ or titonobre/


Copy link

apple12347678 commented Oct 28, 2019

What problem does this feature solve?

For some sites that can have multiple frontend URLs (ex., pointing the exact same server) I think it is not a good practice to send AJAX requests such as $axios.get('') - it will cause CORS errors on sites. So I want to request without base URL on clients, for example, $axios.get('/api/sth') - it will append its client-side base URL automatically.

If baseURL is empty, it works fine on both client and server side - every request is fired with no baseURL. But on server side the automatically set baseURL is, so if I'm proxying http requests from another server to localhost:3000 or something, it will throw ECONNREFUSED as my rendering server is not listening on port 80. So what I want to do is to set { baseURL: 'http://localhost:3000', browserBaseURL: '' }.

The problem is, if I set { baseURL: 'http://localhost:3000', browserBaseURL: '' }, browserBaseURL is completely ignored. As empty string is considered falsy, the if statement is not skipped and browserBaseURL is automatically set to baseURL. The exactly same thing happens when I use empty API_URL_BROWSER. I think it's fundamentally different when the variable is an empty string or undefined. If one of axios.options.browserBaseURL or process.env.API_URL_BROWSER is set, the browserBaseURL value should be an empty string, if not, undefined. So could you just change the conditional statement to check if the variable is undefined or not? I hope it will help lots of people struggling with browserBaseURLs just like me... :)

p.s. My breakthrough, or cheat, was to set browserBaseURL: ' ' (string with one whitespace) - it becomes truthy and the space is removed automatically.

The code I want to fix is here:

What does the proposed changes look like?

if (!options.browserBaseURL) => if (typeof options.browserBaseURL === 'undefined') or any assertion that the value is not simply falsy, but exactly undefined

This feature request is available on Nuxt community (#c294)
Copy link

pi0 commented Apr 21, 2020

Hi @apple12347678 thanks for report should be fixed by next release. BTW i would suggest using proxy option instead of external proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants