service-worker.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var CACHE_NAME = 'snapdrop-cache-v1.045';
  2. var urlsToCache = [
  3. '/',
  4. '/styles.css',
  5. '/scripts/network.js',
  6. '/scripts/ui.js',
  7. '/scripts/clipboard.js',
  8. '/sounds/blop.mp3',
  9. '/images/favicon-96x96.png'
  10. ];
  11. self.addEventListener('install', function(event) {
  12. // Perform install steps
  13. event.waitUntil(
  14. caches.open(CACHE_NAME)
  15. .then(function(cache) {
  16. console.log('Opened cache');
  17. return cache.addAll(urlsToCache);
  18. })
  19. );
  20. });
  21. self.addEventListener('fetch', function(event) {
  22. event.respondWith(
  23. caches.match(event.request)
  24. .then(function(response) {
  25. // Cache hit - return response
  26. if (response) {
  27. return response;
  28. }
  29. return fetch(event.request);
  30. }
  31. )
  32. );
  33. });
  34. self.addEventListener('activate', function(event) {
  35. console.log('Updating Service Worker...')
  36. event.waitUntil(
  37. caches.keys().then(function(cacheNames) {
  38. return Promise.all(
  39. cacheNames.filter(function(cacheName) {
  40. // Return true if you want to remove this cache,
  41. // but remember that caches are shared across
  42. // the whole origin
  43. return true
  44. }).map(function(cacheName) {
  45. return caches.delete(cacheName);
  46. })
  47. );
  48. })
  49. );
  50. });