android - 如何避免本地 Web 应用程序数据被删除?

标签 android html google-chrome service-worker progressive-web-apps

我们希望我们的网络应用可以离线使用,主要是在移动设备上。我们已经为此编写了代码,使用了 service worker。应用程序数据存储在 IndexedDb 中,应用程序代码(html、js、css 等)存储在 SW 缓存中。到目前为止,一切都很好。我们知道用户可以删除浏览器缓存和我们的数据,这不是问题。但是浏览器本身删除应用数据呢?我们还没有找到一个全面的规范,我们找到的主要信息是:

1) StorageManager当前标记为“实验性”的功能(自 2016 年起);

2)来自谷歌的一篇短文here关于它(也从 2016 年开始)。

代码示例如下:

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

谷歌文章说:

When storage on the local machine is running tight (“under storage pressure”), user agents automatically clear storage to make more available space. Of course, for offline apps, this may be unfortunate, as they may not have synced their data to the server yet, or they may be apps that the user expects to just work offline (like a music player); so the Storage spec defines two different modes for storage for a given domain - “best effort” and “persistent”. The default mode, of course, is “best effort”. Storage for a domain that is “best effort” (aka “not persistent”) can be cleared automatically, without interrupting or asking the user. However, “persistent” data will not be automatically cleared. (If the system is still under storage pressure after clearing all non-persistent data, the user will need to manually clear any remaining persistent storage.)

...

Beginning with Chrome 55, Chrome will automatically grant the persistence permission 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
  • The site has push notifications enabled

The permission is automatically denied in all other cases.

The goal is to ensure that users can rely on their favorite web apps and not find they have suddenly been cleared.



这是针对 Chrome 55 的,让我们假设信息是最新的。乍一看,他们的目标听起来很合理,但如果你仔细看看,这个实现是为“大”网站(如谷歌)而不是更面向任务的小众应用程序而设计的。

事实上,在 Chrome 80+ 上测试各种 Android 手机时,持久性总是被拒绝,没有用户交互。所以,“尽力而为”。

我们本可以在这里停止调查并收工。毕竟,目前的手机和 PC 的存储量非常大,而我们只使用了几百 KB,所以我们应该没问题。问题是,我们不是:在带有 Chrome 的全新旗舰 Android 手机上进行测试,我们的代码只需要几秒钟的摆弄就被删除(关闭和打开页面几次就足够了)。在其他平台上有所不同,但 Android+Chrome 将得到最多的使用。

奇怪的是,只有 SW 缓存(<100KB)中的代码被删除,而更大的 IndexedDb 没有。所以我们也尝试将代码放在 IndexedDb 中,这样看起来更“持久”,但要管理的代码也更复杂,所以感觉有点 hackish。

只有我们一个人有这个问题吗?如果没有,你们是如何处理它的?

额外的问题:在某个地方是否有更多关于该主题的最新文档?

最佳答案

如果我理解正确,主要问题是 Android 上的 Chrome 浏览器不断清空不满足自动授予持久性权限的 Chrome 条件的网站的浏览器缓存 .

到目前为止,我还没有遇到这种情况,但是我观察到了这种行为,你引用了 https://developers.google.com/web/updates/2016/06/persistent-storage - 到目前为止,我只是不知道它明确记录在案。

我看到了一些网站如何强制执行持久用户数据存储的三种方式:

  • 反复要求用户将网站添加到主屏幕和/或启用推送通知。 我观察到此请求通常以某种方式出现在错误标记下,即“订阅通知以表达您的感激之情”而不是“我们是否允许持久存储数据”之类的东西。甚至可以说“安装我们的应用程序”本质上意味着将全屏 Chrome 实例添加到移动设备的主屏幕,而不是应用商店中的真实应用程序。
  • 部分网站提供 Chrome 扩展程序 这允许他们在那里存储东西,甚至获得更多的访问权限。我个人不建议这种方法,它以某种方式给具有安全意识的用户带来了一种奇怪的感觉。
  • 另一种选择是混合方法,您可以 提供一个 native 应用程序,实际上只是一个定制的浏览器 .如果乍一看这听起来很奇怪,请耐心等待我。这个选项实际上很容易获得,例如在 React Native 中为 react-native-webbrowser component .这显然需要编程工作,但似乎有不少新闻网站使用这种方法。

  • 选项 1 和 2 都使用 Chrome,但显然不适合您,因为您只是想避免打扰用户。

    选项 3 是非常规的,但可能是一个值得考虑的选项。只有身为程序员的用户可能会意识到他们在安装本质上只是一个浏览器的东西时有点被愚弄了。尽管如此,它确实是一个干净的解决方案:应用商店负责访问权限控制,您可以让用户完全选择数据会发生什么。

    关于android - 如何避免本地 Web 应用程序数据被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61685257/

    相关文章:

    google-chrome - Chrome的奇怪 `gpu-preferences`

    安卓广播组

    android - android 中 0xff -colors 有什么用?

    javascript - 工具提示 z-index 位置或其他?

    javascript - 如何相对于 Canvas 大小javascript定位对象?

    javascript - 无法在同一台机器上加载 XMLHttpRequest

    Android DefaultHttpClient 默认超时

    Fedora/NVIDIA GPU 上的 Android Emulator 黑屏

    javascript - setTimeout 我做错了吗?用于烟花

    html - CSS 不随变化更新