Last active
February 16, 2023 08:48
-
-
Save patissier-boulanger/e194ad9f0e70f6c323413a04953480ec to your computer and use it in GitHub Desktop.
Cache worker
This file contains hidden or 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
// 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