forked from Ajaxy/telegram-tt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceWorker.ts
60 lines (49 loc) · 1.65 KB
/
serviceWorker.ts
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
import { DEBUG } from './config';
import { respondForProgressive } from './serviceWorker/progressive';
import { respondWithCache, clearAssetCache } from './serviceWorker/assetCache';
import { handlePush, handleNotificationClick, handleClientMessage } from './serviceWorker/pushNotification';
import { pause } from './util/schedulers';
declare const self: ServiceWorkerGlobalScope;
const ASSET_CACHE_PATTERN = /[0-9a-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|json|wasm)$/;
const ACTIVATE_TIMEOUT = 3000;
self.addEventListener('install', (e) => {
if (DEBUG) {
// eslint-disable-next-line no-console
console.log('ServiceWorker installed');
}
// Activate worker immediately
e.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', (e) => {
if (DEBUG) {
// eslint-disable-next-line no-console
console.log('ServiceWorker activated');
}
e.waitUntil(
Promise.race([
// An attempt to fix freezing UI on iOS
pause(ACTIVATE_TIMEOUT),
Promise.all([
clearAssetCache(),
// Become available to all pages
self.clients.claim(),
]),
]),
);
});
// eslint-disable-next-line no-restricted-globals
self.addEventListener('fetch', (e: FetchEvent) => {
const { url } = e.request;
if (url.includes('/progressive/')) {
e.respondWith(respondForProgressive(e));
return true;
}
if (url.startsWith('http') && url.match(ASSET_CACHE_PATTERN)) {
e.respondWith(respondWithCache(e));
return true;
}
return false;
});
self.addEventListener('push', handlePush);
self.addEventListener('notificationclick', handleNotificationClick);
self.addEventListener('message', handleClientMessage);