-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
89 lines (89 loc) · 2.99 KB
/
sw.js
File metadata and controls
89 lines (89 loc) · 2.99 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const CORE_ASSETS = [
'/', '/index.html', '/styles.css', '/main.js',
'/modules/api.js', '/modules/highlights.js', '/modules/hotkeys.js',
'/modules/navigation.js', '/modules/passage.js', '/modules/settings.js',
'/modules/state.js', '/modules/strongs.js', '/modules/ui.js',
'/sw.js', '/404.html', '/manifest.json',
'/favicons/apple-touch-icon.png', '/favicons/favicon.ico',
'/favicons/favicon-16x16.png', '/favicons/favicon-32x32.png'
];
let CURRENT_CACHE = null;
self.addEventListener('message', e => {
if (e.data?.type === 'VERSION') {
CURRENT_CACHE = `provinent-cache-v${e.data.version}`;
preCacheCoreAssets();
}
});
async function preCacheCoreAssets() {
if (!CURRENT_CACHE) return;
const cache = await caches.open(CURRENT_CACHE);
try {
await cache.addAll(
CORE_ASSETS.map(u => new Request(u, { credentials: 'same-origin' }))
);
console.log('Core assets cached under', CURRENT_CACHE);
} catch (err) {
console.error('Failed to pre‑cache core assets', err);
}
}
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', evt => {
evt.waitUntil(
(async () => {
const expected = CURRENT_CACHE ||
`provinent-cache-v${self.registration.scope}`;
const names = await caches.keys();
await Promise.all(
names.map(name =>
name !== expected && name.startsWith('provinent-cache-v')
? caches.delete(name)
: null
)
);
await self.clients.claim();
})()
);
});
self.addEventListener('fetch', evt => {
if (evt.request.method !== 'GET') return;
if (evt.request.mode === 'navigate') {
evt.respondWith(
fetch(evt.request)
.then(resp => {
if (shouldCache(evt.request, resp)) {
const copy = resp.clone();
caches.open(CURRENT_CACHE).then(c => c.put(evt.request, copy));
}
return resp;
})
.catch(() => caches.match(evt.request).then(c => c || caches.match('/404.html')))
);
return;
}
evt.respondWith(
fetch(evt.request)
.then(resp => {
if (shouldCache(evt.request, resp)) {
const copy = resp.clone();
caches.open(CURRENT_CACHE).then(c => c.put(evt.request, copy));
}
return resp;
})
.catch(() => caches.match(evt.request))
);
});
function shouldCache(request, response) {
return (
CURRENT_CACHE &&
isHttpScheme(request.url) &&
response.type !== 'opaque'
);
}
function isHttpScheme(url) {
try {
const u = new URL(url);
return u.protocol === 'http:' || u.protocol === 'https:';
} catch (_) {
return false;
}
}