From 65e231588de9399e7ce06ec2626ca34730c0a28c Mon Sep 17 00:00:00 2001 From: Sebastien Date: Sat, 15 Sep 2018 11:54:34 +0300 Subject: [PATCH] Add connection fail message and auto cache refresh for service worker --- src/main/resources/static/js/navabar.js | 25 +++++++++++ src/main/resources/static/sw.js | 56 ++++++++++++++++++++---- src/main/resources/templates/header.html | 18 ++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/main/resources/static/js/navabar.js b/src/main/resources/static/js/navabar.js index acd0c03..0358232 100644 --- a/src/main/resources/static/js/navabar.js +++ b/src/main/resources/static/js/navabar.js @@ -22,6 +22,13 @@ $(document).ready(function() { dismissible: false // Modal can be dismissed by clicking outside of the modal }); + $('#modal_internet').modal({ + dismissible: false // Modal can be dismissed by clicking outside of the modal + }); + + + + nav_bar_account_link = $("#nav-bar-account"); input_name = $("#user_input"); @@ -37,6 +44,8 @@ $(document).ready(function() { connected(); } + checkConnection(); + }); @@ -198,5 +207,21 @@ function getGuild(){ error = true; } + }); +} + + +function checkConnection() { + $.ajax({ + type: "GET", + url: "/api/isReady", + success: function (data) { + console.log("Connection Ok"); + } + + }).fail(function (data) { + console.error("Connection fail!"); + $('#modal_internet').modal('open'); + }); } \ No newline at end of file diff --git a/src/main/resources/static/sw.js b/src/main/resources/static/sw.js index 786fa73..2a09125 100644 --- a/src/main/resources/static/sw.js +++ b/src/main/resources/static/sw.js @@ -1,5 +1,4 @@ - -var CACHE_NAME = 'Clap-Trap-Bot-V1'; +var CACHE_NAME = 'Clap-Trap-Bot-V0.1'; var urlsToCache = [ '/', '/music', @@ -12,23 +11,22 @@ var urlsToCache = [ ]; - - -self.addEventListener('install', function(event) { +self.addEventListener('install', function (event) { // Perform install steps event.waitUntil( caches.open(CACHE_NAME) - .then(function(cache) { + .then(function (cache) { console.log('Opened cache'); return cache.addAll(urlsToCache); }) ); }); -self.addEventListener('fetch', function(event) { +self.addEventListener('fetch', function (event) { + console.log("fetch"); event.respondWith( caches.match(event.request) - .then(function(response) { + .then(function (response) { // Cache hit - return response if (response) { return response; @@ -37,4 +35,46 @@ self.addEventListener('fetch', function(event) { } ) ); + + event.waitUntil( + update(event.request) + .then(refresh) + ); + + function update(request) { + console.log(request); + console.log(caches.match(request)); + return caches.match(request).then( + function (response) { + if (response) { + return caches.open(CACHE_NAME).then(function (cache) { + return fetch(request).then(function (response) { + return cache.put(request, response.clone()).then(function () { + return response; + }); + }); + }); + } + } + ); + } + + function refresh(response) { + if(response){ + console.log("refresh"); + return self.clients.matchAll().then(function (clients) { + clients.forEach(function (client) { + var message = { + type: 'refresh', + url: response.url, + eTag: response.headers.get('eTag') + }; + client.postMessage(JSON.stringify(message)); + }); + }); + } + + } + + }); \ No newline at end of file diff --git a/src/main/resources/templates/header.html b/src/main/resources/templates/header.html index fdd97bb..dad8103 100644 --- a/src/main/resources/templates/header.html +++ b/src/main/resources/templates/header.html @@ -162,6 +162,24 @@ + + + + + + +