Skip to content

Commit

Permalink
Bloody service workers!
Browse files Browse the repository at this point in the history
  • Loading branch information
edent committed May 23, 2023
1 parent f365c7b commit bb83db9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 44 deletions.
33 changes: 0 additions & 33 deletions www/public/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,4 @@ self.addEventListener('install', async (event) => {
caches.open(CACHE)
.then((cache) => cache.add(offlineFallbackPage))
);
});

if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}

workbox.routing.registerRoute(
new RegExp('/*'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: CACHE
})
);

self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
const preloadResp = await event.preloadResponse;

if (preloadResp) {
return preloadResp;
}

const networkResp = await fetch(event.request);
return networkResp;
} catch (error) {

const cache = await caches.open(CACHE);
const cachedResp = await cache.match(offlineFallbackPage);
return cachedResp;
}
})());
}
});
20 changes: 9 additions & 11 deletions www/templates/footer.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ function geoFindMe() {
</script>
<script>
if ("serviceWorker" in navigator) {
if (navigator.serviceWorker.controller) {
console.log("[PWA Builder] active service worker found, no need to register");
} else {
// Register the service worker
navigator.serviceWorker.register("/sw.js", {
scope: "./"
})
.then(function (reg) {
console.log("[PWA Builder] Service worker has been registered for scope: " + reg.scope);
});
}
// Register the service worker
navigator.serviceWorker.register("/sw.js?cache=2023-05-23T22:00", {
scope: "./"
})
.then(function (reg) {
console.log("[PWA Builder] 2023-05-23 Service worker has been registered for scope: " + reg.scope);
});
}
</script>

0 comments on commit bb83db9

Please sign in to comment.