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
self.addEventListener('fetch', event => { | |
var type = event.request.headers.get('Accept'); | |
// network-first for HTML documents | |
if(type.includes('text/html')) { | |
event.respondWith( | |
// respondWith code from network-first section | |
); | |
} | |
// cache-first for assets | |
else { |
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
event.respondWith(caches.match(request).then(response => { | |
return response // cache-first | |
|| fetch(request).then(response => { // network | |
// ... | |
}) | |
.catch(() => { // offline | |
if(type.startsWith('image')) { | |
return new Response('<svg...', {headers: {'Content-Type': 'image/svg+xml', 'Cache-Control': 'no-store'}}); | |
} | |
else { |
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
event.respondWith(caches.match(request).then(response => { | |
return response // cache-first | |
|| fetch(request).then(response => { // network | |
// ... | |
}) | |
.catch(() => { // offline | |
if(type.startsWith('image')) { | |
return caches.match('/offline.jpg'); | |
} | |
else { |
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 version = '1-', | |
criticalsCacheName = `${version}critical`, | |
otherCacheName = `${version}other`, | |
imagesCacheName = `${version}images`; |
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 addToCache = (cacheName, request, response) => { | |
caches.open(cacheName).then(cache => cache.put(request, response)); | |
}; |
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 clearOldCaches = () => { | |
return caches.keys().then(keys => { | |
return Promise.all(keys.filter(key => !key.startsWith(version)).map(key => caches.delete(key))); | |
}); | |
}; | |
self.addEventListener('activate', event => { | |
event.waitUntil(clearOldCaches().then(() => self.clients.claim())); | |
}); |
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
if('serviceWorker' in navigator) { | |
navigator.serviceWorker.register('/serviceworker.js'); | |
if(navigator.serviceWorker.controller) { | |
navigator.serviceWorker.controller.postMessage({'command': 'trimCache'}); | |
} | |
} |
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 trimCache = (cacheName, maxItems) => { | |
caches.open(cacheName).then(cache => { | |
cache.keys().then(keys => { | |
if(keys.length > maxItems) | |
cache.delete(keys[0]).then(trimCache(cacheName, maxItems)); | |
}); | |
}); | |
}; | |
self.addEventListener('message', event => { |
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
if(criticalResources.includes(url.pathname)) | |
addToCache(criticalsCacheName, request, responseCopy); | |
else if(type.startsWith('image')) | |
addToCache(imagesCacheName, request, responseCopy); | |
else | |
addToCache(otherCacheName, request, responseCopy); |
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 thirdPartyExceptions = [ | |
'https://code.jquery.com/jquery.min.js' | |
]; | |
// ... | |
event.respondWith(fetch(request).then(response => { // network | |
// cache only resources from this domain as well as the exceptions | |
if(location.origin === url.origin || | |
thirdPartyExceptions.includes(url.href)) { | |
// addToCache use | |
} |