Last active
December 8, 2015 03:30
-
-
Save srhise/c2099b347f68b958884d to your computer and use it in GitHub Desktop.
serviceworker
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
/* in main.js it is successfully registering | |
if ('serviceWorker' in navigator) { | |
navigator.serviceWorker.register('js/serviceWorker.js').then(function(reg) { | |
// registration worked | |
console.log('Registration succeeded. Scope is ' + reg.scope); | |
}).catch(function(error) { | |
// registration failed | |
console.log('Registration failed with ' + error); | |
}); | |
}; | |
*/ | |
this.addEventListener('install', function(event) { | |
event.waitUntil( | |
caches.open('v1').then(function(cache) { | |
console.log(cache); | |
return cache.addAll([ | |
'/json/0.json', | |
'/json/1.json', | |
'/json/2.json', | |
'/json/3.json', | |
'/json/4.json', | |
'/json/5.json', | |
'/json/6.json', | |
'/json/7.json', | |
'/json/8.json', | |
'/json/9.json', | |
'/json/states.json', | |
'/json/counties.json' | |
]); | |
}) | |
); | |
}); | |
this.addEventListener('fetch', function(event) { | |
console.log(event); | |
var response; | |
event.respondWith(caches.match(event.request).catch(function() { | |
return fetch(event.request); | |
}).then(function(r) { | |
response = r; | |
caches.open('v1').then(function(cache) { | |
cache.put(event.request, response); | |
}); | |
return response.clone(); | |
}).catch(function() { | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment