2021-05-06 18:03:34 +02:00
|
|
|
import { registerRoute } from "workbox-routing";
|
2021-05-06 17:20:54 +02:00
|
|
|
import {
|
|
|
|
NetworkFirst,
|
|
|
|
StaleWhileRevalidate,
|
|
|
|
CacheFirst,
|
|
|
|
} from "workbox-strategies";
|
|
|
|
|
|
|
|
// Used for filtering matches based on status code, header, or both
|
|
|
|
import { CacheableResponsePlugin } from "workbox-cacheable-response";
|
|
|
|
// Used to limit entries in cache, remove entries after a certain period of time
|
|
|
|
import { ExpirationPlugin } from "workbox-expiration";
|
|
|
|
|
2021-05-06 18:03:34 +02:00
|
|
|
import { precacheAndRoute } from "workbox-precaching";
|
2021-06-17 16:00:20 +02:00
|
|
|
|
|
|
|
/// <reference lib="WebWorker" />
|
|
|
|
|
|
|
|
// export empty type because of tsc --isolatedModules flag
|
|
|
|
export type {};
|
|
|
|
declare const self: ServiceWorkerGlobalScope;
|
2021-05-06 17:20:54 +02:00
|
|
|
|
|
|
|
// Use with precache injection
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
precacheAndRoute(self.__WB_MANIFEST);
|
|
|
|
|
|
|
|
registerRoute(
|
|
|
|
// Check to see if the request is a navigation to a new page
|
|
|
|
({ request }) => request.mode === "navigate",
|
|
|
|
// Use a Network First caching strategy
|
|
|
|
new NetworkFirst({
|
|
|
|
// Put all cached files in a cache named 'pages'
|
|
|
|
cacheName: "pages",
|
|
|
|
plugins: [
|
|
|
|
// Ensure that only requests that result in a 200 status are cached
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cache CSS, JS, and Web Worker requests with a Stale While Revalidate strategy
|
|
|
|
registerRoute(
|
2021-06-18 18:41:09 +02:00
|
|
|
// Check to see if the request's destination is style for stylesheets, script for JavaScript, font, or worker for web worker
|
2021-05-06 17:20:54 +02:00
|
|
|
({ request }) =>
|
|
|
|
request.destination === "style" ||
|
|
|
|
request.destination === "script" ||
|
2021-06-18 18:41:09 +02:00
|
|
|
request.destination === "font" ||
|
2021-05-06 17:20:54 +02:00
|
|
|
request.destination === "worker",
|
|
|
|
// Use a Stale While Revalidate caching strategy
|
|
|
|
new StaleWhileRevalidate({
|
|
|
|
// Put all cached files in a cache named 'assets'
|
|
|
|
cacheName: "assets",
|
|
|
|
plugins: [
|
|
|
|
// Ensure that only requests that result in a 200 status are cached
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cache images with a Cache First strategy
|
|
|
|
registerRoute(
|
|
|
|
// Check to see if the request's destination is style for an image
|
|
|
|
({ request }) => request.destination === "image",
|
|
|
|
// Use a Cache First caching strategy
|
|
|
|
new CacheFirst({
|
|
|
|
// Put all cached files in a cache named 'images'
|
|
|
|
cacheName: "images",
|
|
|
|
plugins: [
|
|
|
|
// Ensure that only requests that result in a 200 status are cached
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [200],
|
|
|
|
}),
|
|
|
|
// Don't cache more than 50 items, and expire them after 30 days
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxEntries: 50,
|
|
|
|
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 Days
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
2021-05-06 18:39:59 +02:00
|
|
|
|
2021-06-18 18:41:34 +02:00
|
|
|
async function isClientFocused(): Promise<boolean> {
|
|
|
|
const windowClients = await self.clients.matchAll({
|
|
|
|
type: "window",
|
|
|
|
includeUncontrolled: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
let clientIsFocused = false;
|
|
|
|
for (let i = 0; i < windowClients.length; i++) {
|
|
|
|
const windowClient = windowClients[i] as WindowClient;
|
|
|
|
if (windowClient.focused) {
|
|
|
|
clientIsFocused = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientIsFocused;
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:00:20 +02:00
|
|
|
self.addEventListener("push", async (event: PushEvent) => {
|
|
|
|
if (!event.data) return;
|
|
|
|
const payload = event.data.json();
|
2022-08-26 16:08:58 +02:00
|
|
|
console.debug("received push", payload);
|
2021-05-06 18:39:59 +02:00
|
|
|
const options = {
|
|
|
|
body: payload.body,
|
|
|
|
icon: "/img/icons/android-chrome-512x512.png",
|
|
|
|
badge: "/img/icons/badge-128x128.png",
|
2021-06-17 16:00:20 +02:00
|
|
|
timestamp: new Date(payload.timestamp).getTime(),
|
2021-05-06 18:39:59 +02:00
|
|
|
lang: payload.locale,
|
|
|
|
data: {
|
|
|
|
dateOfArrival: Date.now(),
|
2021-06-17 16:00:20 +02:00
|
|
|
url: payload.url,
|
2021-05-06 18:39:59 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-06-18 18:41:34 +02:00
|
|
|
event.waitUntil(
|
|
|
|
(async () => {
|
|
|
|
if (await isClientFocused()) {
|
|
|
|
// No need to show a notification, client already focused
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.registration.showNotification(payload.title, options);
|
|
|
|
})()
|
|
|
|
);
|
2021-06-17 16:00:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener("notificationclick", function (event: NotificationEvent) {
|
|
|
|
const url = event.notification.data.url;
|
|
|
|
event.notification.close();
|
|
|
|
|
|
|
|
// This looks to see if the current is already open and
|
|
|
|
// focuses if it is
|
2021-05-06 18:39:59 +02:00
|
|
|
event.waitUntil(
|
2021-06-17 16:00:20 +02:00
|
|
|
(async () => {
|
|
|
|
const clientList = await self.clients.matchAll({
|
|
|
|
type: "window",
|
2021-06-18 18:41:53 +02:00
|
|
|
includeUncontrolled: true,
|
2021-06-17 16:00:20 +02:00
|
|
|
});
|
|
|
|
for (let i = 0; i < clientList.length; i++) {
|
|
|
|
const client = clientList[i] as WindowClient;
|
|
|
|
if (client.url == url && "focus" in client) {
|
|
|
|
return client.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self.clients.openWindow) {
|
|
|
|
return self.clients.openWindow(url);
|
|
|
|
}
|
|
|
|
})()
|
2021-05-06 18:39:59 +02:00
|
|
|
);
|
|
|
|
});
|
2021-06-18 18:19:04 +02:00
|
|
|
|
|
|
|
self.addEventListener("message", (event: ExtendableMessageEvent) => {
|
|
|
|
const replyPort = event.ports[0];
|
|
|
|
const message = event.data;
|
|
|
|
if (replyPort && message && message.type === "skip-waiting") {
|
2022-08-26 16:08:58 +02:00
|
|
|
console.debug("doing skip waiting");
|
2021-06-18 18:19:04 +02:00
|
|
|
event.waitUntil(
|
|
|
|
self.skipWaiting().then(
|
|
|
|
() => replyPort.postMessage({ error: null }),
|
|
|
|
(error) => replyPort.postMessage({ error })
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|