Created
July 21, 2017 04:34
-
-
Save profburnes/2a79ff6c345b33aa0b40bc670215b5cc to your computer and use it in GitHub Desktop.
Service Worker
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
//nome do cache | |
var cacheName = 'akiexpress'; | |
//arquivos a serem cacheados | |
var filesToCache = [ | |
"index.html", | |
"categoria.html", | |
"carrinho.html", | |
"produto.html", | |
"imgs/logo.png", | |
"imgs/load.gif", | |
"imgs/icon-256.png", | |
"css/materialize.min.css", | |
"css/style.css", | |
"js/jquery-3.2.1.min.js", | |
"js/materialize.js", | |
"js/functions.js", | |
"js/home.js" | |
]; | |
//instalando o service worker | |
self.addEventListener('install', function(e) { | |
console.log('Service Worker Instalado'); | |
e.waitUntil( | |
caches.open(cacheName).then(function(cache) { | |
console.log('[ServiceWorker] Adicionando dados no cache'); | |
return cache.addAll(filesToCache); | |
}) | |
); | |
}); | |
//atualizar o cache | |
self.addEventListener('activate', function(e) { | |
console.log('Service Worker Ativo'); | |
e.waitUntil( | |
caches.keys().then(function(keyList) { | |
return Promise.all(keyList.map(function(key) { | |
if (key !== cacheName) { | |
console.log('Service Worker Cache Antigo Removido', key); | |
return caches.delete(key); | |
} | |
})); | |
}) | |
); | |
return self.clients.claim(); | |
}); | |
//verificar se informação está disponivel no cache | |
self.addEventListener('fetch', function(e) { | |
console.log('Service Worker Fetch', e.request.url); | |
e.respondWith( | |
caches.match(e.request).then(function(response) { | |
return response || fetch(e.request); | |
}) | |
); | |
}); | |
//retirado de https://developers.google.com/web/fundamentals/getting-started/codelabs/your-first-pwapp/?hl=pt-br |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment