Skip to content

Instantly share code, notes, and snippets.

@ahmetilhn
Created May 2, 2024 20:28
Show Gist options
  • Save ahmetilhn/6acbdb36e3af597840e33db116a3a691 to your computer and use it in GitHub Desktop.
Save ahmetilhn/6acbdb36e3af597840e33db116a3a691 to your computer and use it in GitHub Desktop.
Service worker stale revalidate cache strategy
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