about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--build.js7
-rw-r--r--src/worker.ts31
2 files changed, 32 insertions, 6 deletions
diff --git a/build.js b/build.js
index 54488cf..2f3f771 100644
--- a/build.js
+++ b/build.js
@@ -122,8 +122,11 @@ async function buildApp() {
 	);
 
 	// Add fonts and images
-	MANIFEST_ASSETS.push(...globSync('lib/fonts/*'));
-	MANIFEST_ASSETS.push(...globSync('img/*.png'));
+    MANIFEST_ASSETS.push(...globSync('lib/fonts/*'));
+    MANIFEST_ASSETS.push(...globSync('img/*.png'));
+
+    // Add bootstrap fonts
+    MANIFEST_ASSETS.push(...globSync('lib/bootstrap/v3/fonts/*'));
 
 	// deduplicate
 	MANIFEST_ASSETS = [...new Set(MANIFEST_ASSETS)];
diff --git a/src/worker.ts b/src/worker.ts
index e7133e4..a1e69a0 100644
--- a/src/worker.ts
+++ b/src/worker.ts
@@ -8,6 +8,27 @@ const CACHE_VERSION = BUILD_CACHE_VERSION;
 const CACHE_NAME = CACHE_PREFIX + "-" + CACHE_VERSION;
 const MANIFEST_ASSETS = BUILD_MANIFEST_ASSETS;
 
+function fetchWithTimeout(request: Request, timeoutMs = 2500): Promise<Response> {
+    return new Promise((resolve, reject) => {
+        // Create an abort controller to stop the native network request if the timer fires first
+        const controller = new AbortController();
+        const timeoutId = setTimeout(() => {
+            controller.abort();
+            reject(new Error("Network timeout: DNS resolution took too long."));
+        }, timeoutMs);
+
+        fetch(request, { signal: controller.signal })
+            .then((response) => {
+                clearTimeout(timeoutId);
+                resolve(response);
+            })
+            .catch((err) => {
+                clearTimeout(timeoutId);
+                reject(err);
+            });
+    });
+}
+
 // Install: precache all assets from manifest
 sw.addEventListener('install', (event) => {
     (event as any).waitUntil((async () => {
@@ -86,7 +107,7 @@ sw.addEventListener('fetch', (event) => {
     if (url.pathname.match('backend.php')) {
         (event as any).respondWith((async () => {
             try {
-                const networkResp = await fetch((event as any).request);
+                const networkResp = await fetchWithTimeout((event as any).request);
                 if (networkResp.ok) {
                     const cache = await caches.open(CACHE_NAME);
                     await cache.put(networkResp.url, networkResp.clone());
@@ -103,10 +124,12 @@ sw.addEventListener('fetch', (event) => {
         return;
     }
     // 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.html|offline.html|read.html")) {
+    // note that instead of hardcoding specific URLs, we handle all user-initiated explicit page navigation requests,
+    // everything else falls back to the next block with cache-first-online-fallback logic
+    if (event.request.mode === 'navigate') {
         (event as any).respondWith((async () => {
             try {
-                const networkResp = await fetch((event as any).request);
+                const networkResp = await fetchWithTimeout((event as any).request);
                 if (networkResp.ok) {
                     const cache = await caches.open(CACHE_NAME);
                     await cache.put(networkResp.url, networkResp.clone());
@@ -116,7 +139,7 @@ sw.addEventListener('fetch', (event) => {
             catch (err) {
                 // i.e. read.html?id=1234&b=1234
                 if (url.pathname.includes("read.html")) {
-                    return caches.match("read.html");
+                    return caches.match("/read.html", { ignoreSearch: true });
                 }
                 else {
                     return caches.match('/offline.html');