Clear service worker cache from Android API

送分小仙女□ 提交于 2019-12-08 13:20:43

问题


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-worker/introduction/

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.


回答1:


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.

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));

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.



来源:https://stackoverflow.com/questions/39202258/clear-service-worker-cache-from-android-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!