-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.ts
68 lines (60 loc) · 1.77 KB
/
sw.ts
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
// Tsukumart Service Worker
((self: ServiceWorkerGlobalScope) => {
self.addEventListener("install", (e) => {
console.log(
"Service Worker内でServiceWorkerがブラウザにインストールされたことを検知した!"
);
e.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", (e) => {
console.log("Service Workerがアクティブな状態になった");
e.waitUntil(self.clients.claim());
});
self.addEventListener("fetch", (e) => {
if (navigator.onLine) {
return;
}
const accept = e.request.headers.get("accept");
if (accept === null) {
return;
}
if (accept.includes("text/html")) {
e.respondWith(
new Response(
`
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>つくマート | オフライン!</title>
<style>
body {
font-size: 3rem;
text-align: center;
}
</style>
</head>
<body>
つくマートはオフラインでは利用できません。インターネットへの接続をしてください。
</body>
</html>`,
{ headers: { "content-type": "text/html" } }
)
);
}
});
self.addEventListener("sync", (e) => {
console.log("syncを受け取った", e);
});
self.addEventListener("push", (e) => {
console.log("プッシュ通知をサーバーから受け取った", e);
e.waitUntil(
self.registration.showNotification("プッシュ通知です!", {
body: "プッシュ通知はこのようにして送られるのです",
icon: "/assets/logo_bird.png",
tag: "push-notification-tag",
})
);
});
})(self as unknown as ServiceWorkerGlobalScope);