From 571c27a64e8452a58a05d85bf40e1d7885d2dd1b Mon Sep 17 00:00:00 2001 From: sijanec Date: Mon, 8 Jun 2020 22:43:11 +0200 Subject: added update mechanism, DO NOT MERGE b4 seeing commit notes! this "update mechanism" only deletes old caches. old means caches that are caches with names other than the name in cache_name.txt. It is important not to cache cache_name.txt and always serve it with HTTP headers that allow no HTTP level caching. Update checking occurs every 300 seconds and if a new cache is released and sw.js is not reinstalled, this timer will clear the cache. the old update method did not work since it relied on sw being reloaded often, which is not the case on iPhone devices, where Safari runs almost the whole uptime of the phone and it's not common for people to shut down their precious little iPhones. it worked on android as android is known for killing apps and chrome is no exception. --- assets/js/app.js.bvr | 115 +++++++++++++++++++++++++++------------------ assets/js/setup-storage.js | 77 +++++++++++++++--------------- 2 files changed, 108 insertions(+), 84 deletions(-) (limited to 'assets/js') diff --git a/assets/js/app.js.bvr b/assets/js/app.js.bvr index d19c1d6..0a91078 100644 --- a/assets/js/app.js.bvr +++ b/assets/js/app.js.bvr @@ -1,20 +1,20 @@ <@?i global@> const app_version = "<@?g app_version@>"; const previous_commit = "<@?g latest_commit@>"; - +const BEZIAPP_UPDATE_INTERVAL = 300; // update vsakih 300 sekund if ("serviceWorker" in navigator) { - navigator.serviceWorker.register("/sw.js") - .then(() => { }) - .catch((err) => console.log("Service worker registration failed", err)); + navigator.serviceWorker.register("/sw.js") + .then(() => { }) + .catch((err) => console.log("Service worker registration failed", err)); } // Listen to messages from service workers. if (navigator.serviceWorker) { - navigator.serviceWorker.addEventListener('message', (event) => { - if (event.data.msg === "install") { - window.location.replace("/index.html"); - } - }); + navigator.serviceWorker.addEventListener('message', (event) => { + if (event.data.msg === "install") { + window.location.replace("/index.html"); + } + }); } /** @@ -24,12 +24,12 @@ if (navigator.serviceWorker) { * @param {string} devmsg Developer-friendly message */ async function UIAlert(usermsg, devmsg) { - if (true) { // če bo kakšen dev switch? - M.toast( { html: usermsg } ); - console.log(`[BežiApp UIAlert] ${usermsg} ${devmsg}`); - } else { - M.toast( { html: `${usermsg} ${devmsg}` } ); - } + if (true) { // če bo kakšen dev switch? + M.toast({ html: usermsg }); + console.log(`[BežiApp UIAlert] ${usermsg} ${devmsg}`); + } else { + M.toast({ html: `${usermsg} ${devmsg}` }); + } } /** @@ -37,44 +37,69 @@ async function UIAlert(usermsg, devmsg) { * @param {Object} err GSEC error object */ function gsecErrorHandlerUI(err) { - console.log(`gsecErrorHanderUI: handling ${err}`); - if(err == GSEC_ERR_NET || err == GSEC_ERR_NET_POSTBACK_GET || - err == GSEC_ERR_NET_POSTBACK_POST) { + console.log(`gsecErrorHanderUI: handling ${err}`); + if (err == GSEC_ERR_NET || err == GSEC_ERR_NET_POSTBACK_GET || + err == GSEC_ERR_NET_POSTBACK_POST) { - UIAlert( D("gsecErrNet") ); - } else if(err == GSEC_ERR_LOGIN) { - UIAlert( D("gsecErrLogin") ); - localforage.setItem("logged_in", false).then( () => { - window.location.replace("/index.html"); - }); - } else { - UIAlert( D("gsecErrOther") ); - } + UIAlert(D("gsecErrNet")); + } else if (err == GSEC_ERR_LOGIN) { + UIAlert(D("gsecErrLogin")); + localforage.setItem("logged_in", false).then(() => { + window.location.replace("/index.html"); + }); + } else { + UIAlert(D("gsecErrOther")); + } } +var update_app_function = async function () { + $.get("/cache_name.txt", (data, status) => { + var cache_name = data.split("///")[1].split("|||")[0]; + var data_to_send = { + action: "checkversion", + valid_cache_name: cache_name + } + navigator.serviceWorker.controller.postMessage(JSON.stringify(data_to_send)); + }); +} var error_report_function = async function (msg, url, lineNo, columnNo, error) { - localforage.getItem("errorReporting").then(async function(value) { - let selectedE = value; - if(value == null || value.length < 1) { - selectedE = "on"; - } - if(selectedE == "on") { - var data = {}; - data.error = {"msg": msg, "url": url, "line": lineNo, "column": columnNo, "obj": error}; - data.client = {"ua": navigator.userAgent, "app_version": app_version, "previous_commit": previous_commit, "username": null}; + // catching everything here so no looping error shit. that's the last thing we want + try { + localforage.getItem("errorReporting").then(async function (value) { + let selectedE = value; + if (value == null || value.length < 1) { + selectedE = "on"; + } + if (selectedE == "on") { + var data = {}; + data.error = { "msg": msg, "url": url, "line": lineNo, "column": columnNo, "obj": error }; + data.client = { "ua": navigator.userAgent, "app_version": app_version, "previous_commit": previous_commit, "username": null }; - // Load required data - data.client.username = await localforage.getItem("username"); + // Load required data + data.client.username = await localforage.getItem("username"); - data.type = "error"; - $.post("https://beziapp-report.gimb.tk/", data); - } else { - console.log("error not reported as reporting is disabled!"); - } - }).catch(() => {}); - return false; + data.type = "error"; + $.post("https://beziapp-report.gimb.tk/", data); + } else { + console.log("error not reported as reporting is disabled!"); + } + }).catch(() => { }); + return false; + } catch (e) { + console.log("error_erport_function: !!! ERROR! (caught) - probably some network error."); + } } window.onerror = error_report_function; window.onunhandledrejection = error_report_function; + + +document.addEventListener("DOMContentLoaded", () => { + localforage.getItem("lastUpdate").then((data) => { + if(Math.floor(Date.now() / 1000) > data + BEZIAPP_UPDATE_INTERVAL) { + // trigger an update + update_app_function(); + } + }); +}); \ No newline at end of file diff --git a/assets/js/setup-storage.js b/assets/js/setup-storage.js index c862d5f..b29c959 100644 --- a/assets/js/setup-storage.js +++ b/assets/js/setup-storage.js @@ -1,44 +1,43 @@ async function setupStorage(force = false) { - let logged_in; - promises_check_if_already_installed = [ - localforage.getItem("logged_in").then( function(val) { - console.log("[setupStorage] logged in status: " + val); - logged_in = val; - }) - ]; - await Promise.all(promises_check_if_already_installed); + let logged_in; + promises_check_if_already_installed = [ + localforage.getItem("logged_in").then(function (val) { + console.log("[setupStorage] logged in status: " + val); + logged_in = val; + }) + ]; + await Promise.all(promises_check_if_already_installed); - let promises_update = [ - localforage.setItem("profile", {}), - localforage.setItem("timetable", []), - localforage.setItem("teachers", []), - localforage.setItem("gradings", []), - localforage.setItem("grades", []), - localforage.setItem("absences", {}), - localforage.setItem("messages", [[], [], []]), // see messages.js:129, commit 8eb9ca9caca30fbbe023243657535ab4088be377 - localforage.setItem("directory", {}), //\\ well I could remember my own code but I didn't. - localforage.setItem("meals", {}), - localforage.setItem("chosenLang", "en"), - localforage.setItem("theme", "light"), - localforage.setItem("errorReporting", "on"), - localforage.setItem("triggerWarningAccepted", false) - ]; + let promises_update = [ + localforage.setItem("profile", {}), + localforage.setItem("timetable", []), + localforage.setItem("teachers", []), + localforage.setItem("gradings", []), + localforage.setItem("grades", []), + localforage.setItem("absences", {}), + localforage.setItem("messages", [[], [], []]), // see messages.js:129, commit 8eb9ca9caca30fbbe023243657535ab4088be377 + localforage.setItem("directory", {}), //\\ well I could remember my own code but I didn't. + localforage.setItem("meals", {}), + localforage.setItem("chosenLang", "en"), + localforage.setItem("theme", "light"), + localforage.setItem("errorReporting", "on"), + localforage.setItem("lastUpdate", 0), + localforage.setItem("triggerWarningAccepted", false) + ]; - if (logged_in && force == false) { // torej, če je že bila prijava narejena, ne posodobi backwards-compatible vrednosti (username, password,...) - await Promise.all(promises_update); - console.log("[setupStorage] user logged in: only updated"); - } else { + if (logged_in && force == false) { // torej, če je že bila prijava narejena, ne posodobi backwards-compatible vrednosti (username, password,...) + await Promise.all(promises_update); + console.log("[setupStorage] user logged in: only updated"); + } else { - let promises_first_install = [ - localforage.setItem("logged_in", false), - localforage.setItem("username", ""), - localforage.setItem("password", ""), - localforage.setItem("chosenLang", "en"), - localforage.setItem("theme", "light"), - localforage.setItem("triggerWarningAccepted", false) - ]; - await localforage.clear(); - await Promise.all(promises_first_install); - console.log("[setupStorage] user not logged in: set up whole database"); - } + let promises_first_install = [ + localforage.setItem("logged_in", false), + localforage.setItem("username", ""), + localforage.setItem("password", ""), + ]; + await localforage.clear(); + await Promise.all(promises_first_install); + await Promise.all(promises_update); + console.log("[setupStorage] user not logged in: set up whole database"); + } } -- cgit v1.2.3 From f1e3feaf44adb79aec054fce431b0a235a929466 Mon Sep 17 00:00:00 2001 From: sijanec Date: Mon, 8 Jun 2020 22:56:22 +0200 Subject: DNM+in case you haven't read the prev. commit --- assets/js/app.js.bvr | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'assets/js') diff --git a/assets/js/app.js.bvr b/assets/js/app.js.bvr index 0a91078..5b74aba 100644 --- a/assets/js/app.js.bvr +++ b/assets/js/app.js.bvr @@ -2,6 +2,7 @@ const app_version = "<@?g app_version@>"; const previous_commit = "<@?g latest_commit@>"; const BEZIAPP_UPDATE_INTERVAL = 300; // update vsakih 300 sekund + if ("serviceWorker" in navigator) { navigator.serviceWorker.register("/sw.js") .then(() => { }) @@ -59,7 +60,11 @@ var update_app_function = async function () { action: "checkversion", valid_cache_name: cache_name } - navigator.serviceWorker.controller.postMessage(JSON.stringify(data_to_send)); + try { + navigator.serviceWorker.controller.postMessage(JSON.stringify(data_to_send)); + } catch (e) { + console.log("update requested but sw is not available in app.js"); + } }); } @@ -96,10 +101,12 @@ window.onunhandledrejection = error_report_function; document.addEventListener("DOMContentLoaded", () => { - localforage.getItem("lastUpdate").then((data) => { - if(Math.floor(Date.now() / 1000) > data + BEZIAPP_UPDATE_INTERVAL) { - // trigger an update - update_app_function(); - } - }); + var update_interval = setInterval( () => { // ok, it's value is never read, so what?! + localforage.getItem("lastUpdate").then((data) => { + if(Math.floor(Date.now() / 1000) > data + BEZIAPP_UPDATE_INTERVAL) { + // trigger an update + update_app_function(); + } + }); + }, 1000*BEZIAPP_UPDATE_INTERVAL); }); \ No newline at end of file -- cgit v1.2.3 From 5f22473be3faa9cd2a50444f8bea4af46725c030 Mon Sep 17 00:00:00 2001 From: sijanec Date: Mon, 8 Jun 2020 23:13:49 +0200 Subject: added some protection, proby can merge now --- assets/js/app.js.bvr | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'assets/js') diff --git a/assets/js/app.js.bvr b/assets/js/app.js.bvr index 5b74aba..0bc7bcb 100644 --- a/assets/js/app.js.bvr +++ b/assets/js/app.js.bvr @@ -54,18 +54,22 @@ function gsecErrorHandlerUI(err) { } var update_app_function = async function () { - $.get("/cache_name.txt", (data, status) => { - var cache_name = data.split("///")[1].split("|||")[0]; - var data_to_send = { - action: "checkversion", - valid_cache_name: cache_name - } - try { - navigator.serviceWorker.controller.postMessage(JSON.stringify(data_to_send)); - } catch (e) { - console.log("update requested but sw is not available in app.js"); - } - }); + try { + $.get("/cache_name.txt", (data, status) => { + var cache_name = data.split("///")[1].split("|||")[0]; + var data_to_send = { + action: "checkversion", + valid_cache_name: cache_name + } + try { + navigator.serviceWorker.controller.postMessage(JSON.stringify(data_to_send)); + } catch (e) { + console.log("update requested but sw is not available in app.js"); + } + }); + } catch (e) { + console.log("update requested but failed because of network error probably in update_app_function in app.js"); + } } var error_report_function = async function (msg, url, lineNo, columnNo, error) { @@ -101,12 +105,12 @@ window.onunhandledrejection = error_report_function; document.addEventListener("DOMContentLoaded", () => { - var update_interval = setInterval( () => { // ok, it's value is never read, so what?! + var update_interval = setInterval(() => { // ok, it's value is never read, so what?! localforage.getItem("lastUpdate").then((data) => { - if(Math.floor(Date.now() / 1000) > data + BEZIAPP_UPDATE_INTERVAL) { + if (Math.floor(Date.now() / 1000) > data + BEZIAPP_UPDATE_INTERVAL) { // trigger an update update_app_function(); } }); - }, 1000*BEZIAPP_UPDATE_INTERVAL); + }, 1000 * BEZIAPP_UPDATE_INTERVAL); }); \ No newline at end of file -- cgit v1.2.3 From 31ca33571c90a50d3b1a0d91ece764d2054d05bc Mon Sep 17 00:00:00 2001 From: sijanec Date: Mon, 8 Jun 2020 23:27:50 +0200 Subject: killing the cache ... good to go --- assets/js/app.js.bvr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'assets/js') diff --git a/assets/js/app.js.bvr b/assets/js/app.js.bvr index 0bc7bcb..c252c40 100644 --- a/assets/js/app.js.bvr +++ b/assets/js/app.js.bvr @@ -55,7 +55,7 @@ function gsecErrorHandlerUI(err) { var update_app_function = async function () { try { - $.get("/cache_name.txt", (data, status) => { + $.get("/cache_name.txt?cache_kill="+Date.now(), (data, status) => { var cache_name = data.split("///")[1].split("|||")[0]; var data_to_send = { action: "checkversion", -- cgit v1.2.3 From cdf17dfd5d57b5461831868feb2497073a9b19d6 Mon Sep 17 00:00:00 2001 From: sijanec Date: Mon, 8 Jun 2020 23:33:02 +0200 Subject: forgot a semi-crucial thingy --- assets/js/app.js.bvr | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'assets/js') diff --git a/assets/js/app.js.bvr b/assets/js/app.js.bvr index c252c40..9bddb31 100644 --- a/assets/js/app.js.bvr +++ b/assets/js/app.js.bvr @@ -107,9 +107,11 @@ window.onunhandledrejection = error_report_function; document.addEventListener("DOMContentLoaded", () => { var update_interval = setInterval(() => { // ok, it's value is never read, so what?! localforage.getItem("lastUpdate").then((data) => { - if (Math.floor(Date.now() / 1000) > data + BEZIAPP_UPDATE_INTERVAL) { + if (Math.floor(Date.now() / 1000) > Number(data) + BEZIAPP_UPDATE_INTERVAL) { // trigger an update - update_app_function(); + localforage.setItem("lastUpdate", Math.floor(Date.now() / 1000)) .then(()=>{ + update_app_function(); + }); } }); }, 1000 * BEZIAPP_UPDATE_INTERVAL); -- cgit v1.2.3 From 5fd015d56db69193baead6d7ae6a23f763d3dc58 Mon Sep 17 00:00:00 2001 From: sijanec Date: Tue, 9 Jun 2020 12:03:06 +0200 Subject: so users don't have to stay on a single page for 300 seconds for an update --- assets/js/app.js.bvr | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'assets/js') diff --git a/assets/js/app.js.bvr b/assets/js/app.js.bvr index 9bddb31..3d4cc6b 100644 --- a/assets/js/app.js.bvr +++ b/assets/js/app.js.bvr @@ -55,7 +55,7 @@ function gsecErrorHandlerUI(err) { var update_app_function = async function () { try { - $.get("/cache_name.txt?cache_kill="+Date.now(), (data, status) => { + $.get("/cache_name.txt?cache_kill=" + Date.now(), (data, status) => { var cache_name = data.split("///")[1].split("|||")[0]; var data_to_send = { action: "checkversion", @@ -103,16 +103,22 @@ var error_report_function = async function (msg, url, lineNo, columnNo, error) { window.onerror = error_report_function; window.onunhandledrejection = error_report_function; +async function try_app_update() { + localforage.getItem("lastUpdate").then((data) => { + if (Math.floor(Date.now() / 1000) > Number(data) + BEZIAPP_UPDATE_INTERVAL) { + // trigger an update + localforage.setItem("lastUpdate", Math.floor(Date.now() / 1000)).then(() => { + update_app_function(); + }); + } + }); +} document.addEventListener("DOMContentLoaded", () => { - var update_interval = setInterval(() => { // ok, it's value is never read, so what?! - localforage.getItem("lastUpdate").then((data) => { - if (Math.floor(Date.now() / 1000) > Number(data) + BEZIAPP_UPDATE_INTERVAL) { - // trigger an update - localforage.setItem("lastUpdate", Math.floor(Date.now() / 1000)) .then(()=>{ - update_app_function(); - }); - } - }); + try_app_update(); + var update_interval = setInterval(() => { + + try_app_update(); + }, 1000 * BEZIAPP_UPDATE_INTERVAL); }); \ No newline at end of file -- cgit v1.2.3