forked from DSC-ChitkaraUniv/octahacks4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
90 lines (83 loc) · 2.92 KB
/
service-worker.js
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
const CACHE_NAME = 'offline';
// Customize this with a different URL if needed.
const OFFLINE_URL = 'offline.html';
self.addEventListener('install', event => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);
// Setting {cache: 'reload'} in the new request will ensure that the
// response isn't fulfilled from the HTTP cache; i.e., it will be from
// the network.
await cache.add(new Request(OFFLINE_URL, { cache: 'reload' }));
await cache.addAll([
'./',
'./assets/icons/favicon.ico',
'./static/css/style.css',
'./assets/media/octahcks_logo.png',
'./assets/media/GDSClogo.png',
'./assets/media/Home_Banner_1.png',
'./assets/Sponsors/1pass.png',
'./assets/Sponsors/jetbrains.png',
'./assets/Sponsors/airmeet.png',
'./assets/Sponsors/axure.png',
'./assets/Sponsors/balsamiq.png',
'./assets/Sponsors/bugsee.png',
'./assets/Sponsors/cb.png',
'./assets/Sponsors/devfolio.png',
'./assets/Sponsors/codechef.png',
'./assets/Sponsors/ninja(1).png',
'./assets/Sponsors/streamyard.png',
'./static/js/index.js',
]);
})()
);
// Force the waiting service worker to become the active service worker.
self.skipWaiting();
});
self.addEventListener('activate', event => {
event.waitUntil(
(async () => {
// Enable navigation preload if it's supported.
// See https://developers.google.com/web/updates/2017/02/navigation-preload
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable();
}
})()
);
// Tell the active service worker to take control of the page immediately.
self.clients.claim();
});
self.addEventListener('fetch', event => {
event.respondWith(
(async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
// Always try the network first.
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
// If fetch() returns a valid HTTP response with a response code in
// the 4xx or 5xx range, the catch() will NOT be called.
const cache = await caches.open(CACHE_NAME);
let cachedResponse = null;
if (event.request.mode === 'navigate') {
cachedResponse = await cache.match(OFFLINE_URL);
} else {
cachedResponse = await cache.match(event.request.url);
}
return cachedResponse;
}
})()
);
// If our if() condition is false, then this fetch handler won't intercept the
// request. If there are any other fetch handlers registered, they will get a
// chance to call event.respondWith(). If no fetch handlers call
// event.respondWith(), the request will be handled by the browser as if there
// were no service worker involvement.
});