Skip to content

Commit 96cadd4

Browse files
committed
Fix service worker for disabled docs
1 parent 3ccb669 commit 96cadd4

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

views/service-worker.js.erb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,25 @@ self.addEventListener('fetch', event => {
3535
const cachedResponse = await caches.match(event.request);
3636
if (cachedResponse) return cachedResponse;
3737

38-
const url = new URL(event.request.url);
38+
try {
39+
const response = await fetch(event.request);
3940

40-
<%# 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 %>
41-
<%# The index page will handle the routing %>
42-
if (url.origin === location.origin && !url.pathname.includes('.')) {
43-
const cachedIndex = await caches.match('/');
44-
if (cachedIndex) return cachedIndex;
45-
}
41+
if (!response.ok) {
42+
throw new Error(`The HTTP request failed with status code ${response.status}`);
43+
}
44+
45+
return response;
46+
} catch (err) {
47+
const url = new URL(event.request.url);
4648

47-
return fetch(event.request);
49+
<%# 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 %>
50+
<%# The index page will make sure the correct documentation or a proper offline page is shown %>
51+
if (url.origin === location.origin && !url.pathname.includes('.')) {
52+
const cachedIndex = await caches.match('/');
53+
if (cachedIndex) return cachedIndex;
54+
}
55+
56+
throw err;
57+
}
4858
})());
4959
});

0 commit comments

Comments
 (0)