mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix: service worker improvements for PWA reliability (#127)
This commit is contained in:
1 parent
79b034d3ba
commit
626e2b1954
13 files changed
+51
-2879
No files matched your search
@@ -1,10 +1,8 @@
|
||||
import "../sw?worker";
|
||||
|
||||
export function registerServiceWorker(): void {
|
||||
if (!("serviceWorker" in navigator)) return;
|
||||
|
||||
navigator.serviceWorker
|
||||
.register("/sw.js", { scope: "/", type: "module" })
|
||||
.then((registration) => registration.update())
|
||||
.catch(() => {});
|
||||
navigator.serviceWorker.register("/sw.js", { scope: "/" }).catch(() => {});
|
||||
}
|
||||
|
||||
export async function getServiceWorkerRegistration(): Promise<ServiceWorkerRegistration | null> {
|
||||
|
||||
+15
-17
@@ -1,12 +1,18 @@
|
||||
/// <reference lib="webworker" />
|
||||
import { precacheAndRoute, cleanupOutdatedCaches } from "workbox-precaching";
|
||||
|
||||
declare const self: ServiceWorkerGlobalScope & {
|
||||
__WB_MANIFEST: Array<{ url: string; revision: string | null }>;
|
||||
};
|
||||
declare const self: ServiceWorkerGlobalScope & typeof globalThis;
|
||||
|
||||
cleanupOutdatedCaches();
|
||||
precacheAndRoute(self.__WB_MANIFEST);
|
||||
self.addEventListener("install", () => {
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((names) =>
|
||||
Promise.all(names.map((name) => caches.delete(name)))
|
||||
).then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
interface PushNotificationData {
|
||||
title: string;
|
||||
@@ -22,15 +28,7 @@ interface PushNotificationData {
|
||||
};
|
||||
}
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener("install", () => {
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener("push", (event: PushEvent) => {
|
||||
self.addEventListener("push", (event) => {
|
||||
if (!event.data) return;
|
||||
|
||||
let payload: PushNotificationData;
|
||||
@@ -56,7 +54,7 @@ self.addEventListener("push", (event: PushEvent) => {
|
||||
event.waitUntil(self.registration.showNotification(payload.title, options));
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event: NotificationEvent) => {
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
|
||||
const url = (event.notification.data?.url as string) ?? "/";
|
||||
@@ -67,7 +65,7 @@ self.addEventListener("notificationclick", (event: NotificationEvent) => {
|
||||
.then((clientList) => {
|
||||
for (const client of clientList) {
|
||||
if (new URL(client.url).origin === self.location.origin) {
|
||||
client.focus();
|
||||
(client as WindowClient).focus();
|
||||
(client as WindowClient).navigate(url);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user