Fix service worker handling for updating/deleting
This commit is contained in:
@@ -2,8 +2,9 @@ import api from "./Api";
|
||||
import notifier from "./Notifier";
|
||||
import prefs from "./Prefs";
|
||||
import db from "./db";
|
||||
import { messageWithSequenceId, topicUrl } from "./utils";
|
||||
import { EVENT_MESSAGE, EVENT_MESSAGE_DELETE, EVENT_MESSAGE_CLEAR } from "./events";
|
||||
import { topicUrl } from "./utils";
|
||||
import { messageWithSequenceId } from "./notificationUtils";
|
||||
import { EVENT_MESSAGE, EVENT_MESSAGE_CLEAR, EVENT_MESSAGE_DELETE } from "./events";
|
||||
|
||||
class SubscriptionManager {
|
||||
constructor(dbImpl) {
|
||||
|
||||
@@ -7,6 +7,7 @@ export const EVENT_MESSAGE = "message";
|
||||
export const EVENT_MESSAGE_DELETE = "message_delete";
|
||||
export const EVENT_MESSAGE_CLEAR = "message_clear";
|
||||
export const EVENT_POLL_REQUEST = "poll_request";
|
||||
export const EVENT_SUBSCRIPTION_EXPIRING = "subscription_expiring";
|
||||
|
||||
// Check if an event is a notification event (message, delete, or read)
|
||||
export const isNotificationEvent = (event) => event === EVENT_MESSAGE || event === EVENT_MESSAGE_DELETE || event === EVENT_MESSAGE_CLEAR;
|
||||
|
||||
@@ -25,13 +25,13 @@ const formatTitleWithDefault = (m, fallback) => {
|
||||
|
||||
export const formatMessage = (m) => {
|
||||
if (m.title) {
|
||||
return m.message;
|
||||
return m.message || "";
|
||||
}
|
||||
const emojiList = toEmojis(m.tags);
|
||||
if (emojiList.length > 0) {
|
||||
return `${emojiList.join(" ")} ${m.message}`;
|
||||
return `${emojiList.join(" ")} ${m.message || ""}`;
|
||||
}
|
||||
return m.message;
|
||||
return m.message || "";
|
||||
};
|
||||
|
||||
const imageRegex = /\.(png|jpe?g|gif|webp)$/i;
|
||||
@@ -79,3 +79,10 @@ export const toNotificationParams = ({ message, defaultTitle, topicRoute }) => {
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export const messageWithSequenceId = (message) => {
|
||||
if (message.sequenceId) {
|
||||
return message;
|
||||
}
|
||||
return { ...message, sequenceId: message.sequence_id || message.id };
|
||||
};
|
||||
|
||||
@@ -103,13 +103,6 @@ export const maybeActionErrors = (notification) => {
|
||||
return actionErrors;
|
||||
};
|
||||
|
||||
export const messageWithSequenceId = (message) => {
|
||||
if (message.sequenceId) {
|
||||
return message;
|
||||
}
|
||||
return { ...message, sequenceId: message.sequence_id || message.id };
|
||||
};
|
||||
|
||||
export const shuffle = (arr) => {
|
||||
const returnArr = [...arr];
|
||||
|
||||
|
||||
@@ -5,10 +5,21 @@ import { registerSW as viteRegisterSW } from "virtual:pwa-register";
|
||||
const intervalMS = 60 * 60 * 1000;
|
||||
|
||||
// https://vite-pwa-org.netlify.app/guide/periodic-sw-updates.html
|
||||
const registerSW = () =>
|
||||
const registerSW = () => {
|
||||
console.log("[ServiceWorker] Registering service worker");
|
||||
console.log("[ServiceWorker] serviceWorker in navigator:", "serviceWorker" in navigator);
|
||||
|
||||
if (!("serviceWorker" in navigator)) {
|
||||
console.warn("[ServiceWorker] Service workers not supported");
|
||||
return;
|
||||
}
|
||||
|
||||
viteRegisterSW({
|
||||
onRegisteredSW(swUrl, registration) {
|
||||
console.log("[ServiceWorker] Registered:", { swUrl, registration });
|
||||
|
||||
if (!registration) {
|
||||
console.warn("[ServiceWorker] No registration returned");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -23,9 +34,16 @@ const registerSW = () =>
|
||||
},
|
||||
});
|
||||
|
||||
if (resp?.status === 200) await registration.update();
|
||||
if (resp?.status === 200) {
|
||||
console.log("[ServiceWorker] Updating service worker");
|
||||
await registration.update();
|
||||
}
|
||||
}, intervalMS);
|
||||
},
|
||||
onRegisterError(error) {
|
||||
console.error("[ServiceWorker] Registration error:", error);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default registerSW;
|
||||
|
||||
Reference in New Issue
Block a user