nuxt / http

Universal HTTP Module for Nuxt.js
https://http.nuxtjs.org
MIT License
229 stars 51 forks source link

change browserBaseUrl default value to '/' #54

Open pimlie opened 5 years ago

pimlie commented 5 years ago

What problem does this feature solve?

Currently the default value of browserBaseUrl is baseUrl which works correctly if you are running your server on localhost as well. But when running Nuxt with HOST=0.0.0.0 then baseUrl and thus browseBaseUrl still fallbacks to localhost. This is fine for baseUrl, but browserBaseUrl shouldnt probably be pointing to localhost.

Related issue: https://github.com/nuxt/press/issues/2 (as fix in nuxtpress I just added browserBaseUrl: '/' as option)

What does the proposed changes look like?

Set default value for browserBaseUrl to /

pi0 commented 5 years ago

Thanks for discovery @pimlie. What do you think if we default browserBaseURL to be relative part of baseURL so if baseURL=http://localhost/api then browserBaseURL will be /api.

pimlie commented 5 years ago

That would probably be even better :)

@pi0 btw, will submit a pr for this in a couple of minutes (unless you are quicker)

pi0 commented 5 years ago

Oops! And we probably need to check for the same origin (which is impossible for default value as it is in the module, not runtime!) If baseURL is external like https://api.example.com or even http://localhost:8080/api it should be preserved as is not / or /api for browserBaseURL.

pi0 commented 5 years ago

It seems explicitly passing browserBaseURL is safer. We can only do this in proxy mode (proxy: true) where API is pointing to the same origin.

pimlie commented 5 years ago

Hmm, probably easier/faster if you have a look at this otherwise cause I am not as fast as you understanding what you mean? Eg isnt checking origin an extra feature?

atinux commented 3 years ago

Checking again the issues, what should we do about this?

pi0 commented 3 years ago

As mentioned above, it is only safe when serving API on same domain using either proxy, serverMiddleware or serverless functions (and also taking into account that fetching another ssr route can cause infinite loop). There was also another nice idea (i guess in axios) using window.location to get browser baseURL.

I think the best would be in a major release for both axios and http, we don't have any defaults (no baseURL options) and and a new option that indicates a relative path on same domain to be marked as api endpoint. (basePath: '/api' and baseDomain: 'https://google.com')