Created
December 21, 2020 16:30
-
-
Save scsskid/c18a955343b1a68c823841189271d84c to your computer and use it in GitHub Desktop.
[SW Fetch handler async/await sytnax] #serviceworker
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
addEventListener('fetch', event => { | |
// Prevent the default, and handle the request ourselves. | |
event.respondWith(async function() { | |
// Try to get the response from a cache. | |
const cachedResponse = await caches.match(event.request); | |
// Return it if we found one. | |
if (cachedResponse) return cachedResponse; | |
// If we didn't find a match in the cache, use the network. | |
return fetch(event.request); | |
}()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment