From f9b063ece0b321c4652533dd570065dc841b9a81 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Mon, 29 Jul 2024 10:18:54 +0300
Subject: Bundle service worker correctly
---
src/main/assets/scripts.js | 2 +-
src/main/assets/sw.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 src/main/assets/sw.js
(limited to 'src/main/assets')
diff --git a/src/main/assets/scripts.js b/src/main/assets/scripts.js
index 38406c2a..1583cedc 100644
--- a/src/main/assets/scripts.js
+++ b/src/main/assets/scripts.js
@@ -535,7 +535,7 @@ function fetchUserUri(dataUri) {
const registerServiceWorker = () => {
const publicKey = 'BPU0LniKKR0QiaUvILPd9AystmSOU8rWDZobxKm7IJN5HYxOSQdktRdc74TZvyRS9_kyUz7LDN6gUAmAVOmObAU'
- navigator.serviceWorker.register('/sw.js', { scope: '/' })
+ navigator.serviceWorker.register(new URL('./sw.js', import.meta.url), { scope: '/' })
navigator.serviceWorker.ready.then(reg => {
return reg.pushManager.subscribe({
userVisibleOnly: true,
diff --git a/src/main/assets/sw.js b/src/main/assets/sw.js
new file mode 100644
index 00000000..a64de7cc
--- /dev/null
+++ b/src/main/assets/sw.js
@@ -0,0 +1,47 @@
+///
+///
+///
+
+var sw = /** @type {ServiceWorkerGlobalScope & typeof self} */ (self)
+sw.addEventListener('install', function() {
+ sw.skipWaiting()
+})
+sw.addEventListener('activate', function() {
+ sw.clients.claim()
+})
+sw.addEventListener('push', function(evt) {
+ if (evt.data) {
+ const data = evt.data.json()
+ console.log('Push', data)
+ if (data.body) {
+ sw.registration.showNotification(data.title, {
+ body: data.body,
+ icon: data.custom.message.user.avatar,
+ tag: data.tag,
+ data: data.custom.message
+ })
+ }
+ }
+})
+sw.addEventListener('notificationclick', function(evt) {
+ evt.waitUntil(
+ sw.clients.matchAll().then(function(clientList) {
+ const message = evt.notification.data
+ const url = message.mid
+ ? message.rid
+ ? `https://juick.com/m/${message.mid}#${message.rid}`
+ : `https://juick.com/m/${message.mid}`
+ : 'https://juick.com/pm/inbox'
+ if (clientList.length > 0) {
+ for (var i = 0; i < clientList.length; i++) {
+ if (clientList[i].url === url) {
+ return clientList[i].focus()
+ }
+ }
+ clientList[0].navigate(url)
+ return clientList[0].focus()
+ }
+ return sw.clients.openWindow(url)
+ })
+ )
+})
--
cgit v1.2.3