Advertisement
Guest User

server-worker

a guest
Mar 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Set a name for the current cache
  2. var cacheName = 'v1';
  3.  
  4. // Default files to always cache
  5. var cacheFiles = [
  6.     './www/assets/sw/app.js',
  7.     'app.js'
  8. ]
  9.  
  10.  
  11. self.addEventListener('install', function(e) {
  12.     console.log('[ServiceWorker] Installed');
  13.  
  14.     // e.waitUntil Delays the event until the Promise is resolved
  15.     e.waitUntil(
  16.  
  17.         // Open the cache
  18.         caches.open(cacheName).then(function(cache) {
  19.  
  20.             // Add all the default files to the cache
  21.             console.log('[ServiceWorker] Caching cacheFiles');
  22.             return cache.addAll(cacheFiles);
  23.         })
  24.     ); // end e.waitUntil
  25. });
  26.  
  27. self.addEventListener('activate', function(e) {
  28.     console.log('[ServiceWorker] Activated');
  29.  
  30.     e.waitUntil(
  31.  
  32.         // Get all the cache keys (cacheName)
  33.         caches.keys().then(function(cacheNames) {
  34.             return Promise.all(cacheNames.map(function(thisCacheName) {
  35.  
  36.                 // If a cached item is saved under a previous cacheName
  37.                 if (thisCacheName !== cacheName) {
  38.  
  39.                     // Delete that cached file
  40.                     console.log('[ServiceWorker] Removing Cached Files from Cache - ', thisCacheName);
  41.                     return caches.delete(thisCacheName);
  42.                 }
  43.             }));
  44.         })
  45.     ); // end e.waitUntil
  46.  
  47. });
  48.  
  49.  
  50. self.addEventListener('fetch', function(e) {
  51.     console.log('[ServiceWorker] Fetch', e.request.url);
  52.  
  53.     // e.respondWidth Responds to the fetch event
  54.     e.respondWith(
  55.  
  56.         // Check in cache for the request being made
  57.         caches.match(e.request)
  58.  
  59.  
  60.             .then(function(response) {
  61.  
  62.                 // If the request is in the cache
  63.                 if ( response ) {
  64.                     console.log("[ServiceWorker] Found in Cache", e.request.url, response);
  65.                     // Return the cached version
  66.                     return response;
  67.                 }
  68.  
  69.                 // If the request is NOT in the cache, fetch and cache
  70.  
  71.                 var requestClone = e.request.clone();
  72.                 return fetch(requestClone)
  73.                     .then(function(response) {
  74.  
  75.                         if ( !response ) {
  76.                             console.log("[ServiceWorker] No response from fetch ")
  77.                             return response;
  78.                         }
  79.  
  80.                         var responseClone = response.clone();
  81.  
  82.                         //  Open the cache
  83.                         caches.open(cacheName).then(function(cache) {
  84.  
  85.                             // Put the fetched response in the cache
  86.                             cache.put(e.request, responseClone);
  87.                             console.log('[ServiceWorker] New Data Cached', e.request.url);
  88.  
  89.                             // Return the response
  90.                             return response;
  91.            
  92.                         }); // end caches.open
  93.  
  94.                     })
  95.                     .catch(function(err) {
  96.                         console.log('[ServiceWorker] Error Fetching & Caching New Data', err);
  97.                     });
  98.  
  99.  
  100.             }) // end caches.match(e.request)
  101.     ); // end e.respondWith
  102. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement