Skip to content

Commit df1214e

Browse files
committed
feat: register custom service worker with firebase push notification initialization
- use injectManifest to customize service worker - use injectRegister to control when it gets registered (after fetching firebase config)
1 parent a4d7c77 commit df1214e

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

frontend/index.html

+4-5
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@
184184
window.frappePushNotification = new FrappePushNotification("hrms")
185185
document.addEventListener("DOMContentLoaded", () => {
186186
window.frappePushNotification
187-
.appendConfigToServiceWorkerURL(
188-
"/assets/hrms/frontend/frappe-push-notification-sw.js"
189-
)
187+
.appendConfigToServiceWorkerURL("/assets/hrms/frontend/sw.js")
190188
.then((url) => {
191189
navigator.serviceWorker
192190
.register(url, {
@@ -196,7 +194,7 @@
196194
window.frappePushNotification
197195
.initialize(registration)
198196
.then(() => {
199-
console.log("Frappe Notification Initialized")
197+
console.log("Frappe Push Notification initialized")
200198
})
201199
})
202200
})
@@ -205,7 +203,7 @@
205203
})
206204
})
207205
</script>
208-
<!-- END ! Import Frappe Notification JS -->
206+
<!-- END Import Frappe Notification JS -->
209207
</head>
210208
<body class="antialiased">
211209
<div id="app"></div>
@@ -215,6 +213,7 @@
215213
<script>
216214
window.csrf_token = "{{ csrf_token }}"
217215
</script>
216+
218217
<script type="module" src="/src/main.js"></script>
219218
</body>
220219
</html>

frontend/public/frappe-push-notification-sw.js frontend/public/sw.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching"
2+
import { clientsClaim } from "workbox-core"
3+
14
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js"
25
import {
36
getMessaging,
47
onBackgroundMessage,
58
} from "https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-sw.js"
69

10+
// Use the precache manifest generated by Vite
11+
precacheAndRoute(self.__WB_MANIFEST)
12+
13+
// Clean up old caches
14+
cleanupOutdatedCaches()
15+
716
const jsonConfig = new URL(location).searchParams.get("config")
817
const firebaseApp = initializeApp(JSON.parse(jsonConfig))
918
const messaging = getMessaging(firebaseApp)
19+
1020
function isChrome() {
1121
return navigator.userAgent.toLowerCase().includes("chrome")
1222
}
23+
1324
onBackgroundMessage(messaging, (payload) => {
1425
const notificationTitle = payload.data.title
1526
let notificationOptions = {
@@ -24,7 +35,7 @@ onBackgroundMessage(messaging, (payload) => {
2435
notificationOptions["actions"] = [
2536
{
2637
action: payload.data.click_action,
27-
title: "View details",
38+
title: "View Details",
2839
},
2940
]
3041
}
@@ -41,3 +52,6 @@ if (isChrome()) {
4152
}
4253
})
4354
}
55+
56+
self.skipWaiting()
57+
clientsClaim()

frontend/vite.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ export default defineConfig({
1414
vue(),
1515
VitePWA({
1616
registerType: "autoUpdate",
17+
strategies: "injectManifest",
18+
injectRegister: null,
1719
devOptions: {
1820
enabled: true,
21+
type: "module",
22+
navigateFallback: "index.html",
1923
},
2024
manifest: {
2125
display: "standalone",
2226
name: "Frappe HR",
2327
short_name: "Frappe HR",
2428
start_url: "/hrms",
2529
description: "Everyday HR & Payroll operations at your fingertips",
30+
theme_color: "#ffffff",
2631
icons: [
2732
{
2833
src: "/assets/hrms/manifest/manifest-icon-192.maskable.png",

0 commit comments

Comments
 (0)