Open MateuszG opened 5 years ago
I noticed a similar problem.
This won't redirect:
created: function () {
if (this.$store.state.isLoggedIn) {
this.$router.replace('/admin/Dashboard')
}
},
While this does:
created: function () {
if (this.$store.state.isLoggedIn) {
this.$router.replace('/admin/Dashboard')
}
if (this.$store.state.isLoggedIn) {
this.$router.replace('/admin/Dashboard')
}
},
So does this:
created: function () {
console.log(this.$store.state.isLoggedIn)
if (this.$store.state.isLoggedIn) {
this.$router.replace('/admin/Dashboard')
}
},
Is this a race condition?
I removed window.onNuxtReady
in plugins/vuex-persist.js
:
export default ({ store }) => {
new VuexPersistence({
key: 'key'
}).plugin(store)
}
Now it appears to work.
I removed
window.onNuxtReady
inplugins/vuex-persist.js
:export default ({ store }) => { new VuexPersistence({ key: 'key' }).plugin(store) }
Now it appears to work.
you're a lifesaver, been struggling for 6 hours on why it kept losing my states.
This fix https://github.com/championswimmer/vuex-persist/issues/119#issuecomment-504863193 seems to work for Nuxt apps with ssr: false
(previously called: mode: 'spa'
).
However it doesn't seem to work for ssr: true
(previously called mode: 'universal'
). The window.onNuxtReady
seems to be important for SSR apps; not having it leads to errors about the DOM being out of sync.
Docs about the ssr
property: https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-ssr
Along with removing window.onNuxtReady
I had to explicitly specify the storage for it to start working:
export default ({ store }) => {
new VuexPersistence({
key: 'key',
storage: window.localStorage
}).plugin(store)
}
This fix #119 (comment) seems to work for Nuxt apps in SPA mode.
However it doesn't seem to work for Universal mode. The
window.onNuxtReady
seems to be important for Universal SSR apps; not having it leads to errors about the DOM being out of sync.
I have noticed this as well.
At the moment this is what I have got working:
import VuexPersistence from 'vuex-persist'
export default ({ store }) => {
new VuexPersistence({
key: 'state',
storage: window.localStorage,
}).plugin(store)
}
This is the only way I was able to store and correctly have restored data back in components.
Apart of this, I needed to use localStorage, as when using localForage it didn't work.
Having it this way, does correctly store/restore the state, while using localForage instead doesn't.
However, as you correctly say, using this without window.onNuxtReady
does produce a client-side rendered virtual DOM tree is not matching server-rendered content
message (while having window.onNuxtReady
didn't but didn't correctly restore the state as well).
What is the recommended way to setup this for Universal SSR apps?
import { VuexPersistence } from 'vuex-persist'
export default ({ store }) => {
new VuexPersistence({
reducer: state => ({
auth: {
user: state.auth.user
},
userInteraction: {
userInteractions: state.userInteraction.userInteractions,
},
})
}).plugin(store)
}
This solved my issue by saving state instances to vuex persist plugin. Hope it helps 👍🏼
Create nuxt project and add vuex-presist