Skip to content

Commit

Permalink
Add static page clear-cache.html for clear Service Worker cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefJezek committed Jun 16, 2016
1 parent daa65a7 commit 2d7f044
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions clear-cache.html
@@ -0,0 +1,49 @@
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Clear Cache</title>
</head>
<body>
<p>This will clear all service worker cache entries for the current origin.</p>
<button id="clear-cache">Clear Cache</button>
<script>
function sendMessage(message) {
return new Promise(function(resolve, reject) {
if (navigator.serviceWorker.controller) {
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function (event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
}
};
navigator.serviceWorker.controller.postMessage(message, [messageChannel.port2]);
} else {
reject("This page isn't currently controlled by a service worker.");
}
});
}

document.querySelector('#clear-cache').addEventListener('click', function() {
sendMessage({command: 'delete_all'}).then(function() {
console.log('All caches deleted.');
}).catch(function(error) {
console.error('Caches not deleted:', error);
});
});
</script>
</body>
</html>

0 comments on commit 2d7f044

Please sign in to comment.