When do items in HTML5 local storage expire?
My web application uses client side storage (
localStorage). However, I want this data to be expired after some time (eg. 2 days). It was a super easy task with expiration date of cookies but I don't know how to set an expiration date for HTML5 storage.
Any ideas how to solve this problem? Efficiency and simplicity is important.
The best you can do is to set a timestamp in storage, and if the user visits your site after a specified amount of time, then you can delete the stored data.
sessionStorage should work the same way, except when it doesn't even last long enough for your timed expiration. In that case, it'll expire sooner.
Storage model was meant to be a cache for scripts; not of a spur-of-the-moment data holder. Infact
sessionStorage should clear as soon as the protocol+domain is navigated away from. With that said, the simplest way would be to store and check a 'hold-until' value at each page view, and update/clear-storage it as needed
©2020 All rights reserved.