Skip to content

Commit e6c38f5

Browse files
committed
refactor: register service worker after window object is set
- move registration script to main.js
1 parent c62ea24 commit e6c38f5

File tree

5 files changed

+77
-34
lines changed

5 files changed

+77
-34
lines changed

frontend/index.html

-31
Original file line numberDiff line numberDiff line change
@@ -176,37 +176,6 @@
176176
href="/assets/hrms/manifest/apple-splash-1136-640.jpg"
177177
media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape)"
178178
/>
179-
180-
<!-- Import Frappe Notification JS -->
181-
<script type="module">
182-
import FrappePushNotification from "./public/frappe-push-notification"
183-
184-
window.frappePushNotification = new FrappePushNotification("hrms")
185-
186-
if ("serviceWorker" in navigator) {
187-
document.addEventListener("DOMContentLoaded", () => {
188-
window.frappePushNotification
189-
.appendConfigToServiceWorkerURL("/assets/hrms/frontend/sw.js")
190-
.then((url) => {
191-
navigator.serviceWorker
192-
.register(url, {
193-
type: "classic",
194-
})
195-
.then((registration) => {
196-
window.frappePushNotification
197-
.initialize(registration)
198-
.then(() => {
199-
console.log("Frappe Push Notification initialized")
200-
})
201-
})
202-
})
203-
.catch((err) => {
204-
console.error("Failed to register service worker", err)
205-
})
206-
})
207-
}
208-
</script>
209-
<!-- END Import Frappe Notification JS -->
210179
</head>
211180
<body class="antialiased">
212181
<div id="app"></div>

frontend/public/frappe-push-notification.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import {
88
} from "firebase/messaging"
99

1010
class FrappePushNotification {
11-
static relayServerBaseURL = "http://notification-relay:8003"
11+
static get relayServerBaseURL() {
12+
return window.push_relay_server_url
13+
}
14+
15+
static get isNotificationRelayEnabled() {
16+
return window.push_notifications_enabled
17+
}
1218

1319
// Type definitions
1420
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<template>
2+
<Dialog
3+
:options="{
4+
title: notificationTitle,
5+
message: notificationBody,
6+
size: 'lg',
7+
}"
8+
v-model="showDialog"
9+
@close="() => (showDialog = false)"
10+
/>
11+
</template>
12+
13+
<script setup>
14+
import { ref, onMounted } from "vue"
15+
import { Dialog } from "frappe-ui"
16+
17+
const showDialog = ref(false)
18+
const notificationTitle = ref("")
19+
const notificationBody = ref("")
20+
21+
onMounted(() => {
22+
window.frappePushNotification.onMessage((payload) => {
23+
notificationTitle.value = payload.data.title
24+
notificationBody.value = payload.data.body
25+
showDialog.value = true
26+
})
27+
})
28+
</script>

frontend/src/main.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { employeeResource } from "@/data/employee"
2222
import dayjs from "@/utils/dayjs"
2323
import getIonicConfig from "@/utils/ionicConfig"
2424

25+
import FrappePushNotification from "../public/frappe-push-notification"
26+
2527
/* Core CSS required for Ionic components to work properly */
2628
import "@ionic/vue/css/core.css"
2729

@@ -54,8 +56,46 @@ app.provide("$employee", employeeResource)
5456
app.provide("$socket", socket)
5557
app.provide("$dayjs", dayjs)
5658

59+
const registerServiceWorker = () => {
60+
window.frappePushNotification = new FrappePushNotification("hrms")
61+
62+
if ("serviceWorker" in navigator) {
63+
window.frappePushNotification
64+
.appendConfigToServiceWorkerURL("/assets/hrms/frontend/sw.js")
65+
.then((url) => {
66+
navigator.serviceWorker
67+
.register(url, {
68+
type: "classic",
69+
})
70+
.then((registration) => {
71+
window.frappePushNotification.initialize(registration).then(() => {
72+
console.log("Frappe Push Notification initialized")
73+
})
74+
})
75+
})
76+
.catch((err) => {
77+
console.error("Failed to register service worker", err)
78+
})
79+
} else {
80+
console.error("Service worker not enabled/supported by browser")
81+
}
82+
}
83+
5784
router.isReady().then(() => {
58-
app.mount("#app")
85+
if (import.meta.env.DEV) {
86+
frappeRequest({
87+
url: "/api/method/hrms.www.hrms.get_context_for_dev",
88+
}).then((values) => {
89+
for (let key in values) {
90+
window[key] = values[key]
91+
}
92+
registerServiceWorker()
93+
app.mount("#app")
94+
})
95+
} else {
96+
registerServiceWorker()
97+
app.mount("#app")
98+
}
5999
})
60100

61101
router.beforeEach(async (to, from, next) => {

frontend/src/views/AppSettings.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { ref } from "vue"
5555
5656
const router = useRouter()
5757
const pushNotificationState = ref(
58-
window.frappePushNotification.isNotificationEnabled()
58+
window.frappePushNotification?.isNotificationEnabled()
5959
)
6060
const isLoading = ref(false)
6161

0 commit comments

Comments
 (0)