-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
132 lines (132 loc) · 4.4 KB
/
Copy pathsw.js
File metadata and controls
132 lines (132 loc) · 4.4 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const CORE_ASSETS = [
'/',
'/index.html',
'/styles.css',
'/main.js',
'/modules/api.js',
'/modules/highlights.js',
'/modules/hotkeys.js',
'/modules/mobile.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.png',
'/favicons/favicon-16x16.png',
'/favicons/favicon-32x32.png',
'/favicons/favicon-192x192.png',
'/favicons/favicon-512x512.png',
'https://cdn.jsdelivr.net/npm/marked/marked.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css'
];
let CURRENT_CACHE = 'provinent-cache-v-initial';
self.addEventListener('message', e => {
if (e.data?.type === 'VERSION') {
const newCacheName = `provinent-cache-v${e.data.version}`;
if (newCacheName !== CURRENT_CACHE) {
CURRENT_CACHE = newCacheName;
preCacheCoreAssets();
}
}
});
async function preCacheCoreAssets() {
if (!CURRENT_CACHE) return;
const cache = await caches.open(CURRENT_CACHE);
const results = await Promise.allSettled(
CORE_ASSETS.map(async url => {
try {
const isExternal = url.startsWith('http');
const request = new Request(url, isExternal ? { mode: 'cors' } : { credentials: 'same-origin' });
const response = await fetch(request);
if (!response.ok && !isExternal) throw new Error(`HTTP ${response.status}`);
await cache.put(url, response);
} catch (err) {
throw new Error(`Failed to cache ${url}: ${err.message}`);
}
})
);
results.forEach((result, index) => {
if (result.status === 'rejected') {
console.warn(`[SW] ${result.reason}`);
}
});
console.log('[SW] Core assets caching completed for', CURRENT_CACHE);
}
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', evt => {
evt.waitUntil(
(async () => {
const names = await caches.keys();
await Promise.all(
names.map(name =>
name !== CURRENT_CACHE && 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(async () => {
const cached = await caches.match(evt.request);
return cached || await caches.match('/index.html') || await 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(async () => {
const cachedResponse = await caches.match(evt.request);
if (cachedResponse) {
return cachedResponse;
}
return new Response(JSON.stringify({ error: 'Offline and not cached' }), {
status: 503,
statusText: 'Service Unavailable',
headers: { 'Content-Type': 'application/json' }
});
})
);
});
function shouldCache(request, response) {
return (
CURRENT_CACHE &&
isHttpScheme(request.url) &&
response.type !== 'opaque' &&
response.ok
);
}
function isHttpScheme(url) {
try {
const u = new URL(url);
return u.protocol === 'http:' || u.protocol === 'https:';
} catch (_) {
return false;
}
}