Weird behavior with SSR #948
-
| 
         Hi all, I am super happy to make our stack evolve and move from  Almost everything is perfect except one weird behaviour. Thanks in advance, ReproductionClone this repo : pinia-vite-ssr-issue and use  Steps to reproduce the behaviorCheck the logs and the browser. Expected behaviorStore instance should have 1 item (as initial state and active pinia instance do). Actual behaviorStore instance is empty. Additional informationI've added comments and snippets inside the   | 
  
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
| 
         I’m glad you like it!  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Now I see the problem: on the client side you need to make sure you hydrate the state before using the store:     const pinia = createPinia()
    app.use(pinia)
    // This does not work
    if (import.meta.env.SSR) {
      doSomethingWithStore()
      const store = useStore()
      store.fetchSomething()
      initialState.pinia = pinia.state.value
    } else {
      pinia.state.value = initialState.pinia
      doSomethingWithStore()
    }On server you finish by serializing the state to the client while on the client you start by reading (hydrating) the state before using the store.  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Found this behaviour in my Nuxt 4 project.  | 
  
Beta Was this translation helpful? Give feedback.



Now I see the problem: on the client side you need to make sure you hydrate the state before using the store:
On server you finish by serializing the state to the client while on the client you start by reading (hydrating) the state before using the store.