aboutsummaryrefslogtreecommitdiffstats
path: root/src/service-worker.js
blob: fa934333b2cded1faf5c8d6d67b035eaf873bef8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { build, files, version } from "$service-worker";

self.addEventListener("install", (event) => {});
self.addEventListener("activate", (event) => {});
self.addEventListener("fetch", (event) => {});
self.addEventListener("push", (event) => {
	const { title, body, url } = event.data.json();
	const icon = "/images/icon.png";
	const badge = "/images/badge.png";
	self.registration.showNotification(title, {
		body,
		icon,
		badge,
		data: { url },
	});
});

self.addEventListener("notificationclick", (event) => {
	event.notification.close();

	const url = event.notification.data.url || "/";

	event.waitUntil(
		clients
			.matchAll({ type: "window", includeUncontrolled: true })
			.then((clientList) => {
				for (const client of clientList) {
					if (client.url === url && "focus" in client) {
						return client.focus();
					}
				}
				if (clients.openWindow) {
					return clients.openWindow(url);
				}
			}),
	);
});