Created
May 2, 2024 20:19
-
-
Save ahmetilhn/e9554484ed3f2517958f6256c387228b to your computer and use it in GitHub Desktop.
Service worker cache first 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 => { | |
// İ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