clean up service worker
This commit is contained in:
10
app/service-worker/activate.ts
Normal file
10
app/service-worker/activate.ts
Normal file
@ -0,0 +1,10 @@
|
||||
declare let self: ServiceWorkerGlobalScope;
|
||||
|
||||
export default async function handleActivate(event: ExtendableEvent) {
|
||||
console.debug("Service worker activated");
|
||||
// @ts-ignore
|
||||
if (self.registration.navigationPreload) {
|
||||
// @ts-ignore
|
||||
await self.registration.navigationPreload.enable();
|
||||
}
|
||||
}
|
10
app/service-worker/fetch.ts
Normal file
10
app/service-worker/fetch.ts
Normal file
@ -0,0 +1,10 @@
|
||||
declare let self: ServiceWorkerGlobalScope;
|
||||
|
||||
export default async function handleFetch(event: FetchEvent & { preloadResponse?: Promise<Response | undefined> }) {
|
||||
const preloaded = await event.preloadResponse;
|
||||
if (preloaded) {
|
||||
return preloaded;
|
||||
}
|
||||
|
||||
return fetch(event.request);
|
||||
}
|
5
app/service-worker/install.ts
Normal file
5
app/service-worker/install.ts
Normal file
@ -0,0 +1,5 @@
|
||||
declare let self: ServiceWorkerGlobalScope;
|
||||
|
||||
export default async function handleInstall(event: ExtendableEvent) {
|
||||
console.debug("Service worker installed");
|
||||
}
|
32
app/service-worker/notification-click.ts
Normal file
32
app/service-worker/notification-click.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { removeBadge } from "~/utils/pwa.client";
|
||||
|
||||
declare let self: ServiceWorkerGlobalScope;
|
||||
|
||||
// noinspection TypeScriptUnresolvedVariable
|
||||
export default async function handleNotificationClick(event: NotificationEvent) {
|
||||
console.debug("On notification click: ", event.notification.tag);
|
||||
// Android doesn’t close the notification when you click on it
|
||||
// See: http://crbug.com/463146
|
||||
event.notification.close();
|
||||
await removeBadge();
|
||||
|
||||
const url = getUrl(event.notification.data);
|
||||
return self.clients.openWindow?.(url);
|
||||
}
|
||||
|
||||
type NotificationData = {
|
||||
recipient: string;
|
||||
type: "message" | "incoming-call";
|
||||
};
|
||||
|
||||
function getUrl(data: NotificationData) {
|
||||
const recipient = encodeURIComponent(data.recipient);
|
||||
switch (data.type) {
|
||||
case "message":
|
||||
return `/messages/${recipient}`;
|
||||
case "incoming-call":
|
||||
return `/incoming-call/${recipient}`;
|
||||
default:
|
||||
return "/messages";
|
||||
}
|
||||
}
|
19
app/service-worker/push.ts
Normal file
19
app/service-worker/push.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import type { NotificationPayload } from "~/utils/web-push.server";
|
||||
import { addBadge } from "~/utils/pwa.client";
|
||||
|
||||
declare let self: ServiceWorkerGlobalScope;
|
||||
|
||||
const defaultOptions: NotificationOptions = {
|
||||
icon: "/icons/android-chrome-192x192.png",
|
||||
badge: "/icons/android-chrome-48x48.png",
|
||||
dir: "auto",
|
||||
image: undefined,
|
||||
silent: false,
|
||||
};
|
||||
|
||||
export default async function handlePush(event: PushEvent) {
|
||||
const { title, ...payload }: NotificationPayload = event.data!.json();
|
||||
const options = Object.assign({}, defaultOptions, payload);
|
||||
await self.registration.showNotification(title, options);
|
||||
await addBadge(1);
|
||||
}
|
Reference in New Issue
Block a user