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

Incorrect URL is being built when using relative paths. #161

Closed
chandu opened this issue Mar 3, 2022 · 5 comments
Closed

Incorrect URL is being built when using relative paths. #161

chandu opened this issue Mar 3, 2022 · 5 comments

Comments

@chandu
Copy link

chandu commented Mar 3, 2022

Hi,
In my application I have the backend serve a static page at a specific path /clients/index.html and client api root is /clients/api
Once the /clients/index.html is loaded, I need to get client information using the end point /clients/api/info.

I am using express for the backend and react for the frontend leveraging axios along with the axios-cache-interceptor.

However when using the axios instance (decorated by setupCache) with the baseURL set to : '/clients/api' and the reqeust url set to /info, I am noticing a 404 error in the console.

Page URL: http://localhost:3010/clients/index.html
Clients Information Endpoint: http://localhost:3010/clients/api/info

Working code:

var clientsApi = axios.create({
    baseURL: '/clients/api'
  });

  clientsApi.get('/info').then(a => {
    console.log('Received response: ', a.data);
  }).catch(err => {
    console.log('Error using vanialla axios');
    console.error(err);
  });

**Failing code: **

var clientsApiWithCache = AxiosCacheInterceptor.setupCache(axios.create({
    baseURL: '/clients/api'
  }));

  
  clientsApiWithCache.get('/info').then(a => {
    console.log('Received response using setupCache: ', a.data);
  }).catch(err => {
    console.log('Error using setupCache:');
    console.error(err);
  });

I have debugged the code and see that the issue occurs because of this line:

request.baseURL && (request.baseURL = request.baseURL.replace(SLASHES_REGEX, ''));

Any specific reason for trimming the slashes from the baseURL and url?

The repo @ https://github.com/Chandu/axios-cache-interceptor-relative-base-path-issue can be used to reproduce the issue (I haven't used react in this repo for simpler setup, but the error can be reproduced ).

Screenshot showing the code/error

image

@arthurfiorette
Copy link
Owner

Any specific reason for trimming the slashes from the baseURL and url?

Yes, but not in the current way. :p

These changes should only be kept within the scope of the keyGenerator. Fixing it...

@arthurfiorette
Copy link
Owner

Tested it and everything worked as intended. I'll release 0.9.3 tomorrow.

If everything seems good, can you close this issue?

@chandu
Copy link
Author

chandu commented Mar 4, 2022

Awesome Thanks @arthurfiorette
Will check with the latest version once released.

@arthurfiorette
Copy link
Owner

@chandu v0.9.3

@chandu
Copy link
Author

chandu commented Mar 7, 2022

Thanks @arthurfiorette.
I am closing this issue as I was to verify the fix successfully

@chandu chandu closed this as completed Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants