text: // Copyright 2017 Kundan Singh.
  // https://kundan.me
  // 
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  // 
  //      http://www.apache.org/licenses/LICENSE-2.0
  // 
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  
  var cacheName = 'weatherPWA-step-6-1';
  var dataCacheName = 'weatherData-v1';
  
  var filesToCache = [
      '/',
      '/index.html',
      '/scripts/app.js',
      '/styles/inline.css',
      '/images/clear.png',
      '/images/cloudy-scattered-showers.png',
      '/images/cloudy.png',
      '/images/fog.png',
      '/images/ic_add_white_24px.svg',
      '/images/ic_refresh_white_24px.svg',
      '/images/partly-cloudy.png',
      '/images/rain.png',
      '/images/scattered-showers.png',
      '/images/sleet.png',
      '/images/snow.png',
      '/images/thunderstorm.png',
      '/images/wind.png'
  ];
  
  self.addEventListener('install', function(e) {
      console.log('[ServiceWorker] Install');
      e.waitUntil(
          caches.open(cacheName).then(function(cache) {
              console.log('[ServiceWorker] Caching app shell');
              return cache.addAll(filesToCache);
          })
      );
  });
  
  self.addEventListener('activate', function(e) {
      console.log('[ServiceWorker] Activate');
      e.waitUntil(
          caches.keys().then(function(keyList) {
              return Promise.all(keyList.map(function(key) {
                  if (key !== cacheName && key !== dataCacheName) {
                      console.log('[ServiceWorker] Removing Old Cache', key);
                      return caches.delete(key);
                  }
              }));
          })
      );
      return self.clients.claim();
  });
  
  self.addEventListener('fetch', function(e) {
      console.log('[ServiceWorker] Fetch', e.request.url);
  
      var dataUrl = 'https://query.yahooapis.com/v1/public/yql';
      if (e.request.url.indexOf(dataUrl) > -1) {
          e.respondWith(
              caches.open(dataCacheName).then(function(cache) {
                  return fetch(e.request).then(function(response) {
                      cache.put(e.request.url, response.clone());
                      return response;
                  });
              })
          );
      } else {
          e.respondWith(
              caches.match(e.request).then(function(response) {
                  return response || fetch(e.request);
              })
          );
      }
  });