从 Android API 清除 Service Worker 缓存 [英] Clear service worker cache from Android API

查看:61
本文介绍了从 Android API 清除 Service Worker 缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带有一些 webviews 的原生 andorid 应用程序.我们在 Android webviews 中使用 Service Worker 功能.

We have a native andorid app with some webviews. We are using Service worker functionality in Android webviews.

http://www.html5rocks.com/en/tutorials/service-工人/介绍/

有没有办法从 Android API 中清除 Service Worker 缓存?我们希望在用户退出 Android 应用时专门清除缓存.

Is there a way to clear out the service worker cache from the Android API? We want to specifically clear the cache when the user logs out from the Android app.

推荐答案

我不是 Android 开发人员,而是 WebStorage#deleteAllData() 方法看起来可以完成你想要的.如果没有,这听起来像是一个错误,我建议您通过用于 Android 框架错误的任何机制进行跟进.

I'm not an Android developer, but the WebStorage#deleteAllData() method looks like it would accomplish what you'd want. If it doesn't, that sounds like a bug, and I'd recommend that you follow up via whatever mechanism is used for Android framework bugs.

或者,由于我是一名 Web 开发人员,这里有一种 JavaScript 方法可以解决问题.请注意,您所说的服务工作者缓存"实际上并不限于服务工作者;缓存存储 API 在工作全局范围和作为 window.caches,所以它会直接从网页的上下文中使用.因此,您可以运行直接从网页执行上下文中删除所有缓存的代码,而无需跳过箍来触发服务工作者:

Alternatively, and since I am a web developer, here's a JavaScript approach to clearing things out. Note that what you're referring to as a "service worker cache" isn't actually limited to service workers; the Cache Storage API is exposed in both the work global scope and as window.caches, so it would be used directly from the context of web pages. So you can run code that deletes all of the caches directly from a web page execution context, without jumping through hoops to trigger the service worker:

caches.keys()
  .then(cacheNames => Promise.all(cacheNames.map(cacheName => caches.delete(cacheName))))
  .then(() => console.log('Everything was deleted!'))
  .catch(error => console.warn('Something went wrong!', error));

WebStorage#deleteAllData() 调用不同,JavaScript 只会清除缓存存储 API,因此正常的 HTTP 缓存以及 cookie 和其他特定于站点的存储仍将保留.

Unlike the WebStorage#deleteAllData() call, that JavaScript will just clear the Cache Storage API, so the normal HTTP cache, as well as cookies and other site-specific storage will still remain.

这篇关于从 Android API 清除 Service Worker 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆