Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add static page clear-cache.html for clear Service Worker cache
- Loading branch information
1 parent
daa65a7
commit 2d7f044
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |