Skip to content

Instantly share code, notes, and snippets.

@srhise
Last active December 8, 2015 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srhise/c2099b347f68b958884d to your computer and use it in GitHub Desktop.
Save srhise/c2099b347f68b958884d to your computer and use it in GitHub Desktop.
serviceworker
/* 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