5

This is might be a dump question but i could not figure it out why. So, i have to permanently store some configuration number into the browser, so if the app reload, it can get those config number right away. I think we can do it by using localStorage. I implemented and got it working, using :

localStorage.setItem('token', 'fsdfdsfsdfdsfds');
localStorage.setItem('config1', 'config1');
localStorage.setItem('config2', 'config2');
localStorage.setItem('config3', 'config3);

However, after 2 hours all of the config1, config2, config3 are gone. Only the token one still exists in localStorage. I though that item in localStore should stay as long as we want. We have full control over it.

Any explain here ? Thanks

j08691
  • 204,283
  • 31
  • 260
  • 272
daniel8x
  • 990
  • 4
  • 16
  • 34
  • This might help: [stackoverflow](https://stackoverflow.com/questions/2326943/when-do-items-in-html5-local-storage-expire) – Rob Moll Dec 06 '19 at 17:31
  • Are you using incognito mode? How are you testing that the values are there? Developer tools or calling functions in the console? – A1rPun Dec 06 '19 at 17:37
  • @A1rPun Yes. I am using incognito mode. Open my configuration app, and run the script above. I open my Dev Tool, i can see all the localStorage variable are set. Its working as expected. So, I am not closing my browser or my laptop, i leave it like that. The next day, when i come back, all localStorage are gone, except the token one. Its just weird or something i am missing. – daniel8x Dec 06 '19 at 17:44

2 Answers2

0

I suggest you use Chrome.storage.api, the data will persists when you close the browser. Works the same, key/pairs are stored and retrieved. I stopped using localStorage when I can do the same in Chrome.storage api.

Jason Owens
  • 525
  • 2
  • 6
  • 21
0

I'm not sure how Chrome handles localStorage in incognito mode so I can't give you a detailed answer but one thing I know is that you can't rely on localStorage in incognito.

Check MDN -> Web Storage API -> Private Browsing / Incognito modes

Most modern browsers support a privacy option called 'Incognito', 'Private Browsing' or something similar that doesn't store data like history and cookies. This is fundamentally incompatible with Web Storage for obvious reasons. As such, browser vendors are experimenting with different scenarios for how to deal with this incompatibility.

Most browsers have opted for a strategy where storage APIs are still available and seemingly fully functional, with the one big difference that all stored data is wiped after the browser is closed. For these browsers there are still different interpretations of what should be done with existing stored data (from a regular browsing session). Should it be available to read when in Private mode? Then there are some browsers, most notably Safari, that have opted for a solution where storage is available, but is empty and has a quota of 0 bytes assigned, effectively making it impossible to write data to it.

Developers should be aware of these different implementations and take them into account when developing websites depending on Web Storage APIs. For more information please have a look at this WHATWG blog post that specifically deals with this topic.

Community
  • 1
  • 1
A1rPun
  • 16,287
  • 7
  • 57
  • 90