question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

store from localStorage in nuxt is restored after mounted()

See original GitHub issue

I’m not sure if this is the expected behavior but it certainly makes things more complicated. I thought the state would be restored if localStorage is used. But it turns out that’s not the case.

Here’s how I set it up.

// ~/plugins/vuex-persist.js
import VuexPersistence from 'vuex-persist'
 
export default ({ store }) => {
  window.onNuxtReady(() => {
    new VuexPersistence({
      storage: window.localStorage,
      reducer: (state) => ({
        settings: {
          ...state.settings,
        }, 
    }).plugin(store);
  });
}

And when I log the state.settings object in a mounted() hook, I get the store defaults instead of the values from localStorage. Also the store.restored value seems to be always undefined. So I came up with my own work around:

// ~/store/settings.js
const state = {
  // ...
  restored: false,
}

// ~/nuxt.config.js
{
// ...
plugins: [
    { src: '~/plugins/vuex-persist', ssr: false },
  ],
}

// ~/plugins/vuex-persist.js
import VuexPersistence from 'vuex-persist'
 
export default ({ store }) => {
  window.onNuxtReady(() => {
    new VuexPersistence({
      storage: window.localStorage,
      reducer: (state) => ({
        settings: {
          ...state.settings,
          restored: true,
        }, 
    }).plugin(store);
  });
}

// pages/test.vue
export default {
  watch:{
    restored(val){
      if(val === true){
        // do things
      }
    }
  },
  computed:{
    restored(){
      return this.$store.state.settings.restored;
    }
  }
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
kreig303commented, Dec 12, 2020

@snakemastr when you say “global” what do you mean?

fwiw if you meant the store persists between applications in the same session, then use the key parameter. we had the same issue.

edit: my instructions edited to include this fact.

1reaction
kreig303commented, Dec 12, 2020

we had to implement it a different way from the instructions in the readme to get it work for us.

no promises! ymmv! etc


instead of using vuex-persist as a nuxt plugin and adding the suggested file into the ./plugins directory and the config, we just imported it directly in our store file, like so:

...
import foo from 'vuex-persist'
...
const bar = new foo({
  key: 'unique-key-if-needed'
  ...
})
...
export const plugins = [bar.plugin]

We also still do the transpile step in the nuxt config, i.e.

module.exports = {
  ...
  build: {
    transpile: ['vuex-persist'], // this module is normally esm only
    ...
  }
  ...
}

It’s possible that the plugin author’s instructions are simply dated. Things change a lot in nuxt minor versions!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I access localStorage in store of NuxtJs?
When you use SSR you don't have access to the browser storage. To do a workaround, you can dispatch an action on the...
Read more >
[Solved]-Middleware executing before Vuex Store restore from ...
Now nuxt has a function called nuxtServerInit() in vuex store index file. You should use it to retrieve the token from request and...
Read more >
Create a shopping cart with Nuxt 3 | EnterFlash Blog
During this tutorial, we'll be using Nuxt 3 to create a persistent shopping cart that saves its information in local storage to persist...
Read more >
vuex-persist - Bountysource
Create nuxt project and add vuex-presist. store/index.js. import Vue from 'vue'; export const state = () => ({ preview: {}, templateR: false })...
Read more >
nuxt-vuex-localstorage - npm Package Health Analysis - Snyk
Since local storage updates as store changes, server side function such as fetch, asyncData can be used before components are mounted. <script> export...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found