'Initate Session on page load

I have a requirement where I need to set Global Axios header and refresh session(Calling multiple Api's). In vuejs we would have a App.vue which is the entry point for our application we would do these types of processing in that.

App.vue

export default {
 async created(){
   await this.$store.dispatch('initateSession');
   }
}

The benefits of doing this is that this is called only once(not on every route change), and on every page load, I am looking to implement the same functionlity in Nuxt.

Now as we dont have any App.vue in case of Nuxt one solution that i could think off is to create a middleware for this , which can work but that will be called on every route change I dont want that, does anyone know any workaround for it?



Solution 1:[1]

Instead of middleware you can create a plugin:

plugins/session.js:

window.onNuxtReady(() => {
  // Do something
})

And then add it to the plugins section inside nuxt.config.js.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mani Mirjavadi