Browse Source

add a cache refresh link to index

Andrew Dolgov 2 years ago
parent
commit
6ee7389a0a
2 changed files with 16 additions and 5 deletions
  1. 12 3
      index.php
  2. 4 2
      js/index.js

+ 12 - 3
index.php

@@ -131,9 +131,13 @@
            .then(function() {
 					console.log("service worker registered");
 			  });
-			 /*$(window).on('offline', function() {
-				 window.location.reload();
-			});*/
+
+			 navigator.serviceWorker.addEventListener('message', function(event) {
+			   // not used yet
+				if (event.data == 'client-reload') {
+					window.location.reload();
+				}
+			 });
 		}
 
 		mark_offline_books();
@@ -375,6 +379,11 @@
 		<?php } ?>
 	</ul>
 
+	<p class="text-center small">
+		<a class="text-muted" href="#" onclick="return cache_refresh(true)">Refresh cache</a>
+	</p>
+
+
 </div>
 </body>
 </html>

+ 4 - 2
js/index.js

@@ -1,9 +1,9 @@
-function cache_refresh() {
+function cache_refresh(force) {
 	if ('serviceWorker' in navigator) {
 		localforage.getItem("epube.cache-timestamp").then(function(stamp) {
 			var ts = parseInt(new Date().getTime()/1000);
 
-			if (!stamp || ts - stamp > 3600) {
+			if (force || !stamp || ts - stamp > 3600) {
 				console.log('asking worker to refresh cache');
 				navigator.serviceWorker.controller.postMessage("refresh-cache");
 				localforage.setItem("epube.cache-timestamp", ts);
@@ -11,6 +11,8 @@ function cache_refresh() {
 
 		});
 	}
+
+	return false;
 }
 
 function toggle_fav(elem) {