Should Zustand's persist middleware automatically fallback to in‑memory storage when localStorage is unavailable?
0 reputation · 03 Aug 2025, 03:14 UTC
When using Zustand's persist middleware in a server‑side rendered application, the store throws a ReferenceError: window is not defined because localStorage is unavailable during SSR. Developers currently work around this by conditionally initializing the middleware or providing a custom storage object. The library does not yet ship an automatic in‑memory fallback, leaving the decision to users. This raises the question of whether Zustand should handle the missing storage gracefully by default, balancing ergonomics against bundle size and potential misuse.
- Should the persist middleware detect a missing
windowand switch to an in‑memory store automatically? - If a fallback is provided, how should it be exposed or configured to avoid breaking existing custom storage implementations?