-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
102 lines (85 loc) · 3.01 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
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
"use strict";
// cache define
const CACHE_NAME = "koopkit-v1";
const FILES_TO_CACHE = [
"favicon.png",
];
// service worker install
self.addEventListener("install", e => {
console.log("[Service Worker] Installing...");
const preCache = async () => {
console.log("[Service Worker] Opening Cache...");
const cache = await caches.open(CACHE_NAME);
console.log("[Service Worker] Caching all: app shell and content");
await cache.addAll(FILES_TO_CACHE);
}
e.waitUntil(preCache());
console.log("[Service Worker] Install Completed ✓");
// Forces the waiting service worker to become the active service worker.
self.skipWaiting();
});
// service worker on activation
self.addEventListener("activate", e => {
console.log(`[Service Worker] Activated ✓`);
let cacheWhitelist = [
CACHE_NAME,
];
const validateCache = async () => {
console.log("[Service Worker] Validating Cache...");
const keyList = await caches.keys();
return await Promise.all(keyList.map(key => {
if (cacheWhitelist.indexOf(key) === -1) {
return caches.delete(key);
}
}));
}
e.waitUntil(validateCache());
console.log("[Service Worker] Validate Cache Completed ✓");
/** When a service worker is initially registered,
* pages won't use it until they next load.
* The claim() method causes those pages to be controlled immediately.
*/
e.waitUntil(self.clients.claim());
});
// listen fetch
self.addEventListener("fetch", (e) => {
if (e.request.mode !== "navigate" || e.request.method !== "GET") {
// Not a page navigation, bail.
return;
}
const fetchResponder = async () => {
const r = await caches.match(e.request);
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
if (r) { return r; }
const response = await fetch(e.request);
const cache = await caches.open(CACHE_NAME);
console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
cache.put(e.request, response.clone());
return response;
}
e.respondWith(fetchResponder())
console.log("[Service Worker] Fetch Completed ✓");
});
// NOTIFICATIONS
// notification cancelled
self.addEventListener("notificationclose", e => {
let { notification } = e;
let primaryKey = notification.data.primaryKey;
let arrivalTime = notification.data.dateOfArrival;
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: "numeric", minute: "numeric" };
console.log("Bildirim ulaşma zamanı: ", new Date(arrivalTime).toLocaleDateString("tr-TR", options));
console.log("Bildirim kapandı: " + primaryKey);
});
// notification action butonları
self.addEventListener("notificationclick", function (e) {
let { notification, action } = e;
let primaryKey = notification.data.primaryKey;
if (action === "close") {
console.log("Kapata tıklandı: ", primaryKey);
notification.close();
} else {
// clients.openWindow("http://www.example.com");
console.log("Bildirim tıklandı: ", primaryKey);
notification.close();
}
});