notification permission fixes, show server urls at bottom, service worker fixes
This commit is contained in:
parent
c13003cd6f
commit
7c9455f31b
|
@ -33,5 +33,7 @@ export const Footer = html`<footer class="container">
|
||||||
second: "numeric",
|
second: "numeric",
|
||||||
})}
|
})}
|
||||||
</p>
|
</p>
|
||||||
|
<p class="center">Server: <code>${import.meta.env.VITE_BASE_URL}</code></p>
|
||||||
|
<p class="center">API: <code>${import.meta.env.VITE_API_BASE_URL}</code></p>
|
||||||
</div>
|
</div>
|
||||||
</footer>`;
|
</footer>`;
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { html } from "uhtml";
|
import { html } from "uhtml";
|
||||||
|
|
||||||
export const Header = html` <hgroup>
|
export const Header = html` <hgroup>
|
||||||
<h1>SURE<span class="primary">DOG</span></h1>
|
<h1>
|
||||||
|
SURE<span class="primary"><a href="/">DOG</a></span>
|
||||||
|
</h1>
|
||||||
<h2>
|
<h2>
|
||||||
<span class="primary">S</span>ecure <span class="primary">U</span>RL
|
<span class="primary">S</span>ecure <span class="primary">U</span>RL
|
||||||
<span class="primary">Re</span>quests
|
<span class="primary">Re</span>quests
|
||||||
|
|
|
@ -23,7 +23,8 @@ export const NotificationManager = () => html` <h2>Notifications</h2>
|
||||||
<p>
|
<p>
|
||||||
<button
|
<button
|
||||||
@click=${subscribeUserToPush}
|
@click=${subscribeUserToPush}
|
||||||
?disabled=${notificationPermission.value === "granted"}
|
?disabled=${notificationPermission.value === "granted" &&
|
||||||
|
pushSubscription.value !== null}
|
||||||
>
|
>
|
||||||
Enable Notifications
|
Enable Notifications
|
||||||
</button>
|
</button>
|
||||||
|
@ -94,12 +95,18 @@ export async function subscribeUserToPush(
|
||||||
) {
|
) {
|
||||||
event?.preventDefault();
|
event?.preventDefault();
|
||||||
try {
|
try {
|
||||||
const permission = await Notification.requestPermission();
|
notificationPermission.value = Notification.permission;
|
||||||
if (permission !== "granted") {
|
if (notificationPermission.value !== "granted") {
|
||||||
throw new Error("Notification permission not granted");
|
const permission = await Notification.requestPermission();
|
||||||
|
|
||||||
|
notificationPermission.value = permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationPermission.value = permission;
|
if (notificationPermission.value !== "granted") {
|
||||||
|
throw new Error(
|
||||||
|
`Notification permission not granted: ${notificationPermission.value}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const registration = await navigator.serviceWorker.ready;
|
const registration = await navigator.serviceWorker.ready;
|
||||||
let subscription = await registration.pushManager.getSubscription();
|
let subscription = await registration.pushManager.getSubscription();
|
||||||
|
|
25
src/sw.js
25
src/sw.js
|
@ -24,13 +24,24 @@ self.addEventListener("push", function (e) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener("notificationclick", function (event) {
|
self.addEventListener("notificationclick", (e) => {
|
||||||
event.notification.close();
|
// Close the notification popout
|
||||||
var url = event.notification.data.url;
|
e.notification.close();
|
||||||
event.waitUntil(
|
// Get all the Window clients
|
||||||
clients
|
e.waitUntil(
|
||||||
.openWindow(event.notification.data.url)
|
clients.matchAll({ type: "window" }).then((clientsArr) => {
|
||||||
.catch((err) => console.log(err))
|
// If a Window tab matching the targeted URL already exists, focus that;
|
||||||
|
const hadWindowToFocus = clientsArr.some((windowClient) =>
|
||||||
|
windowClient.url === e.notification.data.url
|
||||||
|
? (windowClient.focus(), true)
|
||||||
|
: false
|
||||||
|
);
|
||||||
|
// Otherwise, open a new tab to the applicable URL and focus it.
|
||||||
|
if (!hadWindowToFocus)
|
||||||
|
clients
|
||||||
|
.openWindow(e.notification.data.url)
|
||||||
|
.then((windowClient) => (windowClient ? windowClient.focus() : null));
|
||||||
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/// <reference types="vite/client" />
|
/// <reference types="vite/client" />
|
||||||
|
|
||||||
interface ImportMetaEnv {
|
interface ImportMetaEnv {
|
||||||
VITE_BASE_URL?: string;
|
readonly VITE_BASE_URL?: string;
|
||||||
VITE_API_BASE_URL?: string;
|
readonly VITE_API_BASE_URL?: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue