PWA token extend

This commit is contained in:
binwiederhier
2026-05-28 21:58:01 -04:00
parent 85abd40658
commit ef0dde8aa4
3 changed files with 25 additions and 19 deletions
+4 -2
View File
@@ -8,8 +8,10 @@ export const EVENT_MESSAGE_DELETE = "message_delete";
export const EVENT_MESSAGE_CLEAR = "message_clear";
export const EVENT_POLL_REQUEST = "poll_request";
export const WEBPUSH_EVENT_MESSAGE = "message";
export const WEBPUSH_EVENT_SUBSCRIPTION_EXPIRING = "subscription_expiring";
export const SW_WEBPUSH_EVENT_MESSAGE = "message";
export const SW_WEBPUSH_EVENT_SUBSCRIPTION_EXPIRING = "subscription_expiring";
export const SW_PERIODIC_SYNC_EXTEND_TOKEN_TAG = "extend_token";
// 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;
+8 -5
View File
@@ -13,7 +13,7 @@ import versionChecker from "../app/VersionChecker";
import { UnauthorizedError } from "../app/errors";
import notifier from "../app/Notifier";
import prefs from "../app/Prefs";
import { EVENT_MESSAGE_DELETE, EVENT_MESSAGE_CLEAR } from "../app/events";
import { EVENT_MESSAGE_DELETE, EVENT_MESSAGE_CLEAR, SW_PERIODIC_SYNC_EXTEND_TOKEN_TAG } from "../app/events";
/**
* Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection
@@ -284,13 +284,16 @@ export const useStandaloneWebPushAutoSubscribe = () => {
};
/**
* Registers a periodicsync listener for `extend-token` (see sw.js).
* Registers a periodicsync listener for `extend_token` (see sw.js).
* This extends the token regardless of whether the browser is open.
*
* CAVEATS:
* - Chromium-only
* - Only when the PWA is _installed_ (not just running in a browser tab)
* - Only when notifications are granted
*
* This is an experimental feature:
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Periodic_Background_Synchronization_API
*/
const usePeriodicTokenExtend = () => {
const isLaunchedPWA = useIsLaunchedPWA();
@@ -315,9 +318,9 @@ const usePeriodicTokenExtend = () => {
return;
}
console.log(`[usePeriodicTokenExtend] Turning on periodicsync "extend-token"`);
await registration.periodicSync.register("extend-token", {
minInterval: 24 * 60 * 60 * 1000, // 24 hours
console.log(`[usePeriodicTokenExtend] Turning on periodicsync "${SW_PERIODIC_SYNC_EXTEND_TOKEN_TAG}"`);
await registration.periodicSync.register(SW_PERIODIC_SYNC_EXTEND_TOKEN_TAG, {
minInterval: 60 * 1000 // 24 * 60 * 60 * 1000 // 24 hours
});
} catch (error) {
console.log("[usePeriodicTokenExtend] Periodic Sync could not be registered", error);