localStorage data persistence

2020-08-22 06:33发布

问题:

I recently discovered that it's possible for the user to clear HTML5's localStorage value, which caused me to wonder... exactly how persistant is localStorage? If Flash crashes and wipes all my cookies, would I lose my localStorage data? What if my local cache was cleared to make way for a browser update?

I've seen this answer, but it doesn't address browser crashes, updates, cache clearing, etc. Is localStorage really and truly permanent, as the aforementioned answer seems to mantain?

回答1:

LocalStorage is not permanent. The storage belongs to the user so the user can clear it if they want to. Other web apps cannot mess with it, but the user is free to clear it or modify it if they want - it's their data, much like files that belong to a locally installed application are under the control of the computer user.

In addition, LocalStorage can be recycled when space is low.

You should think of LocalStorage as a long term cache that usually will remain with that particular browser on that particular computer, but will not always be there. Any truly persistent state must be stored on your own server.

Heck, if the user just decides to switch to another browser (much less a new computer), all Local Storage will appear to be empty in the new browser.



回答2:

It most certainly isn't permanent. You shouldn't count on it.

Apart from situation where user is actively clearing the storage, his machine can be reinstalled. Or he can log from another computer. Your app better be able to handle that.