İlknur ÖzcomartSeaCodeTREN

Blog

Storing data in the browser: how safe is localStorage?

· Web Development

Storing data without accounts is easy, but a few rules keep you from losing it.

localStorage lets the browser keep key-value pairs on the device. It needs no server or account, so data never leaves the device and privacy is simple. That is why KADRAN keeps its data in localStorage.

Know the limits

It stores text only, has limited capacity and works synchronously. If the user clears site data or uses a private window, the data is gone. So an app should notice when storage does not work and warn the user.

A real data-loss bug

In tests I noticed that all-day events in KADRAN were silently deleted when the page reloaded. The cause: a record was validated when saved, but when it was validated again on load, the all-day event was treated as invalid and dropped. I fixed it and wrote a test for it. A record that cannot be read is also no longer deleted; it is backed up under a separate key and the user is told.

A version number

Putting a version number on stored data makes changing fields safe later. When I added recurring events to KADRAN I raised the version from 1 to 2. Old records open as they are and are not rewritten until the user saves something.

localStoragedataprivacybackup

Next post