Created
January 21, 2018 00:18
-
-
Save ronanyeah/d2a006784a7d0387c81ee3bfd59c51df to your computer and use it in GitHub Desktop.
SW Caching Strategy
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 assets = [].map(url => self.location.origin + url); | |
self.addEventListener("fetch", e => | |
e.respondWith( | |
fetch(e.request) | |
.then(res => { | |
if (res.ok && assets.includes(e.request.url)) { | |
caches.open("cache-name").then(cache => cache.put(e.request, res)); | |
} | |
return res.clone(); | |
}) | |
.catch(err => caches.match(e.request).then(res => res || err)) | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment