Skip to content
This repository was archived by the owner on May 2, 2021. It is now read-only.

Commit 1f4e3c0

Browse files
committed
offline support
1 parent 6b058ff commit 1f4e3c0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/_includes/footer.njk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@
8181
}
8282
})
8383
}
84+
85+
if ('serviceWorker' in navigator) {
86+
navigator.serviceWorker.register('/service-worker.js');
87+
}
8488
</script>

static/service-worker.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-disable no-undef */
2+
const cacheName = 'fevr-v2-cache';
3+
4+
// import workbox
5+
importScripts(
6+
'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js',
7+
);
8+
const { routing, strategies } = workbox;
9+
10+
const listRegExp = /^\/(index\.html|eventi\/index\.html)/;
11+
12+
routing.registerRoute(
13+
({ url }) => !listRegExp.test(url.pathname),
14+
new strategies.StaleWhileRevalidate({ cacheName }),
15+
);
16+
17+
routing.registerRoute(
18+
({ url }) => listRegExp.test(url.pathname),
19+
new strategies.NetworkFirst({ cacheName: cacheName + '-list' }),
20+
);
21+
22+
// removes all caches not named <cacheName>
23+
const invalidateOldCache = async () => {
24+
const keys = await caches.keys();
25+
const isOldCache = (key) => !key.startsWith(cacheName);
26+
const oldKeys = keys.filter(isOldCache);
27+
28+
return Promise.all(oldKeys.map((key) => caches.delete(key)));
29+
};
30+
31+
// runs invalidateOldCache on activation
32+
self.addEventListener('activate', (e) => e.waitUntil(invalidateOldCache()));

0 commit comments

Comments
 (0)