Skip to content

Instantly share code, notes, and snippets.

@evertonstrack
Created December 7, 2018 10:43
Show Gist options
  • Save evertonstrack/4c42354022bdf6620036c2a941920dc4 to your computer and use it in GitHub Desktop.
Save evertonstrack/4c42354022bdf6620036c2a941920dc4 to your computer and use it in GitHub Desktop.
// Reponde o request direto do cache
this.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Retorna o cache
if (response) {
return response;
}
// Faz a requisição
return fetch(event.request);
})
.catch(() => {
// Mostra uma página de offline
return caches.match('/offline.html');
})
)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment