Reputation: 10243
There are some scenarios where I've been using localStorage in order to persist some information for the users of an application. I use keys which help identify the user.
Simple example:
var key = localStorage.getItem("@Membership.GetUser().Username-SomeKey");
Let's say that I want to persist this information on the client, but I want to remove it when there is a code change (ie a new build). Let's assume that a code change might result in some of those locally stored values to contain "bad" data.
What would be an easy way to wipe clean out the localStorage when there is a new build?
Upvotes: 2
Views: 6558
Reputation: 141638
I would start keeping track of a build number of the assembly in localStorage. Then you could on the load of your page check to see if the localStorage version matches your assembly version. If they don't, call localStorage.clear()
and then insert the new build number into localStorage.
To kind of pseudo code it:
if (localStorage.getItem("AssemblyVersion") !== @GetAssemblyVersion) {
localStorage.clear();
localStorage.setItem("AssemblyVersion", @GetAssemblyVersion);
}
There are other questions that demonstrate how to get the assembly version. You could wrap that up in a Razor helper function or add it to your Model. All you would need to do from there is ensure your assembly version changes on each build.
Upvotes: 2