[devdocsgjs/main: 722/1867] Fix service worker for disabled docs




commit 96cadd449bdc0dbcf0e9cd85288257df2b79e8ef
Author: Jasper van Merle <jaspervmerle gmail com>
Date:   Mon Aug 5 18:29:00 2019 +0200

    Fix service worker for disabled docs

 views/service-worker.js.erb | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
---
diff --git a/views/service-worker.js.erb b/views/service-worker.js.erb
index 8d5698a7..b4c77de6 100644
--- a/views/service-worker.js.erb
+++ b/views/service-worker.js.erb
@@ -35,15 +35,25 @@ self.addEventListener('fetch', event => {
     const cachedResponse = await caches.match(event.request);
     if (cachedResponse) return cachedResponse;
 
-    const url = new URL(event.request.url);
+    try {
+      const response = await fetch(event.request);
 
-    <%# Attempt to return the index page from the cache if the user is visiting a url like 
devdocs.io/offline or devdocs.io/javascript/global_objects/array/find %>
-    <%# The index page will handle the routing %>
-    if (url.origin === location.origin && !url.pathname.includes('.')) {
-      const cachedIndex = await caches.match('/');
-      if (cachedIndex) return cachedIndex;
-    }
+      if (!response.ok) {
+        throw new Error(`The HTTP request failed with status code ${response.status}`);
+      }
+
+      return response;
+    } catch (err) {
+      const url = new URL(event.request.url);
 
-    return fetch(event.request);
+      <%# Attempt to return the index page from the cache if the user is visiting a url like 
devdocs.io/offline or devdocs.io/javascript/global_objects/array/find %>
+      <%# The index page will make sure the correct documentation or a proper offline page is shown  %>
+      if (url.origin === location.origin && !url.pathname.includes('.')) {
+        const cachedIndex = await caches.match('/');
+        if (cachedIndex) return cachedIndex;
+      }
+
+      throw err;
+    }
   })());
 });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]