Axios-module: Current route.fullPath is out of date when called inside of an interceptor

Created on 11 Oct 2019  路  3Comments  路  Source: nuxt-community/axios-module

Version

v5.6.0

Steps to reproduce

Make an axios API call after a route change after the initial SSR app load.

export default function ({ $axios, store, route, redirect }) {
  $axios.onError((error) => {
    if (error.response.status === 401) {
      store.commit('misc/setRedirect', route.fullPath);

      redirect('/sign-in');
    }
  });
}

What is expected ?

If I load the page on '/', then navigate to '/one' and make an axios request, the onError interceptor will return '/' via route.fullPath.

What is actually happening?

It should return '/one'.

Additional comments?

I was able to find a workaround by using app instead of route, but I wanted to report the bug.

Workaround: app.router.currentRoute.fullPath

This bug report is available on Nuxt community (#c291)
bug-report

Most helpful comment

I got the same issue in my axios plugin:

export default function({ $axios, app, route }) {
  $axios.onError(error => {
    if (error.response.status === 401) {
      console.log("Axios 401: ", app.router.currentRoute.path, route.path)
    }
  })
}

route.path seems to be set to the route when the plugin was initialized, app.router.currentRoute.path seems correct

All 3 comments

Hi @chrislentz I could not reproduce
Could you provide a repository?

I got the same issue in my axios plugin:

export default function({ $axios, app, route }) {
  $axios.onError(error => {
    if (error.response.status === 401) {
      console.log("Axios 401: ", app.router.currentRoute.path, route.path)
    }
  })
}

route.path seems to be set to the route when the plugin was initialized, app.router.currentRoute.path seems correct

This is not a bug. When destructing an object references are kept. @schellenbergk solution for app.router.currentRoute.path seems best :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lsbyerley picture lsbyerley  路  4Comments

lyzs90 picture lyzs90  路  4Comments

kaboume picture kaboume  路  4Comments

artmarydotir picture artmarydotir  路  4Comments

seanwash picture seanwash  路  6Comments