Should Qwik automatically persist useStore state across page reloads?
0 reputation · 07 May 2024, 22:56 UTC
0 reputation · 07 May 2024, 22:56 UTC
Qwik serializes component state from the useStore hook into DOM attributes during hydration, allowing the UI to reconstruct its previous state without re‑executing side‑effects. The goal is to decide whether this serialized state should be automatically persisted to a durable storage mechanism (e.g., localStorage or IndexedDB) so that a full page reload does not reset the state.
In Qwik 1.x, the state stored in attributes is lost on reload unless the developer manually saves it elsewhere, which adds boilerplate and risks inconsistency. Persisting automatically could improve user experience but may increase HTML size, introduce storage overhead, and complicate serialization logic for large stores. Considering these trade‑offs, what should the default behavior be?
useStore state to localStorage after each hydration?31775 reputation · 08 May 2024, 04:58 UTC
No. Qwik should not automatically persist useStore state to durable storage across full page reloads by default. The default should remain resumability via serialized state in the DOM, with zero durable I/O, and persistence to localStorage or IndexedDB should be opt-in per store.
useStore state into the server-rendered DOM and restores it when the application resumes. This enables resumability across client navigation without re-executing side effects.useStore state resets to the server-rendered initial value unless the developer saves it elsewhere.useStore is reconstructed from the HTML on resume and is discarded on reload.Automatic persistence would change Qwik's core promise of lightweight, zero-IO startup. It would require reading persisted data before or during resume and writing on every state change, adding serialization cost and main-thread work.
An opt-in flag to persist a specific store to localStorage or IndexedDB after resume preserves the default lightweight model while allowing richer UX when desired. That is the more consistent design.
Server rendering has no knowledge of client durable storage. If persisted state is applied unconditionally, the live DOM diverges from both the server-rendered markup and the serialized snapshot Qwik resumes from, so a reconcile step is unavoidable: either re-render after reading storage, or treat persisted data as a post-resume patch.
Both approaches risk flicker, version skew across deploys, and failures for non-serializable values. Serialization of arbitrary store shapes can fail and complicates store schema versioning.
useStore and mutate it client-side. Observe that state survives client navigation but resets on a hard reload.Assumption: Qwik 1.x behavior as described in the question. Current verification is recommended because Qwik evolves quickly and any RFC or utility for store persistence may change.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.