Nuxt 3 + Pinia, Store property not hydrated from server to client #2175
Replies: 5 comments
-
After more tests, it seems it works correctly using Option Stores, but not with Setup Stores, which I was using. |
Beta Was this translation helpful? Give feedback.
-
After yet more tests, it works with setup stores too, but only if you return the state properties unchanged. I'm used to returning a readonly property and a setter function for each store property, to prevent direct change of properties. But looks like this doesn't work well with hydration: What I was doing:
What needs to be done:
Is there any workaround to continue using readonly? |
Beta Was this translation helpful? Give feedback.
-
Just to add to this discussion, I ran across a similar issue and believe it's not limited to the readonly scenario (doesn't apply to my case.) In my example I have a simple setup store with a single ref and I want provide a get function for use in my Nuxt page. In addition, I have a hydration function that is called via a Nuxt plugin. If my store only returns the get function and hydration function I end up with a Store Example (doesn't work): import { defineStore } from 'pinia';
export const useMyStore = defineStore('myStore', () => {
const things = ref('no things here');
function getThings() {
return things.value;
}
function hydrate() {
things.value = 'now we have things!';
}
return {
getThings,
hydrate,
};
}); Warning Message: Return the ref as well (this works): import { defineStore } from 'pinia';
export const useMyStore = defineStore('myStore', () => {
const things = ref('no things here');
function getThings() {
return things.value;
}
function hydrate() {
things.value = 'now we have things!';
}
return {
getThings,
hydrate,
things, // Added this to make it work, even though it is never used
};
}); For clarity on the other moving parts, here is the page component and the plugin that calls the hydrate function. Page Component: <template>
<div>
{{ myThings }}
</div>
</template>
<script setup>
import { useMyStore } from '~/stores/myStore.js';
const myStore = useMyStore();
const myThings = myStore.getThings();
</script> Plugin calling hydration function: import { useMyStore } from '~/stores/myStore.js';
export default defineNuxtPlugin(async (nuxtApp) => {
const myStore = useMyStore(nuxtApp.$pinia);
myStore.hydrate();
}); |
Beta Was this translation helpful? Give feedback.
-
Works for me with SSR (persist: true):
|
Beta Was this translation helpful? Give feedback.
-
@soylomass |
Beta Was this translation helpful? Give feedback.
-
Reproduction
Steps to reproduce the bug
Expected behavior
I expect the Pinia store on the client to be hydrated with the property set on the server side. This would allow the client to access the updated store state and render the application accordingly.
Is this what's supposed to happen? Or I misunderstood?
Actual behavior
The Pinia store on the client does not have the property set on the server side. The store state is not being hydrated from the server to the client, causing inconsistencies between the server-rendered and client-rendered content.
Additional information
No response
Beta Was this translation helpful? Give feedback.
All reactions