37

I read few articles about IndexdDB, but couldn't find details about the lifetime of persisted data. I plan to use it for a session of data manipulation and upload once the user finishes. But what will happen if:

  • user close the browser tab
  • user closes the browser
  • user restarted the system

Also, I maintain user session through cookie based authentication. What will happen if the user logs off and log back in again? Is there a way to retrieve the data before the logoff?

Any documentation on handling this is appreciated. I skimmed through the spec, but it is not that good a read.

Thanks.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
bsr
  • 57,282
  • 86
  • 216
  • 316

4 Answers4

22

It's like localStorage, so it's cross-session, meaning restarting browser or system won't affect what is stored in it. However, user can clear it like clearing cookie. So it's just like persistent cookie, you don't trust it from the server-side, and you always need to check its integrity.

Cat Chen
  • 2,387
  • 17
  • 12
14

Persistent Storage has been available in Chrome since v52 and Firefox since v55. Support in other browsers can't be relied on though. You must test if persistent storage is available and react accordingly.

if (navigator.storage && navigator.storage.persist) {
  navigator.storage.persist().then(persistent => {
    if (persistent) {
      console.log("Storage will not be cleared except by explicit user action");
    } else {
      console.warn("Storage may be cleared by the UA under storage pressure.");
    }
  });
}

Chrome requires permission to use this feature. It will automatically be granted when calling navigator.storage.persist() if any of the following are true:

  • The site is bookmarked (and the user has 5 or less bookmarks)
  • The site has high site engagement
  • The site has been added to home screen on mobile device
  • The site has push notifications enabled

This list comes from an article outlining Chrome's implementation which is updated periodically with new information about this subject.

Besworks
  • 4,123
  • 1
  • 18
  • 34
  • 1
    In the current chrome version, the permission is granted forever, even if you remove the bookmark. – Sebi2020 Dec 09 '20 at 10:23
7

As of 2022, IndexedDB is persistent type of data storage. Thus, it is evicted if the user chooses to.

Note: After introducing Storage API, the "permanent" folder can be considered obsolete; the "permanent" folder only stores IndexedDB persistent-type databases. It doesn't matter if box mode is "best-effort" or "persistent" — data is stored under /storage/default. https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria

So, If user logs off and log back in again, then user can access data as before logs off.

koijigen
  • 166
  • 1
  • 2
  • 8
  • 2
    The data can also be removed without the intervention of the user if the browser runs out of space in the hard drive, as explained in your MDN link. – AxeEffect Jan 27 '23 at 13:36
5

IndexedDB data belong to a type of temporary. So these data can be wiped out at any time.

These data size/lifetime are managed by very new Quota Management API.

In the future, IndexedDB could possibly used persistance type (not likely and not good idea too).

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • so how to get guarantee of permanent storage? for instance local cache of large database which is expensive to keep having to copy to the device? – Michael May 11 '16 at 01:18
  • 4
    It appears that Chrome is moving forward with allowing for persistence to be turned on. But it doesn't look like it will be generally available until October 2016. https://developers.google.com/web/updates/2016/06/persistent-storage – Andrew Sep 02 '16 at 04:11