Skip to content

Instantly share code, notes, and snippets.

@profburnes
Created July 21, 2017 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save profburnes/2a79ff6c345b33aa0b40bc670215b5cc to your computer and use it in GitHub Desktop.
Save profburnes/2a79ff6c345b33aa0b40bc670215b5cc to your computer and use it in GitHub Desktop.
Service Worker
//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