-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
109 lines (95 loc) · 2.83 KB
/
Copy pathsw.js
File metadata and controls
109 lines (95 loc) · 2.83 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
const CACHE_VERSION = 'wraven-v1.2.0';
const PRECACHE = [
'/manifest.json',
'/imgs/littlelogo.png',
'/imgs/icon-192x192.png',
'/imgs/icon-512x512.png',
'/imgs/widelogo.png',
'/imgs/og-image.png'
];
const BYPASS_HOSTS = [
'dashboard.wraven.org',
'api.ipify.org',
'ipapi.co',
'httpbin.org',
'ipinfo.io'
];
const FONT_HOSTS = [
'fonts.googleapis.com',
'fonts.gstatic.com'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_VERSION).then((cache) => cache.addAll(PRECACHE))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_VERSION).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
if (request.method !== 'GET') return;
if (url.protocol === 'chrome-extension:') return;
if (BYPASS_HOSTS.some((h) => url.hostname.includes(h))) return;
if (FONT_HOSTS.some((h) => url.hostname.includes(h))) {
event.respondWith(cacheFirst(request));
return;
}
if (url.origin === self.location.origin) {
const isAsset = /\.(png|jpg|jpeg|webp|gif|svg|ico|woff2?|ttf|eot)$/i.test(url.pathname);
event.respondWith(isAsset ? staleWhileRevalidate(request) : networkFirst(request));
return;
}
event.respondWith(networkFirst(request));
});
async function networkFirst(request) {
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(CACHE_VERSION);
cache.put(request, response.clone());
}
return response;
} catch {
const cached = await caches.match(request);
if (cached) return cached;
if (request.destination === 'document') {
return new Response('WRAVEN — Offline', { status: 503, headers: { 'Content-Type': 'text/plain' } });
}
return Response.error();
}
}
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(CACHE_VERSION);
cache.put(request, response.clone());
}
return response;
}
async function staleWhileRevalidate(request) {
const cache = await caches.open(CACHE_VERSION);
const cached = await cache.match(request);
const fetched = fetch(request).then((response) => {
if (response.ok) cache.put(request, response.clone());
return response;
});
return cached || fetched;
}
self.addEventListener('message', (event) => {
if (event.data?.type === 'SKIP_WAITING') self.skipWaiting();
if (event.data?.type === 'CLEAR_CACHE') {
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k.startsWith('wraven-')).map((k) => caches.delete(k)))
);
}
});