fix: ui fixes
This commit is contained in:
@@ -11,6 +11,51 @@ self.addEventListener("activate", (event) =>
|
||||
event.waitUntil(self.clients.claim()),
|
||||
);
|
||||
|
||||
// The page registers us as /sw.js?bff=<origin> so subscription management
|
||||
// works without a client tab open (the auth cookie rides on
|
||||
// credentials:"include"). Same-origin deployments fall back to our origin.
|
||||
const BFF_BASE =
|
||||
new URL(self.location.href).searchParams.get("bff") || self.location.origin;
|
||||
|
||||
/* Push services rotate subscriptions (expiry, key migration). Without this
|
||||
* handler the server keeps pushing to the dead endpoint and the user silently
|
||||
* stops getting notifications — so re-subscribe with the same application
|
||||
* server key and swap the registration on the BFF. */
|
||||
self.addEventListener("pushsubscriptionchange", (event) => {
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
const old = event.oldSubscription;
|
||||
let sub = event.newSubscription || null;
|
||||
if (!sub) {
|
||||
const appKey = old && old.options && old.options.applicationServerKey;
|
||||
if (!appKey) return; // nothing to re-subscribe with
|
||||
sub = await self.registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: appKey,
|
||||
});
|
||||
}
|
||||
|
||||
const json = sub.toJSON();
|
||||
await fetch(`${BFF_BASE}/api/v1/push/subscribe`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ endpoint: json.endpoint, keys: json.keys }),
|
||||
});
|
||||
if (old && old.endpoint && old.endpoint !== sub.endpoint) {
|
||||
await fetch(`${BFF_BASE}/api/v1/push/unsubscribe`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ endpoint: old.endpoint }),
|
||||
}).catch(() => {
|
||||
/* best-effort — the server prunes dead endpoints on send anyway */
|
||||
});
|
||||
}
|
||||
})(),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
|
||||
Reference in New Issue
Block a user