Skip to content

Distinguishing empty string vs undefined for options.browserBaseURL #303

@apple12347678

Description

@apple12347678

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions