Vuex module reset state for SSR - vue.js

I am using vuejs with vuex as state management.While doing SSR,I am creating fresh instance of app with fresh instance of router and store,
to avoid Stateful Singletons in server.More in this can be found here:https://ssr.vuejs.org/guide/structure.html#avoid-stateful-singletons.
The problem is my modules state in vuex store don't get reset.I didn't find a suitable way to create a fresh instance of a module.
I don't want to use new vmcontext in node server,also don't want to reset state of same module everytime for SSR.

Related

Using nuxt-socket-io with Pinia

I want to write my application like a chat on Nuxt.Js 3 and use Pinia as a store. I found the nuxt-socket-io module - the easiest way for me to implement my chat. I read full docs, but only vuex usage and some kind of iox were mentioned there. Iox saves all the data in the state, but I need to call mutation/action from store when emitting an event on the server side. Question: how can I link nuxt-socket-io events with Pinia actions?
I tried to declare Pinia actions in vuex configuration in io.config. Then i tried to pass global $ioState value to Pinia state.

Why use Vuex in SSR?

My question is, what's the point of setting up Vuex for the server when the state will be overwritten when the client side hydration takes place?
I have some data (Helm env variables) that I want to store in the vuex store for later use.
These variables is only available to me on the server, so I started trying to add them to the store in my createApp script when running on the server.
The store state however is reset when the client side hydration kicks in, so no env variables left.
Google told me I should use like window.INITIAL_DATA to set the state again on the client:
store.replaceState(window.INITIAL_DATA)
But if have to use the window object to pass store data to the client, what's the point of using Vuex on the server at all?
Isn't it better to skip Vuex overhead on the server and just use Vuex on the client and populate it with INITIAL_DATA?
I'm probably missing something..
https://ssr.vuejs.org/guide/data.html#data-store
During SSR, we are essentially rendering a "snapshot" of our app. The asynchronous data from our components needs to be available before we mount the client side app - otherwise the client app would render using different state and the hydration would fail.
To address this, the fetched data needs to live outside the view components, in a dedicated data store, or a "state container". On the server, we can pre-fetch and fill data into the store while rendering. In addition, we will serialize and inline the state in the HTML after the app has finished rendering. The client-side store can directly pick up the inlined state before we mount the app.
Also to mention:
The data you access while SSR in any Component needs to come from somewhere if you want to share information across Components, this is what Vuex is there for.

VueJS - Object in VUEX store not the same between development server and production build

I have an object in a vue component with about 8 keys. It forms part of the data object.
The interesting thing is when I use - npm serve everything works 100% and the whole dictionary can be observerved in the VUEX store after a mutation.
If I then use npm build on the exact unmodified code the json object is not the same in the VUEX Store and is missing some of the keys after a mutation.
This is so weird. I have searched for a day and cannot find any reference to this.

Dynamically register a Vuex plugin?

How can I register a plugin dynamically to vuex. The documentation says I can dynamically register modules etc. but not how to do it for a plugin.
Is including the plugin at store creation the only method to add plugins?
I was hoping for a store.use(plugin) or vuex.use(plugin)…
A Vuex plugin is simply a function that receives the store as the only argument, and is invoked in the Store instance during construction.
To apply a plugin after a Store has been constructed, you just need to invoke the plugin function and pass the Store instance to that function:
import Vuex from 'vuex'
import Plugin from 'plugin'
const store = new Vuex.Store({ ... })
// Later on
Plugin(store)
Keep in mind that some plugins simply may not work correctly with already-constructed Store instances. Your milage may vary.

Vue js 2 Persist a component's data *privately*

I currently have a Vue.js 2 SPA using Vuex for inter-component communication and Vuex-persisted state to persist my shared state.
However, I want to persist a component's local private data.
I tried the package vue-persist and it persists the component data fine, however, the variables are not namespaced and therefore shared between all component instances. I want to persist each component's data separately.
Does anyone know how I can achieve these 2 things in my Vue.js 2.x app?
1) Persistence of my local data (so it survives a browser refresh).
2) The persisted data is not shared with other components (perhaps name-spaced for each component instance?).
Thanks!
I'm not familiar with vue-persist, but it looks like you can pass a store name as the second argument to $persist. Choose a unique store name for the component instance (this will vary depending on the component).
e.g.
created() {
this.$persist(['foo'], 'my-comp:' + this.id);
}