diff --git a/manifest.json b/manifest.json new file mode 100755 index 00000000..f2b8582d --- /dev/null +++ b/manifest.json @@ -0,0 +1,21 @@ +{ + "name": "PHP Server Monitor", + "short_name": "PSM", + "icons": [{ + "src": "static/phpservermon.png", + "sizes": "128x128", + "type": "image/png" + }, { + "src": "favicon.png", + "sizes": "32x32", + "type": "image/png" + } ,{ + "src": "favicon.ico", + "sizes": "32x32", + "type": "image/x-icon" + }], + "start_url": "/index.php", + "display": "standalone", + "background_color": "#3E4EB8", + "theme_color": "#ffffff" +} diff --git a/service-worker.js b/service-worker.js new file mode 100755 index 00000000..1d2bbcf4 --- /dev/null +++ b/service-worker.js @@ -0,0 +1,73 @@ +var dataCacheName = 'PSM-v1'; +var cacheName = 'PSM-PWA-final-1'; +var filesToCache = [ + '/', + '/index.php', + '/static/js/history.js', + '/static/js/script.js', + '/static/js/search.js', + '/static/css/bootstrap.min.css', + '/static/css/search.min.css', + '/static/css/style.min.css', + '/static/phpservermon.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('[Service Worker] Fetch', e.request.url); + var dataUrl = 'https://query.yahooapis.com/v1/public/yql'; + if (e.request.url.indexOf(dataUrl) > -1) { + /* + * When the request URL contains dataUrl, the app is asking for fresh + * weather data. In this case, the service worker always goes to the + * network and then caches the response. This is called the "Cache then + * network" strategy: + * https://jakearchibald.com/2014/offline-cookbook/#cache-then-network + */ + /* + 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 { + /* + * The app is asking for app shell files. In this scenario the app uses the + * "Cache, falling back to the network" offline strategy: + * https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network + */ + /* + e.respondWith( + caches.match(e.request).then(function(response) { + return response || fetch(e.request); + }) + ); + } +});*/ diff --git a/src/templates/default/main/body.tpl.html b/src/templates/default/main/body.tpl.html index 275a45b7..c566c44a 100644 --- a/src/templates/default/main/body.tpl.html +++ b/src/templates/default/main/body.tpl.html @@ -7,6 +7,16 @@ + + + + + + + + + + diff --git a/static/js/scripts.js b/static/js/scripts.js index 70a91d94..b9bcf079 100755 --- a/static/js/scripts.js +++ b/static/js/scripts.js @@ -153,4 +153,10 @@ if ($(".search_input").length > 0) { rel: 'stylesheet', href: 'static/css/search.min.css' }); +} + +if ('serviceWorker' in navigator) { + navigator.serviceWorker + .register('./service-worker.js') + .then(function() { console.log('Service Worker Registered'); }); } \ No newline at end of file