Created
November 27, 2018 07:19
-
-
Save pakastin/e792b687af09a46bdad6a5424a6c1b84 to your computer and use it in GitHub Desktop.
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 CACHE = 'site-name-here'; | |
self.addEventListener('fetch', (event) => { | |
event.respondWith(fetch(event.request) | |
.then((response) => { | |
if (!response || response.status !== 200 || response.type !== 'basic') { | |
return response; | |
} | |
const clonedResponse = response.clone(); | |
caches.open(CACHE) | |
.then((cache) => { | |
cache.put(event.request, clonedResponse); | |
}); | |
return response; | |
}).catch(err => { | |
console.error(new Error(err)); | |
return caches.match(event.request); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment