Skip to content

Commit

Permalink
maybe an actually good service worker for once
Browse files Browse the repository at this point in the history
  • Loading branch information
William Blythe committed Nov 12, 2018
1 parent e129e1d commit 3987c4e
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/sw.js
Expand Up @@ -3,6 +3,14 @@ console.log('Hello from sw.js');
if (workbox) {
console.log('Yay! Workbox is loaded 🎉');
workbox.precaching.precacheAndRoute(self.__precacheManifest);

workbox.routing.registerNavigationRoute('/index.html');

workbox.routing.registerRoute(
new RegExp('/(.*?)'),
workbox.strategies.staleWhileRevalidate()
);

workbox.routing.registerRoute(
new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'),
workbox.strategies.cacheFirst({
Expand Down Expand Up @@ -41,23 +49,3 @@ self.addEventListener('message', event => {
break;
}
});
const OFFLINE_URL = '/';
self.addEventListener('fetch', function(event) {
console.log('Handling fetch event for', event.request.url);

event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
return response;
}

return fetch(event.request)
.then(function(response) {
return response;
})
.catch(function(error) {
return caches.match(OFFLINE_URL);
});
})
);
});

0 comments on commit 3987c4e

Please sign in to comment.