SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var CURRENT_CACHES = {
  2.     font: 'font-cache-v6',
  3.     css:'css-cache-v6',
  4.     js:'js-cache-v6',
  5.     site: 'site-cache-v6',
  6.     image: 'image-cache-v6'
  7. };
  8.  
  9. self.addEventListener('install', (event) => {
  10.     self.skipWaiting();
  11.     console.log('Service Worker has been installed');
  12. });
  13.  
  14. self.addEventListener('activate', (event) => {
  15.     var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
  16.         return CURRENT_CACHES[key];
  17.     });
  18.  
  19.     // Delete out of date caches
  20.     event.waitUntil(
  21.         caches.keys().then(function(cacheNames) {
  22.             return Promise.all(
  23.                 cacheNames.map(function(cacheName) {
  24.                     if (expectedCacheNames.indexOf(cacheName) == -1) {
  25.                         console.log('Deleting out of date cache:', cacheName);
  26.                         return caches.delete(cacheName);
  27.                     }
  28.                 })
  29.             );
  30.         })
  31.     );
  32.  
  33.     console.log('Service Worker has been activated');
  34. });
  35.  
  36. self.addEventListener('fetch', function(event) {
  37.     console.log('Fetching:', event.request.url);
  38.     event.respondWith(async function() {
  39.         const cachedResponse = await caches.match(event.request);
  40.         if (cachedResponse) {
  41.             console.log("\tCached version found: " + event.request.url);
  42.             return cachedResponse;
  43.         } else {
  44.             console.log("\tGetting from the Internet:" + event.request.url);
  45.             return await fetchAndCache(event.request);
  46.         }
  47.     }());
  48.  
  49. });
  50.  
  51. function fetchAndCache(request) {
  52.  
  53.     return fetch(request)
  54.         .then(function(response) {
  55.             // Check if we received a valid response
  56.             if (!response.ok) {
  57.                 return response;
  58.                 // throw Error(response.statusText);
  59.             }
  60.  
  61.             var url = new URL(request.url);
  62.             if (response.status < 400 &&
  63.                 response.type === 'basic' &&
  64.                 url.search.indexOf("mode=nocache") == -1 &&
  65.                 url.search.indexOf("bitrix") == -1
  66.             ) {
  67.                 var cur_cache;
  68.                 if (response.headers.get('content-type') &&
  69.                     response.headers.get('content-type').indexOf("application/javascript") >= 0) {
  70.                     cur_cache = CURRENT_CACHES.js;
  71.                 } else if (response.headers.get('content-type') &&
  72.                     response.headers.get('content-type').indexOf("text/css") >= 0) {
  73.                     cur_cache = CURRENT_CACHES.css;
  74.                 } else if (response.headers.get('content-type') &&
  75.                     response.headers.get('content-type').indexOf("font") >= 0) {
  76.                     cur_cache = CURRENT_CACHES.font;
  77.                 } else if (response.headers.get('content-type') &&
  78.                     response.headers.get('content-type').indexOf("image") >= 0) {
  79.                     cur_cache = CURRENT_CACHES.image;
  80.                 } else if (response.headers.get('content-type') &&
  81.                     response.headers.get('content-type').indexOf("text") >= 0) {
  82.                     cur_cache = CURRENT_CACHES.site;
  83.                 }
  84.                 if (cur_cache) {
  85.                     console.log('\tCaching the response to', request.url);
  86.                     return caches.open(cur_cache).then(function(cache) {
  87.                         cache.put(request, response.clone());
  88.                         return response;
  89.                     });
  90.                 }
  91.             }
  92.             return response;
  93.         })
  94.         .catch(function(error) {
  95.             console.log('Request failed for: ' + request.url, error);
  96.             throw error;
  97.         });
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top