Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- self.addEventListener('fetch', function(e) {
- console.log('[ServiceWorker] Fetch', e.request.url);
- if (cdnToCache.find((element)=>{return e.request.url.indexOf(element) === 0;})) {
- e.respondWith(
- caches.match(e.request.url).then(function(response) {
- if (response){
- return response
- }else{
- return fetch(e.request)
- .then(function(response) {
- return caches.open(cacheCdnName).then(function(cache) {
- cache.put(e.request.url, response.clone());
- console.log('[ServiceWorker] Fetched&Cached Data');
- return response;
- });
- })
- }
- })
- );
- } else {
- e.respondWith(
- caches.match(e.request).then(function(response) {
- return response || fetch(e.request);
- })
- );
- }
- });
Add Comment
Please, Sign In to add comment