Skip to content

Instantly share code, notes, and snippets.

@scsskid
Created December 21, 2020 16:30
Show Gist options
  • Save scsskid/c18a955343b1a68c823841189271d84c to your computer and use it in GitHub Desktop.
Save scsskid/c18a955343b1a68c823841189271d84c to your computer and use it in GitHub Desktop.
[SW Fetch handler async/await sytnax] #serviceworker
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