Client-Side State Persistence with localStorage

Client-Side State Persistence with localStorage

The Lesson

localStorage can serve as a full persistence layer for client-side applications when the data is user-specific, the data volume is small, and there is no multi-device sync requirement. The key challenges are key design, migration of storage formats, and graceful handling of storage limits and corruption.

Context

The quiz application persists quiz state (current question, answers given, hints revealed, timestamps) to localStorage so users can close the browser and resume later. It also maintains a history of completed quiz attempts for the results page.

What Happened

  1. The initial quiz application had no persistence — closing the browser lost all progress. Users studying 50-question exams couldn't complete them in one sitting.
  2. A ProgressTracker class was added that saves quiz state to localStorage after every answer submission and hint reveal, keyed by exam code (e.g., progress_az-900).
  3. On quiz load, the tracker checks for saved state and prompts the user to resume or start fresh. Resuming restores the exact question, answers, and hint state.
  4. When the storage format changed (adding timestamp tracking for the results page), a migration path was added: the tracker detects old format entries and upgrades them in place rather than discarding them.
  5. A results history feature was added that saves completed quiz attempts separately, allowing the results page to show past scores, timing, and per-question breakdowns.
  6. The clear operation was scoped per-exam rather than global, so clearing progress on one exam doesn't affect others.

Key Insights

Related Lessons