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. --- dist/cache_name.txt | 5 + dist/js/app.js | 119 ++++++++++++--------- dist/js/setup-storage.js | 2 +- dist/pages/about.html | 265 +++++++++++++++++++++++++---------------------- dist/sw.js | 253 ++++++++++++++++++++++++-------------------- 5 files changed, 358 insertions(+), 286 deletions(-) create mode 100644 dist/cache_name.txt (limited to 'dist') diff --git a/dist/cache_name.txt b/dist/cache_name.txt new file mode 100644 index 0000000..30e3bd1 --- /dev/null +++ b/dist/cache_name.txt @@ -0,0 +1,5 @@ + + + + +///site-static-1.0.14.3-beta-134d890||| diff --git a/dist/js/app.js b/dist/js/app.js index f5c2f1b..87de1fc 100755 --- a/dist/js/app.js +++ b/dist/js/app.js @@ -2,22 +2,22 @@ -const app_version = "1.0.14.2-beta"; -const previous_commit = "c28c1c56dd807f620e916f9711d4c969817c6dd0"; - +const app_version = "1.0.14.3-beta"; +const previous_commit = "134d89078f864250abf340713edc1d23d90ede24"; +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"); + } + }); } /** @@ -27,12 +27,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}` }); + } } /** @@ -40,44 +40,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(); + } + }); +}) diff --git a/dist/js/setup-storage.js b/dist/js/setup-storage.js index b394f1e..be05650 100644 --- a/dist/js/setup-storage.js +++ b/dist/js/setup-storage.js @@ -1,2 +1,2 @@ -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 promises_update=[localforage.setItem("profile",{}),localforage.setItem("timetable",[]),localforage.setItem("teachers",[]),localforage.setItem("gradings",[]),localforage.setItem("grades",[]),localforage.setItem("absences",{}),localforage.setItem("messages",[[],[],[]]),localforage.setItem("directory",{}),localforage.setItem("meals",{}),localforage.setItem("chosenLang","en"),localforage.setItem("theme","light"),localforage.setItem("errorReporting","on"),localforage.setItem("triggerWarningAccepted",false)];if(logged_in&&force==false){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");}} \ No newline at end of file +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 promises_update=[localforage.setItem("profile",{}),localforage.setItem("timetable",[]),localforage.setItem("teachers",[]),localforage.setItem("gradings",[]),localforage.setItem("grades",[]),localforage.setItem("absences",{}),localforage.setItem("messages",[[],[],[]]),localforage.setItem("directory",{}),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){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",""),];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");}} \ No newline at end of file diff --git a/dist/pages/about.html b/dist/pages/about.html index 834a0a6..179db07 100755 --- a/dist/pages/about.html +++ b/dist/pages/about.html @@ -2,51 +2,52 @@ - - - - - - - - About « BežiApp - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + About « BežiApp + + + + + + + + + + + + + + + + + + + + + + + + + + + -
-
-
-

- BežiApp -

- -
version 1.0.14.2-beta
-
-
-
-
- -
-
-
-
-
    -
  • -
    translatorsForThisLanguage - miscTranslationLanguage
    -
  • -
  • - miscTranslationAuthors -
  • -
-
-
- +
+
+
+

+ BežiApp +

+ + # I agree, will make a script +
+ version + 1.0.14.3-beta +
+
+
+
+
+ +
+
+
+
+
    +
  • +
    + translatorsForThisLanguage - miscTranslationLanguage +
    +
  • +
  • + + miscTranslationAuthors + +
  • +
+
+
+

- ^HEAD c28c1c56dd807f620e916f9711d4c969817c6dd0 - + ^HEAD 134d89078f864250abf340713edc1d23d90ede24 +

-
- +
+ - + { - // Add localforage.clear() if storage purge is required - evt.waitUntil( - // localforage.clear() - setupStorage() - ); - - evt.waitUntil( - caches.open(static_cache_name).then((cache) => { - cache.addAll(assets); - }) - ); + // Add localforage.clear() if storage purge is required + evt.waitUntil( + // localforage.clear() + setupStorage() + ); + + evt.waitUntil( + caches.open(static_cache_name).then((cache) => { + cache.addAll(assets); + }) + ); }); // Delete old caches self.addEventListener("activate", evt => { - evt.waitUntil( - caches.keys().then((keys) => { - return Promise.all(keys - .filter(key => key !== static_cache_name) - .map(key => caches.delete(key)) - ); - }) - ); + evt.waitUntil( + caches.keys().then((keys) => { + return Promise.all(keys + .filter(key => key !== static_cache_name) + .map(key => caches.delete(key)) + ); + }) + ); }); self.addEventListener("message", event => { - if (event.data) { - let data = JSON.parse(event.data); // parse the message back to JSON - if (data.action == "addtocache") { // check the action - event.waitUntil( - caches.open(static_cache_name).then(function (cache) { - try { - return cache.add([data.url]); - } - catch (error) { - console.error("[sw.js] error: " + error); - } - }) - ); - } else if (data.action == "deletecaches") { - caches.keys().then(function (names) { - for (let name of names) - console.log("[sw.js] deleting cache named " + name); - caches.delete(name); - }); - } - } + if (event.data) { + let data = JSON.parse(event.data); // parse the message back to JSON + if (data.action == "addtocache") { // check the action + event.waitUntil( + caches.open(static_cache_name).then(function (cache) { + try { + return cache.add([data.url]); + } + catch (error) { + console.error("[sw.js] error: " + error); + } + }) + ); + } else if (data.action == "deletecaches") { + caches.keys().then(function (names) { + for (let name of names) { + console.log("[sw.js] deleting cache named " + name); + caches.delete(name); + } + }); + } else if (data.action.startsWith("checkversion")) { + try { + var names = await caches.keys(); + console.log("[sw.js] checkversion: ***** checkversion v0 for BežiApp ***** hello, world!"); + if(!(data.valid_cache_name == undefined || data.valid_cache_name == null || data.valid_cache_name == "")) { + var valid_cache_name = data.valid_cache_name; + console.log("[sw.js] checkversion: requested version (cachename) " + valid_cache_name); + } else { + var valid_cache_name = static_cache_name; + console.log("[sw.js] checkversion: no version to keep specified, using current "+valid_cache_name+", but that makes no sense to me."); + } + console.log("[sw.js] checkversion: deleting caches that don't match that cache name ..."); + for (let name of names) { + if(valid_cache_name != name) { + caches.delete(name); + console.log("[sw.js] checkversion: done requesting delete of cache " + name); + } + } + console.log("[sw.js] checkversion: done, exiting!"); + } catch (e) { + console.log("[sw.js] checkversion: !!! ERRORS! (caught)"); + } + } + } }); self.addEventListener("fetch", (evt) => { - evt.respondWith(caches.match(evt.request).then((cache_res) => { - return cache_res || fetch(evt.request); - })) + evt.respondWith(caches.match(evt.request).then((cache_res) => { + return cache_res || fetch(evt.request); + })) }); -- cgit v1.2.3