about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-28 18:53:00 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-28 18:53:00 +0300
commit9a36065a88271c79faf2017b391e9aae40b4a4b4 (patch)
tree13248106a352df939c71b2540e19aba58cdcaf7a /src
parentd4f72e3aeaae49501e46ac4fbe825c6d747b4150 (diff)
downloadthe-epube-9a36065a88271c79faf2017b391e9aae40b4a4b4.tar.gz
the-epube-9a36065a88271c79faf2017b391e9aae40b4a4b4.tar.xz
various worker-related fixes
Diffstat (limited to 'src')
-rw-r--r--src/worker.js50
1 files changed, 39 insertions, 11 deletions
diff --git a/src/worker.js b/src/worker.js
index cf863ef..fab10a0 100644
--- a/src/worker.js
+++ b/src/worker.js
@@ -25,7 +25,7 @@ const MANIFEST_ASSETS = BUILD_MANIFEST_ASSETS;
 self.addEventListener('install', (event) => {
 	event.waitUntil(
 		(async () => {
-			console.log('[worker] install started');
+			console.log('[worker] install started', CACHE_NAME);
 
 			// await loadConfig();
 
@@ -87,11 +87,13 @@ self.addEventListener('activate', (event) => {
 self.addEventListener('fetch', (event) => {
 	const url = new URL(event.request.url);
 
-	// Skip /assets/ — handled separately (e.g., book content)
+	// console.log('[fetch]', url);
+
+	// /assets/ belong to android app
 	if (url.pathname.startsWith('/assets/')) return;
 
-	// API endpoints: network-first with cache fallback
-	if (url.pathname.includes('backend.php')) {
+	// request specific API endpoints from the network if possible and cache them locally
+	if (url.pathname.match('backend.php\\?op=(cover|getinfo)')) {
 		event.respondWith(
 			(async () => {
 				try {
@@ -107,28 +109,52 @@ self.addEventListener('fetch', (event) => {
 					// Network failed, try to get the cached API response
 					const cachedResp = await caches.match(event.request);
 					if (cachedResp) return cachedResp;
+				}
+			})()
+		);
+		return;
+	}
 
-					// Both failed, return the offline fallback page (Removed redundant await here)
-					return caches.match('/offline.html');
+	// index & read UI: online first; if offline, map index pages to offline.html & map all read.html?* to offline read.html
+	if (url.pathname == "/" || url.pathname.match("index.php|offline.html|read.html")) {
+		event.respondWith(
+			(async () => {
+				try {
+					const networkResp = await fetch(event.request);
+
+					if (networkResp.ok) {
+						const cache = await caches.open(CACHE_NAME);
+						await cache.put(networkResp.url, networkResp.clone());
+					}
+
+					return networkResp;
+				} catch (err) {
+					// i.e. read.html?id=1234&b=1234
+					if (url.pathname.includes("read.html")) {
+						return caches.match("read.html");
+					} else {
+						return caches.match('/offline.html');
+					}
 				}
 			})()
 		);
+
 		return;
 	}
 
-	// Static assets: cache-first with background update (Stale-While-Revalidate)
+	// everything else: cache-first with network fallback
 	event.respondWith(
 		(async () => {
 			const cache = await caches.open(CACHE_NAME);
 			const cached = await cache.match(event.request);
 
 			if (cached) {
-				// Fire background network check without waiting for it (no await)
+				// if cached, try to update: fire background network check without waiting for it (no await)
 				fetch(event.request)
 					.then((networkResp) => {
 						if (networkResp.ok) cache.put(event.request, networkResp.clone());
 					})
-					.catch((e) => console.warn('[worker] Background sync failed:', e));
+					.catch((e) => console.warn('[worker] Background update failed:', e));
 
 				// Return cached asset immediately
 				return cached;
@@ -136,9 +162,11 @@ self.addEventListener('fetch', (event) => {
 
 			// Cache miss: fetch from network and cache it
 			const networkResp = await fetch(event.request);
-			if (networkResp.ok) {
+
+			/* if (networkResp.ok) { -- we don't cache all responses
 				await cache.put(event.request, networkResp.clone());
-			}
+			} */
+
 			return networkResp;
 		})()
 	);