summaryrefslogblamecommitdiffstats
path: root/sworker.js
blob: b80e7ae27c7f18038248bc7a73eda9aa95d2161a (plain) (tree)
1
2
3
4
5
6
7
8
9
                               



                      



                               

                  










                      

                 
                
  
 
                                           
                    
                          




                                                                                            

   
                                            

                                     
                                             

                                          
     

    
 
                                                




















                                                                         
   
var VERSION = 'v44 2022-03-31';
var toCache = [
  '/',
  'activePage.js',
  'activePage.js.map',
  'android-chrome-192x192.png',
  'android-chrome-512x512.png',
  'apple-touch-icon.png',
  'browser.js',
  'config.js',
  'config.js.map',
  'favicon.ico',
  'favicon-16x16.png',
  'favicon-32x32.png',
  'index_en.html',
  'index.html',
  'jsconfig.json',
  'require.js',
  'screen.css',
  'site.webmanifest',
  'sworker.js',
  'system.js',
  'zalmy.js',
  'zalmy.js.map',
  'zalmy.webapp'
];

self.addEventListener('install', event => {
    event.waitUntil(
      caches.open(VERSION)
      .then(
        c => c.addAll(toCache)
          .then(v => console.log('********* Yay!'), e => console.log('************* Error'))
      )
    );
});

self.addEventListener('activate', event => {
  event.waitUntil(caches.keys().then(
        keys => Promise.all(
          keys.filter(key => key != VERSION).
            map(key => caches.delete(key))
        )
    )
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(caches.match(event.request).then(function(response) {
    // caches.match() always resolves
    // but in case of success response will have value
    if (response !== undefined) {
      return response;
    } else {
      return fetch(event.request).then(function (response) {
        // response may be used only once
        // we need to save clone to put one copy in cache
        // and serve second one
        let responseClone = response.clone();

        caches.open(VERSION).then(function (cache) {
          cache.put(event.request, responseClone);
        });
        return response;
      }).catch(function () {
        return caches.match('/sw-test/gallery/myLittleVader.jpg');
      });
    }
  }));
});