summaryrefslogtreecommitdiff
path: root/src/client/app/sw.js
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-03-29 20:32:18 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-03-29 20:32:18 +0900
commitcf33e483f7e6f40e8cbbbc0118a7df70bdaf651f (patch)
tree318279530d3392ee40d91968477fc0e78d5cf0f7 /src/client/app/sw.js
parentUpdate .travis.yml (diff)
downloadmisskey-cf33e483f7e6f40e8cbbbc0118a7df70bdaf651f.tar.gz
misskey-cf33e483f7e6f40e8cbbbc0118a7df70bdaf651f.tar.bz2
misskey-cf33e483f7e6f40e8cbbbc0118a7df70bdaf651f.zip
整理した
Diffstat (limited to 'src/client/app/sw.js')
-rw-r--r--src/client/app/sw.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/client/app/sw.js b/src/client/app/sw.js
new file mode 100644
index 0000000000..669703b16c
--- /dev/null
+++ b/src/client/app/sw.js
@@ -0,0 +1,71 @@
+/**
+ * Service Worker
+ */
+
+import composeNotification from './common/scripts/compose-notification';
+
+// キャッシュするリソース
+const cachee = [
+ '/'
+];
+
+// インストールされたとき
+self.addEventListener('install', ev => {
+ console.info('installed');
+
+ ev.waitUntil(Promise.all([
+ self.skipWaiting(), // Force activate
+ caches.open(_VERSION_).then(cache => cache.addAll(cachee)) // Cache
+ ]));
+});
+
+// アクティベートされたとき
+self.addEventListener('activate', ev => {
+ // Clean up old caches
+ ev.waitUntil(
+ caches.keys().then(keys => Promise.all(
+ keys
+ .filter(key => key != _VERSION_)
+ .map(key => caches.delete(key))
+ ))
+ );
+});
+
+// リクエストが発生したとき
+self.addEventListener('fetch', ev => {
+ ev.respondWith(
+ // キャッシュがあるか確認してあればそれを返す
+ caches.match(ev.request).then(response =>
+ response || fetch(ev.request)
+ )
+ );
+});
+
+// プッシュ通知を受け取ったとき
+self.addEventListener('push', ev => {
+ console.log('pushed');
+
+ // クライアント取得
+ ev.waitUntil(self.clients.matchAll({
+ includeUncontrolled: true
+ }).then(clients => {
+ // クライアントがあったらストリームに接続しているということなので通知しない
+ if (clients.length != 0) return;
+
+ const { type, body } = ev.data.json();
+
+ console.log(type, body);
+
+ const n = composeNotification(type, body);
+ return self.registration.showNotification(n.title, {
+ body: n.body,
+ icon: n.icon,
+ });
+ }));
+});
+
+self.addEventListener('message', ev => {
+ if (ev.data == 'clear') {
+ caches.keys().then(keys => keys.forEach(key => caches.delete(key)));
+ }
+});