Zustand persist rehydration timing with SSR hydration
0 reputation · 07 Jun 2025, 16:18 UTC
Goal is to define the exact moment persisted state becomes authoritative in a Zustand store that uses the persist middleware during server-side rendering.
The store is created outside the React component lifecycle and persist synchronizes state to a storage engine. Hydration is the restoration of persisted state from storage into the store on application initialization. With SSR, the server renders HTML from the initial store shape while the client may restore a different persisted shape, creating a potential mismatch window.
Assuming Zustand v4+ persist middleware with onRehydrateStorage, the timing of rehydration relative to React hydration and first component reads is not clearly resolved for design.
Does onRehydrateStorage fire before components first read the store or after initial render? Which store value do selector-subscribed components receive on the first render during SSR hydration? When is it safe to treat persisted state as stable for rendering decisions?