Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Vuex store reside in Nuxt

Tags:

vue.js

vuex

Just trying to get my head around Vuex / Nuxt , also quite new to nodejs.

Given that nuxt is a server side rendering app , Where does Vuex "reside" . In standard Vue , Vuex is a client side store. How does it work in Nuxt? Is it still a client side store but just rendered on the server first?

If it stays on the server , then I am just wondering how it handles per user data - or is it somehow shared. thank you.

like image 485
Martin Thompson Avatar asked Mar 01 '18 20:03

Martin Thompson


People also ask

Where is Vuex data stored?

In Vuex, our data store is defined as state in the store/index. js config object. Even though it sounds like new terminology to learn, think of it as the data property that we have been using this whole time.

How Vuex store is created in Nuxt?

The Vuex Store comes with Nuxt out of the box but is disabled by default. Creating an index. js file in this directory enables the store. This directory cannot be renamed without extra configuration.

How do I import Vuex into Nuxt?

import Vuex from "vuex"; import state from './state'; import actions from './actions'; import mutations from './mutations; export default { state, actions, mutations, }; There is no need to import the modules because Nuxt will do that for you.

Does Vuex store persist?

A Typescript-ready Vuex plugin that enables you to save the state of your app to a persisted storage like Cookies or localStorage.


1 Answers

SSR and rehydration:

Both client and server hold the state. When the client makes the first request to load the initial page the state is usually passed through the html document inside a script, something like:

<script type="text/javascript" defer>window.__NUXT__={"data":[{"name":"server"}],"error":null,"serverRendered":true}</script>

Then when the application js load it has to pickup the state, this processed is known as rehydration. From this point the client will maintain the server state in sync.

Nuxt specifics:

Interesting parts of Nuxt documentation:

  • https://nuxtjs.org/guide/async-data
  • https://nuxtjs.org/guide/vuex-store

Nuxt Vuex Demo:

https://nuxtjs.org/examples/vuex-store

Vue SSR:

In addition take a look into vue ssr documentation, it's very detailed and it does a better job explaining how everything fits in:

  • https://ssr.vuejs.org/en/hydration.html
  • https://ssr.vuejs.org/en/data.html

we will serialize and inline the state in the HTML. The client-side store can directly pick up the inlined state before we mount the app.

like image 105
a--m Avatar answered Oct 21 '22 15:10

a--m