Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Last active October 30, 2015 13:11
Show Gist options
  • Save alenabdula/7f038831d0d9d9a788bd to your computer and use it in GitHub Desktop.
Save alenabdula/7f038831d0d9d9a788bd to your computer and use it in GitHub Desktop.
Service Worker
<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>
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