Created
May 2, 2024 20:22
-
-
Save ahmetilhn/29e7d2c766f778b3f3fd9dd506bc5ab0 to your computer and use it in GitHub Desktop.
Service worker network 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 => { | |
try { | |
// İlk olarak 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); | |
} catch (error) { | |
// Ağ hatası durumunda önbellekten dön | |
const cachedResponse = await caches.match(event.request); | |
return event.respondWith(cachedResponse); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment