3

The concept seems simple, but implementation logic seems daunting right now.

I use localStorage in my app, and I want to clear it when I update the code.

Any ideas how?

5
  • Store the code version in the local storage and compare the versions on script load. Commented Dec 14, 2012 at 17:17
  • You shouldn't need that, though. Commented Dec 14, 2012 at 17:18
  • Why shouldn't I need that? Commented Dec 14, 2012 at 17:19
  • 1
    why should you need that? Commented Dec 14, 2012 at 17:23
  • @Story you may not have foreseen all the situations. How about bug fixes without any code change? You may not wish to loose your localstorage in that case Commented Dec 14, 2012 at 17:24

1 Answer 1

5

You could store the code version in the script and compare that when you load the script:

var version = "1.0.4"; // remember to update this if(localStorage.getItem("version") != version){ localStorage.clear(); localStorage.setItem("version", version); } $(function(){ ... 

If you don't want to track the version manually, you can generate the "version" by hashing the script. A good hash is a balance between ease of implementation (this kinda rules out MD5, unless you're willing to fetch a library), hash size (you don't want to store a copy of the script in localStorage) and its detection capabilities (so the string length may not suffice).

Needless to say, deleting all storage with any single change may be too cautious, so I recommend the manual version tracking approach, or (better yet) refactor the application so that it can work with the previous version data.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm accepting this answer, though I've tweaked it slightly in application. I'm already using a shell script to handle updates and set up, so I just added some lines to prepend git rev-parse HEAD to the top of my main.js as the version, as I absolutely want localStorage cleared every time there's any new code. Thanks for all of your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.