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

Closed
apple12347678 opened this issue Oct 28, 2019 · 1 comment · Fixed by titonobre/fanweekend.pt#17 or titonobre/fanweekend.pt#19

Comments

@apple12347678
Copy link

What problem does this feature solve?

For some sites that can have multiple frontend URLs (ex. test1.com, test2.org pointing the exact same server) I think it is not a good practice to send AJAX requests such as $axios.get('https://test1.com/api/sth') - it will cause CORS errors on test2.org 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 127.0.0.1:80, 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:

https://github.com/nuxt-community/axios-module/blob/master/lib/module.js#L81

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)
@ghost ghost added the cmty:feature-request label Oct 28, 2019
@pi0 pi0 closed this as completed in 18afe5c Apr 21, 2020
@pi0
Copy link
Member

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
Projects
None yet
2 participants