Skip to content

Instantly share code, notes, and snippets.

@ahmetilhn
Created May 2, 2024 20:19
Show Gist options
  • Save ahmetilhn/e9554484ed3f2517958f6256c387228b to your computer and use it in GitHub Desktop.
Save ahmetilhn/e9554484ed3f2517958f6256c387228b to your computer and use it in GitHub Desktop.
Service worker cache first strategy
self.addEventListener('fetch', async event => {
// İlk olarak önbellekte eşleşen bir yanıt arayın
const cachedResponse = await caches.match(event.request);
if (cachedResponse) {
// Önbellekte eşleşen bir yanıt varsa, onu dön
return event.respondWith(cachedResponse);
}
// Eğer önbellekte yoksa, ağdan istek yap
const fetchResponse = await fetch(event.request);
const cache = await caches.open('v1');
// Ağdan alınan yanıtı önbelleğe kaydet
cache.put(event.request, fetchResponse.clone());
return event.respondWith(fetchResponse);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment