Why nuxtServerInit doesn't run on other pages, except index page.

siberiadev
5
siberiadev
commented a year ago

Hello everyone.
I have an ssr web site on nuxt. I use passport.js to auth my users.
When user logged in, I have an access to his data from req.user.
Then I set user profile in my nuxtServerInit (Vuex store/index.js) in code below:

nuxtServerInit({ commit }, { req }) {        
        commit('SET_HOST', req.headers.host)
        console.log('nuxt server init');
        commit('SET_USER', req.user)    
    },

It works fine on the index page, every time I refresh the page nuxt run this code.
But when I go to other page (/foo, /bar) nuxt doesn't run nuxtServerInit, so when I refresh the page like mysite.com/admin, I lose all user data.

The issue is why nuxt run nuxtServerInit only on index page, and never run it on other pages?

If it's a normal behavior of nuxt, so can you advice me where I should set my user data from req.user?

0
siberiadev
5
siberiadev
commented a year ago

The problem was I set some of the pages to spa mode in my serverMiddleware.
When i remove this pages from spa mode it works fine.

0
sersoong
0
sersoong
commented 7 months ago

So,How do you resolve it? @siberiadev

0
Informations
Question โ€ข Unresolved
#c9132 - Created a year ago