Created
February 7, 2019 06:51
-
-
Save anurag-majumdar/ecd3b525bd065bd5915857783a43bed4 to your computer and use it in GitHub Desktop.
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 staticAssetsCacheName = 'todo-assets-v3'; | |
var dynamicCacheName = 'todo-dynamic-v3'; | |
self.addEventListener('install', function (event) { | |
self.skipWaiting(); | |
event.waitUntil( | |
caches.open(staticAssetsCacheName).then(function (cache) { | |
cache.addAll([ | |
'/', | |
"chunks/todo.d41d8cd98f00b204e980.js","index.html","main.d41d8cd98f00b204e980.js" | |
] | |
); | |
}).catch((error) => { | |
console.log('Error caching static assets:', error); | |
}) | |
); | |
}); | |
self.addEventListener('activate', function (event) { | |
if (self.clients && clients.claim) { | |
clients.claim(); | |
} | |
event.waitUntil( | |
caches.keys().then(function (cacheNames) { | |
return Promise.all( | |
cacheNames.filter(function (cacheName) { | |
return (cacheName.startsWith('todo-')) && cacheName !== staticAssetsCacheName; | |
}) | |
.map(function (cacheName) { | |
return caches.delete(cacheName); | |
}) | |
).catch((error) => { | |
console.log('Some error occurred while removing existing cache:', error); | |
}); | |
}).catch((error) => { | |
console.log('Some error occurred while removing existing cache:', error); | |
})); | |
}); | |
self.addEventListener('fetch', (event) => { | |
event.respondWith( | |
caches.match(event.request).then((response) => { | |
return response || fetch(event.request) | |
.then((fetchResponse) => { | |
return cacheDynamicRequestData(dynamicCacheName, event.request.url, fetchResponse.clone()); | |
}).catch((error) => { | |
console.log(error); | |
}); | |
}).catch((error) => { | |
console.log(error); | |
}) | |
); | |
}); | |
function cacheDynamicRequestData(dynamicCacheName, url, fetchResponse) { | |
return caches.open(dynamicCacheName) | |
.then((cache) => { | |
cache.put(url, fetchResponse.clone()); | |
return fetchResponse; | |
}).catch((error) => { | |
console.log(error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment