Skip to content

Instantly share code, notes, and snippets.

@patissier-boulanger
Last active February 16, 2023 08:48
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 patissier-boulanger/e194ad9f0e70f6c323413a04953480ec to your computer and use it in GitHub Desktop.
Save patissier-boulanger/e194ad9f0e70f6c323413a04953480ec to your computer and use it in GitHub Desktop.
Cache worker
// inside service worker eg. sw.ts
self.addEventListener('fetch', function (event) {
if (event.request.url.endsWith('.mp4')) {
event.respondWith(
caches.match(event.request).then(function (response) {
if (response) {
return response
} else {
return fetch(event.request).then(function (response) {
const responseToCache = response.clone()
caches.open('cache_name').then(function (cache) {
cache.put(event.request, responseToCache)
})
return response
})
}
})
)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment