Created
April 18, 2018 12:23
-
-
Save amitkhare/b1828173df33cd8ed60143fb9ca1b1c4 to your computer and use it in GitHub Desktop.
basic cache all service-worker
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
const PRECACHE = 'precache-v2'; | |
const RUNTIME = 'runtime'; | |
// A list of local resources we always want to be cached. | |
const PRECACHE_URLS = [ | |
'./' // Alias for index.html | |
// 'fonts/MaterialIcons-Regular.012cf6a.woff', | |
// 'fonts/Roboto-Bold.ad140ff.woff', | |
// 'fonts/Roboto-Light.37fbbba.woff', | |
// 'fonts/Roboto-Medium.303ded6.woff', | |
// 'fonts/Roboto-Regular.081b11e.woff', | |
// 'fonts/Roboto-Thin.90d3804.woff', | |
// 'js/0.6287906c54a59ee16790.js', | |
// 'js/1.c59f7cafdd7101af8554.js', | |
// 'js/2.12493e50dd0bdd6edc4c.js', | |
// 'js/3.33a4d895589dab5f5e70.js', | |
// 'js/4.5cc8d2a3e2d6669f8a78.js', | |
// 'js/5.cca72417da7ad5104714.js', | |
// 'js/6.baf8cc65343bf68017ab.js', | |
// 'js/7.4391460d2fa8a249e467.js', | |
// 'js/app.1350f10f5c4606bf9aa3.js', | |
// 'js/vendor.868c4b50a129e1482916.js', | |
// 'statics/quasar-logo.png', | |
// 'statics/icons/apple-icon-152x152.png', | |
// 'statics/icons/icon-512x512.png', | |
// 'statics/icons/favicon-16x16.png', | |
// 'index.html', | |
// 'app.8e4b8ab36b5441f8f0f9cd7c89179705.css' | |
]; | |
// The install handler takes care of precaching the resources we always need. | |
self.addEventListener('install', event => { | |
event.waitUntil( | |
caches.open(PRECACHE) | |
.then(cache => cache.addAll(PRECACHE_URLS)) | |
.then(self.skipWaiting()) | |
); | |
}); | |
// The activate handler takes care of cleaning up old caches. | |
self.addEventListener('activate', event => { | |
const currentCaches = [PRECACHE, RUNTIME]; | |
event.waitUntil( | |
caches.keys().then(cacheNames => { | |
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName)); | |
}).then(cachesToDelete => { | |
return Promise.all(cachesToDelete.map(cacheToDelete => { | |
return caches.delete(cacheToDelete); | |
})); | |
}).then(() => self.clients.claim()) | |
); | |
}); | |
// The fetch handler serves responses for same-origin resources from a cache. | |
// If no response is found, it populates the runtime cache with the response | |
// from the network before returning it to the page. | |
self.addEventListener('fetch', event => { | |
// Skip cross-origin requests, like those for Google Analytics. | |
if (event.request.url.startsWith(self.location.origin)) { | |
event.respondWith( | |
caches.match(event.request).then(cachedResponse => { | |
if (cachedResponse) { | |
return cachedResponse; | |
} | |
return caches.open(RUNTIME).then(cache => { | |
return fetch(event.request).then(response => { | |
// Put a copy of the response in the runtime cache. | |
if(event.request.url.includes('kat.khare.co.in/api/')){ | |
return response; | |
} else { | |
return cache.put(event.request, response.clone()).then(() => { | |
return response; | |
}); | |
} | |
}); | |
}); | |
}) | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment