Skip to content

Instantly share code, notes, and snippets.

@JustJenFelice
Forked from gauntface/sw-test-cleaup.js
Created July 18, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustJenFelice/58c11bbdb7265d07dda0ad7b39b90778 to your computer and use it in GitHub Desktop.
Save JustJenFelice/58c11bbdb7265d07dda0ad7b39b90778 to your computer and use it in GitHub Desktop.
Function to unregister SW and clear out old caches.
window.__testCleanup = () => {
const unregisterSW = () => {
return navigator.serviceWorker.getRegistrations()
.then((registrations) => {
const unregisterPromise = registrations.map((registration) => {
return registration.unregister();
});
return Promise.all(unregisterPromise);
});
};
const clearCaches = () => {
return window.caches.keys()
.then((cacheNames) => {
return Promise.all(cacheNames.map((cacheName) => {
return window.caches.delete(cacheName);
}));
});
};
return Promise.all([
unregisterSW(),
clearCaches(),
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment