From Blush Monkey, 1 Month ago, written in Plain Text.
  1. self.addEventListener('fetch', function(event) {
  2.     console.log('Service Worker: Fetch', event.request.url);
  3.     event.respondWith(caches.match(event.request)
  4.         .then(function(response) {
  5.             if (response && response.ok) {
  6.                 console.log(event.request.url, "is cached");
  7.                 return response;
  8.             }
  9.  
  10.             if (self.online) {
  11.                 console.log("App is online, fetching", event.request.url);
  12.                 return fetch(event.request);
  13.             }
  14.  
  15.             // Anything else that's not in the cache should be refused.
  16.             console.log(event.request.url, "is not in the cache, refusing");
  17.             return Response();   // cache-only
  18.         })
  19.     );
  20. });
  21.  
captcha