open existing client if available

This commit is contained in:
liamcottle
2025-02-13 19:07:09 +13:00
parent 61e446ea62
commit 988ed780fd

View File

@@ -10,6 +10,29 @@ self.addEventListener('notificationclick', function(event) {
event.notification.close();
// open pwa
event.waitUntil(self.clients.openWindow("/"));
const url = "/";
event.waitUntil(findClientForUrl(url).then((client) => {
// if an existing client exists, focus it
if(client){
client.focus();
return;
}
// otherwise open a new window
return self.clients.openWindow(url);
}));
});
const findClientForUrl = function(url) {
return self.clients.matchAll({
type: "window",
includeUncontrolled: true,
}).then(function (clientList) {
return clientList.find(function(client) {
return client.url.endsWith(url);
});
});
};