Skip to content

Instantly share code, notes, and snippets.

@ronanyeah
Created January 21, 2018 00:18
Show Gist options
  • Save ronanyeah/d2a006784a7d0387c81ee3bfd59c51df to your computer and use it in GitHub Desktop.
Save ronanyeah/d2a006784a7d0387c81ee3bfd59c51df to your computer and use it in GitHub Desktop.
SW Caching Strategy
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