Created
May 2, 2024 20:28
-
-
Save ahmetilhn/6acbdb36e3af597840e33db116a3a691 to your computer and use it in GitHub Desktop.
Service worker stale revalidate cache 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
self.addEventListener('fetch', async event => { | |
const cache = await caches.open('v1'); | |
// Önce önbellekteki yanıtı al, aynı zamanda ağdan yenile | |
const cachedResponse = await caches.match(event.request); | |
const fetchRes = await fetch(event.request) | |
// Ağdan alınan yanıtı önbelleğe kaydet | |
cache.put(event.request, fetchRes.clone()); | |
// Önbellekteki yanıtı kullan veya ağdan alınanı bekle | |
return event.respondWith(cachedResponse || fetchRes); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment