forked from Opentek-Org/opentek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
74 lines (68 loc) · 2.32 KB
/
sw.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
// documented by Neeraj Gupta - https://neerajgupta.codes
self.addEventListener("install", (event) => {
console.log("Service worker install event!");
// event.waitUntil will wait until innermost event is resolved
// waitUntil will prevent the browser to terminate the service worker process before promise is resolved
// read about waitUntil here - https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent/waitUntil
event.waitUntil(
// Open cache(cacheName) from CacheStorage and if opened add all resourcesToPrecache in CacheStorage
caches
.open(cacheName)
.then((cache) => cache.addAll(resourcesToPrecache))
.catch((err) => console.log("Faled to precache", err))
);
});
self.addEventListener("activate", (event) => console.log("Activate event"));
// fetching files from either cache or network
self.addEventListener("fetch", (event) => {
// respondWith will prevent browser to directly go and do fetch request instead provide user with power to do task manually depending on promise
// read about respondWith here - https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/respondWith
event.respondWith(
caches
.match(event.request)
.then((cachedResponse) => cachedResponse || fetch(event.request)) //returning cache or if not available fetching from event
);
});
self.addEventListener("push", (event) => {
const title = "Yes, a message";
const body = "We have received a push message";
const tag = "simple-push-example-tag";
const options = {
body: body,
tag: tag,
};
event.waitUntil(self.registration.showNotification(title, options));
});
// Pre Caching resources
const cacheName = "cache-v1";
const resourcesToPrecache = [
"/",
"/index.html",
"/pages/about.html",
"/pages/Error.html",
"/pages/program.html",
"/pages/projects.html",
"/pages/tutorial.html",
"/css/style.css",
"/css/about.css",
"/css/bootstrap.min.css",
"/css/projects.css",
"/css/responsive.css.css",
"/css/slick.css",
"/images/Opentek.webp",
"/js/auth0.js",
"/js/bootstrap.min.js",
"/js/circular.js",
"/js/custom.js",
"/js/event.js",
"/js/jquery-3.3.1.min.js",
"/js/program.js",
"/js/projects.js",
"/js/slick.min.js",
"/js/tutorial.js",
"/images/team.svg",
"/images/error.svg",
"/images/about3.svg",
"/images/about2.svg",
"/images/about1.svg",
];