-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsw.js
More file actions
149 lines (128 loc) · 4.28 KB
/
sw.js
File metadata and controls
149 lines (128 loc) · 4.28 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const APP_VERSION = '1.9.20';
const CACHE_NAME = `knowlet-${APP_VERSION}`;
const IGNORE_PARAMS_FOR = '/navigator';
const STATIC_ASSETS = [
'/',
'/offline',
'/about',
'/help',
'/legal/declaration',
'/legal/privacy-policy',
'/legal/terms-and-conditions',
'/index',
'/favourite',
'/assistant',
'/history',
'/profile',
'/login_signup',
'/profile_complition_form',
'/search',
'/navigator',
'/css/style.css',
'/css/favourite.css',
'/css/assistant.css',
'/css/history.css',
'/css/profile.css',
'/css/login_signup.css',
'/css/profile_complition_form.css',
'/css/search.css',
'/css/navigator.css',
'/js/script.js',
'/js/favourite.js',
'/js/assistant.js',
'/js/history.js',
'/js/profile.js',
'/js/login_signup.js',
'/js/profile_complition_form.js',
'/js/search.js',
'/js/navigator.js',
'/css/core.css',
'/js/core.js',
'/assets/notes.json',
'/assets/pyq.json'
];
self.addEventListener('install', event => {
self.skipWaiting(); // activate immediately
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(STATIC_ASSETS))
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys => {
return Promise.all(
keys
.filter(key => key !== CACHE_NAME)
.map(key => caches.delete(key))
);
}).then(() => {
// Force all open pages to reload under the new service worker
return self.clients.matchAll({ type: 'window' }).then(clients =>
clients.forEach(client => client.navigate(client.url))
);
})
);
self.clients.claim(); // take control immediately
});
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
let fetchRequest = event.request;
if (url.origin === self.location.origin && url.pathname === IGNORE_PARAMS_FOR) {
fetchRequest = new Request('/navigator');
}
event.respondWith(
caches.match(fetchRequest).then(cachedResponse => {
// Start network fetch in the background
const networkResponse = fetch(fetchRequest)
.then(res => {
// Update cache with latest response
if ( res && res.status === 200 && fetchRequest.url.startsWith(self.location.origin)) {
caches.open(CACHE_NAME).then(cache => {
cache.put(fetchRequest, res.clone());
});
}
return res;
})
.catch(() => {
// If network fails and it's a page navigation, show offline page
if (fetchRequest.mode === 'navigate') {
return caches.match('/offline');
}
});
// If cached version exists, return it immediately; network fetch updates cache in background
return cachedResponse || networkResponse;
})
);
});
self.addEventListener('push', event => {
const data = event.data?.json() || {};
const title = data.title || 'Your Notes Are Ready';
event.waitUntil(
self.registration.showNotification(title, {
body: data.body || 'Jump back in and keep learning with Knowlet.',
icon: data.icon || '/assets/icons/knowlet/android-chrome-192x192.png',
badge: data.badge || '/assets/icons/knowlet/favicon-32x32.png',
image: data.image,
tag: data.tag,
data: {
url: data.url || '/'
}
})
);
});
self.addEventListener('notificationclick', event => {
event.notification.close();
const url = new URL(event.notification.data?.url || '/', self.location.origin).href;
event.waitUntil(
clients.matchAll({ type: "window", includeUncontrolled: true }).then(clientList => {
for (const client of clientList) {
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
return clients.openWindow(url);
})
);
});