This file contains 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
// a check if the technology is supported by browser | |
if('serviceWorker' in navigator) { | |
navigator.serviceWorker.register('/serviceworker.js'); | |
} |
This file contains 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) { | |
document.querySelector('.hero').addEventListener('animationend', function () { | |
// service worker is registered only when the animation ends | |
navigator.serviceWorker.register('/serviceworker.js'); | |
}); | |
} | |
// --- | |
App.init({ | |
// config | |
complete: function() { |
This file contains 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) { | |
window.addEventListener('load', function() { | |
navigator.serviceWorker.register('/serviceworker.js'); | |
}); | |
} |
This file contains 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('install', event => { | |
// ... | |
}); |
This file contains 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('activate', event => { | |
// ... | |
}); |
This file contains 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 => { | |
// ... | |
}); |
This file contains 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 criticalResources = [ | |
'/', | |
'/offline/', | |
'/assets/css/main.css', | |
'/assets/js/main.js' | |
], | |
cacheCriticals = () => { | |
return caches.open(version).then( cache => { | |
return cache.addAll(criticalResources); | |
}); |
This file contains 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 otherResources = [ | |
'/about/', | |
'/contact/', | |
'/services/' | |
], | |
cacheCriticals = () => { | |
return caches.open(version).then( cache => { | |
cache.addAll(otherResources); // important, but not critical resources | |
return cache.addAll(criticalResources); // critical resources | |
}); |
This file contains 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 => { | |
event.respondWith( | |
// try fetching a resource from the network | |
fetch(event.request).then(response => { | |
// cache the resource and serve it | |
let responseCopy = response.clone(); | |
addToCache(event.request, responseCopy); // this is a custom function, I'll elaborate on it later | |
return response; | |
}) | |
.catch(() => { |
This file contains 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 => { | |
event.respondWith( | |
// check if the resource was cached before | |
caches.match(event.request).then(response => { | |
// serve the resource if cached, otherwise fetch it through the network | |
return response || fetch(event.request).then(response => { | |
// cache the newly fetched resource and serve it | |
let responseCopy = response.clone(); | |
addToCache(event.request, responseCopy); // this is a custom function, I'll elaborate on it later | |
return response; |
OlderNewer