Skip to content

Instantly share code, notes, and snippets.

@steph-crown
Created January 28, 2022 14:00
Show Gist options
  • Save steph-crown/0f9041983331fe76cdb22ecdf4a07710 to your computer and use it in GitHub Desktop.
Save steph-crown/0f9041983331fe76cdb22ecdf4a07710 to your computer and use it in GitHub Desktop.
Fetch event of service worker
// Runs whenever there is a fetch request
self.addEventListener("fetch", (event) => {
// Checks the cache to find matching request.
// If there is a cached version of the request, it returns the cached version, otherwise it makes a new request.
event.respondWith(
caches.match(event.request).then((res) => {
return res || fetch(event.request);
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment