Nuxt.js data caching problem

problem description

when you use Nuxt.js to develop a server-side rendering project, refreshing the web page will cause data saved to vuex store to be lost.

if it is token , then I will save it to cookie , get token in cookie when nuxtServerInit , and reset it to store .

but if I have other data to cache, such as product categories, etc., which I don"t want to set to cookie , but rather want to save to sessionStorage , what should I do if I extract data from sessionStorage to store after the page is loaded? There may be a lot of cached data, so you want to find a place to configure it separately, instead of writing it once for every page, where should it be written? Or can only be done with cookie, like token? )

Dec.21,2021

in the first screen, the acquired data exists in window.__NUXT__ , and then execute store.replaceState (window.__NUXT__) to initial state , and then mix on the client. You can consider merging the data in sessionStorage into window.__NUXT__


before executing store.replaceState (window.__NUXT__) .

tune mutation in layout

const info = sessionStorage.getItem('user')
this.$store.commit('UPDATE_USER', info)
Menu