Sergey Konyshev
Sergey Konyshev

Reputation: 31

IndexedDB persistence in PWA application

PWA application storage (IndexedDB) isn't able to provide data persistence.

In case that PWA is pinned to home screen it is possible to clear all application data from browser by clearing browsing history.

It might be unclear for users that cleaning browser data can affect pinned application and unsynchronised data will be lost.

Is there any way to avoid this? The only way I see for now - turn back to native apps.

Upvotes: 3

Views: 3812

Answers (3)

Francesco
Francesco

Reputation: 10830

This is not possible.

The Storage API provides a StorageManager.persist() method to request the user explicit permission to persist data until deleted by the user itself:

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

If the local storage is running out of space, the User Agent will start automatically pruning cached resourced except the ones set as "persistent". However if the user itself chooses to clear the local data, there is no way to prevent this.

As far as I am aware, there is no event you can intercept in order to detect a browser clear action from the user.

See API reference doc : https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/persist

Upvotes: 0

Chris Love
Chris Love

Reputation: 3893

The clear storage mechanism in browsers is to put the user in control of their device.

This is why you as an application should never (native or web) assume your cached assets are cached.

If it is absolutely important to you to make sure you have core assets and data persisted then you need to have some sort of integrity check when the service worker initiates. That way you can restore cached state in case the application goes offline.

You also need to realize the operating system, looking at you iOS, will purge data when it feels like it (think when the available disk space gets critical), which takes you out fo control. It does this for native apps too as far as I know.

Upvotes: 1

IanC
IanC

Reputation: 883

I do not know a way around that. The function in Chrome to "clear storage" (for example) does exactly that. I suppose it is reasonable for a user to be able to remove any data from their own device, but I agree it is not a good situation for the developer.

Upvotes: 0

Related Questions