summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2020-05-07 09:50:27 +0300
committerAndrew Dolgov <[email protected]>2020-05-07 09:50:27 +0300
commit6c40c6da461294aad6d789154b6342734e0b502e (patch)
tree45305711facb212a2f0d55f5cc552f93f38ae39c
parenta2cf7386c0096deec0b62dab13117329711b4d99 (diff)
check file modification times for local syncing instead of relying on a sync interval
-rw-r--r--index.php48
-rw-r--r--js/app.js14
2 files changed, 56 insertions, 6 deletions
diff --git a/index.php b/index.php
index 76d895a..cd36791 100644
--- a/index.php
+++ b/index.php
@@ -42,6 +42,53 @@
die(dirname(SCRATCH_DB) . " directory is not writable");
}
+ // TODO: this should be unified with the service worker cache list
+ $check_files_mtime = [
+ 'manifest.json',
+ 'worker.js',
+ 'img/ic_launcher_web.png?v4',
+ 'img/favicon.png',
+ 'read.html',
+ 'js/app.js',
+ 'js/reader.js',
+ 'js/reader_iframe.js',
+ 'js/dict.js',
+ 'css/read.css',
+ 'css/reader.css',
+ 'css/index.css',
+ 'css/transitions.css',
+ 'offline.html',
+ 'themes/default.css',
+ 'themes/light.css',
+ 'themes/mocca.css',
+ 'themes/night.css',
+ 'themes/plan9.css',
+ 'themes/gray.css',
+ 'themes/sepia.css',
+ 'lib/promise.js',
+ 'lib/fetch.js',
+ 'lib/zip.min.js',
+ 'lib/epub.js',
+ 'lib/localforage.min.js',
+ 'lib/jquery.mobile-events.min.js',
+ 'lib/holder.min.js',
+ 'lib/bootstrap/v3/css/bootstrap-theme.min.css',
+ 'lib/bootstrap/v3/css/bootstrap.min.css',
+ 'lib/bootstrap/v3/css/theme-dark.min.css',
+ 'lib/bootstrap/v3/js/jquery.js',
+ 'lib/bootstrap/v3/js/bootstrap.min.js',
+ 'lib/bootstrap/v3/fonts/glyphicons-halflings-regular.woff2',
+ 'lib/fonts/pmn-caecilia-55.ttf',
+ 'lib/fonts/pmn-caecilia-56.ttf',
+ 'lib/fonts/pmn-caecilia-75.ttf'
+ ];
+
+ $last_mtime = array_reduce(
+ array_map("filemtime", $check_files_mtime),
+ function ($carry, $item) {
+ return $item > $carry ? $item : $carry;
+ }, 0);
+
@$mode = htmlspecialchars($_REQUEST["mode"]);
$ldb = Db::get();
@@ -76,6 +123,7 @@
$(document).ready(function() {
App.index_mode = "<?php echo $mode ?>";
+ App.last_mtime = parseInt("<?php echo $last_mtime ?>");
App.init();
});
});
diff --git a/js/app.js b/js/app.js
index 74c2fca..33333e8 100644
--- a/js/app.js
+++ b/js/app.js
@@ -37,6 +37,7 @@ const Cookie = {
const App = {
_dl_progress_timeout: false,
index_mode: "",
+ last_mtime: -1,
init: function() {
let refreshed_files = 0;
@@ -74,8 +75,7 @@ const App = {
}
if (event.data == 'client-reload') {
- const ts = parseInt(new Date().getTime()/1000);
- localforage.setItem("epube.cache-timestamp", ts);
+ localforage.setItem("epube.cache-timestamp", App.last_mtime);
window.location.reload()
}
@@ -156,9 +156,7 @@ const App = {
refreshCache: function(force) {
if ('serviceWorker' in navigator) {
localforage.getItem("epube.cache-timestamp").then(function(stamp) {
- const ts = parseInt(new Date().getTime()/1000);
-
- if (force || !stamp || ts - stamp > 3600 * 24 * 7) {
+ if (force || stamp != App.last_mtime) {
console.log('asking worker to refresh cache');
if (navigator.serviceWorker.controller) {
@@ -190,8 +188,12 @@ const App = {
});
}
}
-
});
+ } else {
+ $(".dl-progress")
+ .show()
+ .addClass("alert-danger")
+ .html("Could not communicate with service worker. Try reloading the page.");
}
},
initOfflineEvents: function() {