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.

Double base url appended

See original GitHub issue

Using /v1/api via:

window.axios.defaults.baseURL = '/v1/api/'

On error results in ‘/v1/api/v1/api’ call

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
avindracommented, Jun 2, 2019

@riksnelders , others: I noticed in the code posted in the OP has a relative URL, not an absolute URL.

Axios is making a lot of assumptions in their code that the URL MUST be absolute. An easy fix may be to set it to location.origin + '/v1/your/api/base/here'.

After making this change in our app, I noticed this bug went away.


Aside, this is actually a core axios expectation.

This is actually spelled out in the documentation, but its extremely easy to overlook (its in the comment above the baseUrl config in the code:

https://github.com/axios/axios#request-config

I think if enough heads work on the problem, they can clean it up and have baseUrl ALSO support relative paths, as that expectation works cleanly with just core axios. The sloppy handling of config values manifests an issue when trying to add the retry functionality.

2reactions
Spynacommented, Apr 4, 2019

Facing the same issue, and is not related to retry-axios but to axios. If you look at dispatchRequest.js in the axios source code you’ll see near line 25.

  // Support baseURL config
  if (config.baseURL && !isAbsoluteURL(config.url)) {
    config.url = combineURLs(config.baseURL, config.url);
  }

If you don’t use an absolute URL as baseURL, the config.url will be always combined with the baseURL.

This means that… if you do window.axios.defaults.baseURL = '/v1/api/', the first time you execute a request, such as axios.get('/user') the actual url will be [config.baseURL] + [config.url] = '/v1/api/ + user' = '/v1/api/user'.

When that request is retried the code of dispatchRequest.js will be repeated, and now the value of url is already “prepended” with the baseURL. So the config.url will end up with [baseURL] + [url] = '/v1/api/ + /vi/api/user' = '/v1/api/v1/api/user'.

Solutions

  • Use an absolute URL as the baseURL
  • Use a request interceptor to purge the config.URL if the request is being retried
Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios.get repeating base URL twice - Stack Overflow
I'm trying to use axios.get() to access the backend of a site and returns JSON . However the server returns:
Read more >
Full base url appended after base url when reaching page
The first thing you can check is the website's base URL from the database. To do that, log in to the database and...
Read more >
Web Page
Depending on the relative URL convention you use, the Full URL inserts all or part of the base path and appends the relative...
Read more >
URL() - Web APIs - MDN Web Docs - Mozilla
A string representing the base URL to use in cases where url is a relative URL. If not specified, it defaults to undefined...
Read more >
visit - Cypress Documentation
The URL to visit. This value will be appended to the baseUrl if one is configured. Behaves the same as the url argument....
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