question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable axios to use current domain as host for client requests

See original GitHub issue

What problem does this feature solve?

Since nuxt generate can’t be used together with nuxt.config.js axios: { proxy: true }, is there a way to configure axios to extract host for browser(client) requests from current browser page domain? If what I’m asking for doesn’t make sense please point me to the right direction on where to look for solution. I’m trying to setup a Docker config with nuxt generate which could be run from both local development machine and production without any adjustments and this proxy limitation really prevents me from reaching this without hardcoding stuff.

If described above makes any sense at all consider the following changes:

What does the proposed changes look like?

Perhaps we could use document.location.host to extract host while in the browser and append our port and prefix to it?

This would mean settings in nuxt.config.js like (inspired by proxy setting)

axios: { 
    reuseBrowserDomain: true
},
browserAxios: {
    port: 1337,
    prefix: '/api'
}

which take precedence over browserBaseURL in case they are set.

Another option would be to allow setting a function to browserBaseURL which is to be executed in browser context.

<div align="right">This feature request is available on Nuxt community (#c343)</div>

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
kkojotcommented, Jun 26, 2020

Thank you @kyrsquir for your quick response. Well, I use apache to host my frontend static files in production and I don’t have any proxy. I also develop in SPA mode.

If you have backend on the same port as frontend, you can simply set axios: { browserBaseURL: "/" } and it’s going to use current browser domain, but if you want to set the port also axios: { port: 8080, browserBaseURL: "/"} then it’s going to use localhost always and it seems to be a bug for me.

My workaround is to use a client-side plugin and set the base URL to axios.

nuxt.config.js

  plugins: [
    "~/plugins/axios.client.js"
  ]

axios.client.js in /plugins

export default function({ $axios }) {
  if (process.client) {
    const host = window.location.hostname;
    $axios.setBaseURL("http://" + host + ":8080");
  }
}

This covers all of my cases: developing locally, running docker on the local machine, running docker in production.

0reactions
farnabazcommented, Feb 8, 2021

@vipulphysiofirst You should mock axios inside jest. checkout #105

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP get request with Axios gets sent with the local host IP at ...
Show activity on this post. To have full control over axios' base URL, you can specify baseURL at AxiosInstance creation: var getBaseUrl =...
Read more >
Request Config | Axios Docs
These are the available config options for making requests. ... baseURL: 'https://some-domain.com/api', // `transformRequest` allows changes to the request ...
Read more >
Complete Guide to Axios HTTP Client - Reflectoring
Axios allows us to attach request and response interceptors to the axios instance where we can perform actions common to multiple APIs. Error ......
Read more >
Options - nuxtjs/axios
WARNING: baseURL and proxy cannot be used at the same time, so when the proxy option is in use, you need to define...
Read more >
How to Perform HTTP Requests with Axios – A Complete Guide
One of the most typical things a developer does is make an HTTP call to an API. An API request can be sent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found