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', function(event) { | |
event.respondWith( | |
caches.match(event.request) | |
.then(function(response) { | |
// Cache hit - return response | |
if (response) { | |
return response; | |
} | |
// IMPORTANT: Cloner la requête. |
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('activate', function(event) { | |
var cacheWhitelist = [CACHE_NAME]; | |
event.waitUntil( | |
// Check de toutes les clés de cache. | |
caches.keys().then(function(cacheNames) { | |
return Promise.all( | |
cacheNames.map(function(cacheName) { | |
if (cacheWhitelist.indexOf(cacheName) === -1) { |
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 CACHE_NAME = 'my-site-cache-v1'; | |
var urlsToCache = [ | |
'/', | |
'/styles/main.[hash].css', | |
'/script/main.[hash].js' | |
]; | |
self.addEventListener('install', function(event) { | |
// Perform install steps | |
event.waitUntil( |
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('/sw.js') | |
.then((reg) => { | |
// registration worked | |
console.log('Enregistrement réussi'); | |
}).catch((error) => { | |
// registration failed | |
console.log('Erreur : ' + error); | |
}); | |
} |