Last active
October 30, 2015 13:11
-
-
Save alenabdula/7f038831d0d9d9a788bd to your computer and use it in GitHub Desktop.
Service Worker
This file contains hidden or 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
<script> | |
if ( 'serviceWorker' in navigator ) { | |
navigator.serviceWorker.register('/service-worker.js', { scope: '/' }) | |
.then(function(registration) { | |
console.log("Service Worker Registered"); | |
}); | |
navigator.serviceWorker.ready.then(function(registration) { | |
console.log("Service Worker Ready"); | |
}); | |
} | |
</script> |
This file contains hidden or 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
var cacheName = 'alenabdula'; | |
var urlsToCache = [ | |
'/', | |
'/css/master.min.css', | |
'/js/master.min.js', | |
]; | |
/* https://github.com/coonsta/cache-polyfill */ | |
importScripts('/cache-polyfill.js'); | |
/* Install */ | |
self.addEventListener('install', function(e) { | |
e.waitUntil( | |
caches.open(cacheName).then(function(cache) { | |
return cache.addAll(urlsToCache).then(function() { | |
return self.skipWaiting(); | |
}); | |
}) | |
); | |
}); | |
/* Activate */ | |
self.addEventListener('activate', function(event) { | |
event.waitUntil(self.clients.claim()); | |
}); | |
/* Fetch */ | |
self.addEventListener('fetch', function(event) { | |
console.log(event.request.url); | |
event.respondWith( | |
caches.match(event.request).then(function(response) { | |
return response || fetch(event.request); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment