Reliable persistent local storage for a Chrome web application on Android

Viewed 202

Can a webapp have reliable local data storage on Android?

If not then can a web application be installed as a local application, to gain access to privileged APIs? Perhaps (this is just a wild guess) as a "Chrome extension" or an "Installed web app"?


Details

A single-page web application is a static web page on a web server without a backend-end database or API (i.e. there is no fetch within the application).

The application stores and uses data on the user's device, and I want that storage to be reliable, i.e.

  • I do not mind having to ask the user for permission
  • But the data must not then be deleted without the user's consent

Now I'm worried that this article -- Managing HTML5 Offline Storage (from 2015) -- says,

Persistent storage is storage that stays in the browser unless the user expunges it. It is available only to apps that use the File System API, but will eventually be available to other offline APIs like IndexedDB and Application Cache.

... and the section Running out of storage suggests that storage may be deleted:

  • If the user has more than 5 "offline" applications
  • If the device begins to run out of disk space

The relevant APIs seems to be these:

Browsers API
All Window API: localStorage
Web Storage - name/value pairs
All IndexedDB
not Android! Filesystem & FileWriter (a sandboxed file system)
not Android! File System Access (not in a sandbox)

So can it be done on Android, or is there any workaround?

And the APIs mentioned in the article -- e.g. webkitPersistentStorage -- are apparently deprecated. Have these been replaced by something, and are they available on Android?

1 Answers

According to this article -- Persistent storage (May 2020) -- there's a storage.persist() method:

// Request persistent storage for site
if (navigator.storage && navigator.storage.persist) {
  const isPersisted = await navigator.storage.persist();
  console.log(`Persisted storage granted: ${isPersisted}`);
}

If the persistent storage permission is granted, the browser will not evict data stored in:

  • Cache API
  • Cookies
  • DOM Storage (Local Storage)
  • File System API (browser-provided and sandboxed file system)
  • IndexedDB
  • Service workers
  • App Cache (deprecated, should not be used)
  • WebSQL (deprecated, should not be used)

MDN currently warns that this is "experimental technology" -- but most browsers seems to have implemented it including Chrome on Android.

Related