Madhur Ahuja
Madhur Ahuja

Reputation: 22671

Clear service worker cache from Android API

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.

Upvotes: 0

Views: 1660

Answers (1)

Jeff Posnick
Jeff Posnick

Reputation: 56044

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.

Upvotes: 4

Related Questions