-
Notifications
You must be signed in to change notification settings - Fork 79
/
sw.js
44 lines (39 loc) · 1.5 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//@ts-nocheck
console.log("sw: service worker loaded");
const cachePrefix = "ePubViewer";
const revision = 24; // Update on every change
// Set the cache as the active cache.
self.addEventListener('activate', event =>
self.clients.claim());
// Immediately activate the new cache.
self.addEventListener('install', event => {
event.waitUntil(
caches.open(`${cachePrefix}-${revision}`).then(
cache => self.skipWaiting()))});
// Handle request.
self.addEventListener('fetch', event => {
if (
event.request.url.startsWith(self.location.origin) ||
event.request.url.match(/fonts.(googleapis|gstatic).com/) ||
event.request.url.match(/dict.api.pgaskin.net/)
/* Cache first: */
) event.respondWith(
caches.open(`${cachePrefix}-${revision}`).then(
cache => cache.match(event.request).then(resp => resp
? resp
: fetch(event.request).then(
resp => cache.put(event.request, resp.clone()).then(() => resp)))));});
/**/
/* Network first (currently broken):
) event.respondWith(
fetch(event.request).catch(
err => caches.open(`${cachePrefix}-${revision}`).then(
cache => cache.match(event.request))))});
/**/
// Delete old caches.
self.addEventListener('activate', event =>
event.waitUntil(
caches.keys().then(
ns => Promise.all(ns.filter(
n => n.startsWith(cachePrefix)).map(
n => caches.delete(n))))));