Skip to content

Instantly share code, notes, and snippets.

@PhilJ
Last active September 7, 2021 07:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PhilJ/cd82a7616061da7b5e0c4e0444343ca6 to your computer and use it in GitHub Desktop.
Save PhilJ/cd82a7616061da7b5e0c4e0444343ca6 to your computer and use it in GitHub Desktop.
// this is the service worker which intercepts all http requests
self.addEventListener('fetch', function fetcher (event) {
var request = event.request;
// check if request
if (request.url.indexOf('assets.contentful.com') > -1) {
// contentful asset detected
event.respondWith(
caches.match(event.request).then(function(response) {
// return from cache, otherwise fetch from network
return response || fetch(request);
})
);
}
// otherwise: ignore event
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment