this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Rewrite to be slightly more readable

Also, try to fix openWindow not working for Safari PWA

+31 -43
+31 -43
public/sw.js
··· 161 161 console.log('NOTIFICATION CLICK payload', payload); 162 162 const { badge, body, data, dir, icon, lang, tag, timestamp, title } = payload; 163 163 const { access_token, notification_type } = data; 164 - const actions = new Promise((resolve) => { 165 - event.notification.close(); 166 - const url = `/#/notifications?id=${tag}&access_token=${btoa(access_token)}`; 167 - self.clients 168 - .matchAll({ 164 + const url = `/#/notifications?id=${tag}&access_token=${btoa(access_token)}`; 165 + 166 + event.waitUntil( 167 + (async () => { 168 + const clients = await self.clients.matchAll({ 169 169 type: 'window', 170 170 includeUncontrolled: true, 171 - }) 172 - .then((clients) => { 173 - console.log('NOTIFICATION CLICK clients 1', clients); 174 - if (clients.length && 'navigate' in clients[0]) { 175 - console.log('NOTIFICATION CLICK clients 2', clients); 176 - const bestClient = 177 - clients.find( 178 - (client) => 179 - client.focused || client.visibilityState === 'visible', 180 - ) || clients[0]; 181 - console.log('NOTIFICATION CLICK navigate', url); 182 - // Check if URL is root / or /notifications 183 - // const clientURL = new URL(bestClient.url); 184 - // if ( 185 - // /^#\/?$/.test(clientURL.hash) || 186 - // /^#\/notifications/i.test(clientURL.hash) 187 - // ) { 188 - // bestClient.navigate(url).then((client) => client?.focus()); 189 - // } else { 190 - // User might be on a different page (e.g. composing a post), so don't navigate anywhere else 191 - if (bestClient) { 192 - console.log('NOTIFICATION CLICK postMessage', bestClient); 193 - bestClient.postMessage?.({ 194 - type: 'notification', 195 - id: tag, 196 - accessToken: access_token, 197 - }); 198 - bestClient.focus(); 199 - } else { 200 - console.log('NOTIFICATION CLICK openWindow', url); 201 - self.clients.openWindow(url); 202 - } 203 - // } 171 + }); 172 + console.log('NOTIFICATION CLICK clients 1', clients); 173 + if (clients.length && 'navigate' in clients[0]) { 174 + console.log('NOTIFICATION CLICK clients 2', clients); 175 + const bestClient = 176 + clients.find( 177 + (client) => client.focused || client.visibilityState === 'visible', 178 + ) || clients[0]; 179 + console.log('NOTIFICATION CLICK navigate', url); 180 + if (bestClient) { 181 + console.log('NOTIFICATION CLICK postMessage', bestClient); 182 + bestClient.postMessage?.({ 183 + type: 'notification', 184 + id: tag, 185 + accessToken: access_token, 186 + }); 187 + bestClient.focus(); 204 188 } else { 205 189 console.log('NOTIFICATION CLICK openWindow', url); 206 - self.clients.openWindow(url); 190 + await self.clients.openWindow(url); 207 191 } 208 - resolve(); 209 - }); 210 - }); 211 - event.waitUntil(actions); 192 + // } 193 + } else { 194 + console.log('NOTIFICATION CLICK openWindow', url); 195 + await self.clients.openWindow(url); 196 + } 197 + await event.notification.close(); 198 + })(), 199 + ); 212 200 });