11const CHAT_PATH = "/chat" ;
22const APP_NAME = "Random Rants +" ;
3- const DEFAULT_TAG = "randomrants-plus" ;
3+ const DEFAULT_TAG_PREFIX = "randomrants-plus" ;
44
55var origin = "" + self . location . origin ;
66origin = origin . trim ( ) ;
@@ -21,9 +21,12 @@ async function sendPushNotification({
2121 title = APP_NAME ,
2222 body = "" ,
2323 icon = "/images/push-notify-icon.png" ,
24- tag = DEFAULT_TAG ,
24+ tag = DEFAULT_TAG_PREFIX ,
2525 data = { } ,
2626} ) {
27+ if ( tag == DEFAULT_TAG_PREFIX ) {
28+ tag += "-" + Date . now ( ) ;
29+ }
2730 await self . registration . showNotification ( title , {
2831 body : body ,
2932 icon : icon ,
@@ -156,7 +159,9 @@ self.addEventListener("message", async (event) => {
156159 // Save the current timestamp
157160 await cache . put ( "last-seen-online" , new Response ( Date . now ( ) . toString ( ) ) ) ;
158161 } else if ( event . data && event . data . type === "NOTIFY" ) {
159- await sendPushNotification ( event . data . payload ) ;
162+ if ( await isUserSubscribed ( ) ) {
163+ await sendPushNotification ( event . data . payload ) ;
164+ }
160165 }
161166} ) ;
162167
@@ -191,6 +196,23 @@ self.addEventListener("activate", (event) => {
191196 event . waitUntil ( async ( ) => {
192197 await clients . claim ( ) ;
193198
199+ // Check for "Ghost" registrations
200+ const registrations = ( await self . registration . navigationPreload . getState ( ) )
201+ ? [ ] // Preload check
202+ : await self . registration . scope ;
203+
204+ const allRegs = ( await self . registration . getRegistrations )
205+ ? await self . registration . getRegistrations ( )
206+ : [ ] ;
207+
208+ for ( let reg of allRegs ) {
209+ // If there is another worker with a different scope, kill it
210+ if ( reg . scope !== self . registration . scope ) {
211+ console . warn ( "Killing duplicate worker at scope:" , reg . scope ) ;
212+ await reg . unregister ( ) ;
213+ }
214+ }
215+
194216 if ( ! ( await isUserSubscribed ( ) ) ) {
195217 return ;
196218 }
0 commit comments