Zustand middleware composition order affecting persistence serialization
0 reputation · 25 Mar 2021, 05:37 UTC
0 reputation · 25 Mar 2021, 05:37 UTC
Zustand's middleware system allows developers to compose multiple extensions like persist, serialize, and devtools. The order in which these middlewares are applied can significantly impact how state is transformed and stored. When using persist middleware with custom serialization options, the interaction between the middleware chain and the resulting stored data becomes critical.
The persist middleware handles writing state to storage, while serialize middleware controls how state is transformed before storage and restored after retrieval. If these are composed in different orders, the serialization may occur at different stages of the persistence pipeline, potentially leading to unexpected stored values or restoration behavior.
Documentation shows both persist and serialize as separate capabilities, but the exact interaction when both are applied to the same store shape requires clarification. The question is whether the serialize middleware wraps the persist middleware's internal operations or operates independently within the composition chain.
36775 reputation · 25 Mar 2021, 12:27 UTC
The middleware array order in create(store, [middleware1, middleware2]) determines whether serialization happens before or after persistence writes to storage. Zustand executes middleware sequentially: each middleware's getState wrapper intercepts state before passing it to the next middleware in the array.
[serialize, persist] (serialize first)serialize middleware transforms state via its serialize functionpersist middleware receives the already-serialized value and writes it to storagepersist reads from storage, then serialize runs deserialize before state reaches your appResult: Storage contains the serialized format. Raw Zustand state never touches storage.
[persist, serialize] (persist first)persist middleware writes raw Zustand state to storageserialize middleware runs on the restored value during hydration (via its deserialize)Result: Storage contains raw Zustand state shape. Serialization only applies during restoration.
The official Zustand documentation does not explicitly guarantee middleware execution order beyond the sequential array behavior. The middleware guide shows composition examples but does not formalize a contract for persist + serialize interaction. The behavior described above derives from the middleware chain implementation: each middleware wraps getState/setState and calls next in array order.
To confirm behavior in your version:
localStorage.getItem('your-key')console.log in custom serialize/deserialize and persist's onRehydrateStorage to trace execution sequenceserialize middleware API surface or default behavior. Test on your pinned version.createJSONStorage already serializes to JSON. If serialize runs after persist (Order B), you risk double-serialization if your serialize function also produces JSON strings.persist options like partialize run inside the persist middleware, so their position relative to serialize follows the same array-order rule.Use [serialize, persist] when you want storage to hold your custom serialized format (e.g., compressed, encrypted, or schema-versioned). Use [persist, serialize] only if you need raw Zustand state in storage and want deserialization to run at hydration time. Pick one order and verify the stored output matches expectations.
Missing diagnostic detail: What does your serialize middleware's serialize function return — a plain object, a JSON string, or another format? This determines whether double-serialization occurs with Order B.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.