-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsw.js
More file actions
114 lines (95 loc) · 3.29 KB
/
sw.js
File metadata and controls
114 lines (95 loc) · 3.29 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
const APP_VERSION = '1.7.85';
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',
'/history',
'/profile',
'/login_signup',
'/profile_complition_form',
'/search',
'/navigator',
'/assets/styles/style.css',
'/assets/styles/favourite.css',
'/assets/styles/history.css',
'/assets/styles/profile.css',
'/assets/styles/login_signup.css',
'/assets/styles/profile_complition_form.css',
'/assets/styles/search.css',
'/assets/styles/navigator.css',
'/assets/scripts/script.js',
'/assets/scripts/favourite.js',
'/assets/scripts/history.js',
'/assets/scripts/profile.js',
'/assets/scripts/login_signup.js',
'/assets/scripts/profile_complition_form.js',
'/assets/scripts/search.js',
'/assets/scripts/navigator.js',
'/assets/styles/units.css',
'/assets/scripts/units.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;
})
);
});