|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +Copyright (c) 2016-2018 Pierre-Yves Lapersonne (Mail: dev@pylapersonne.info) |
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +The above copyright notice and this permission notice shall be included in all |
| 11 | +copies or substantial portions of the Software. |
| 12 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | +SOFTWARE. |
| 19 | +*/ |
| 20 | +/* |
| 21 | +✿✿✿✿ ʕ •ᴥ•ʔ/ ︻デ═一 |
| 22 | +*/ |
| 23 | + |
| 24 | +/** |
| 25 | +* @file serviceworker.js |
| 26 | +* @brief A service worker to use as a cache system in case of missing Internet / data / network connection |
| 27 | +* Please note that you must serve this web app through HTTPS or localhost so as to use Service Workers. |
| 28 | +* @author Pierre-Yves Lapersonne |
| 29 | +* @version 2.1.0 |
| 30 | +* @since 22/03/2018 |
| 31 | +*/ |
| 32 | + |
| 33 | +"use strict"; |
| 34 | + |
| 35 | +let CACHE_NAME = 'tipsntools-cache-v3'; |
| 36 | +let urlsToCache = [ |
| 37 | + |
| 38 | + './', |
| 39 | + |
| 40 | + './webapp/styles/devices.css', |
| 41 | + './webapp/styles/filter.css', |
| 42 | + './webapp/styles/global.css', |
| 43 | + './webapp/styles/header.css', |
| 44 | + './webapp/styles/medias.css', |
| 45 | + './webapp/styles/navigationButtons.css', |
| 46 | + './webapp/styles/references.css', |
| 47 | + './webapp/styles/socs.css', |
| 48 | + './webapp/styles/tables.css', |
| 49 | + './webapp/styles/tools.css', |
| 50 | + |
| 51 | + './webapp/pictures/favicon.png', |
| 52 | + './webapp/pictures/logo.svg', |
| 53 | + './webapp/pictures/navigation-down.svg', |
| 54 | + './webapp/pictures/navigation-up.svg', |
| 55 | + |
| 56 | + './webapp/logic/adapter.js', |
| 57 | + './webapp/logic/config.js', |
| 58 | + './webapp/logic/strings.js', |
| 59 | + './webapp/logic/glue.js', |
| 60 | + './webapp/logic/indexeddb.js', |
| 61 | + './webapp/logic/scrollButtons.js', |
| 62 | + './webapp/logic/smoothScroll.js', |
| 63 | + './webapp/logic/webworker.js', |
| 64 | + './webapp/logic/window.js' |
| 65 | + |
| 66 | +]; |
| 67 | + |
| 68 | +/** |
| 69 | + * When the Service Worker in installing |
| 70 | + */ |
| 71 | +self.addEventListener('install', function(event) { |
| 72 | + event.waitUntil( |
| 73 | + caches.open(CACHE_NAME).then(function(cache) { |
| 74 | + console.log('Service Worker: opened cache'); |
| 75 | + return cache.addAll(urlsToCache); |
| 76 | + }) |
| 77 | + ); |
| 78 | +}); |
| 79 | + |
| 80 | +/** |
| 81 | + * When the Service Worker is fetching resources |
| 82 | + */ |
| 83 | +self.addEventListener('fetch', function(event) { |
| 84 | + event.respondWith( |
| 85 | + caches.match(event.request).then(function(response) { |
| 86 | + console.log('Service Worker: fetch something - '+event.request); |
| 87 | + // Cache hit - return response |
| 88 | + if (response) { |
| 89 | + return response; |
| 90 | + } |
| 91 | + // Clone it to consume the request 2 times (by cache and by browser) |
| 92 | + let fetchRequest = event.request.clone(); |
| 93 | + console.log('Service Worker: fetch something - '+fetchRequest); |
| 94 | + return fetch(fetchRequest).then( |
| 95 | + function(response) { |
| 96 | + // Check if we received a valid response |
| 97 | + if(!response || response.status !== 200 || response.type !== 'basic') { |
| 98 | + console.log('Service Worker: fetch response '+response); |
| 99 | + return response; |
| 100 | + } |
| 101 | + // Clone it to consume the reponse 2 times (by cache and by browser) |
| 102 | + var responseToCache = response.clone(); |
| 103 | + caches.open(CACHE_NAME).then(function(cache) { |
| 104 | + console.log('Service Worker: response to cache - '+responseToCache); |
| 105 | + cache.put(event.request, responseToCache); |
| 106 | + }); |
| 107 | + return response; |
| 108 | + } // End of function(response) |
| 109 | + ); // End of return fetch(fetchRequest).then |
| 110 | + }) |
| 111 | + ); // End of event.respondWith |
| 112 | + }); |
0 commit comments