2022-12-19 09:51:56 +03:00
|
|
|
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
|
|
|
|
import { ExpirationPlugin } from 'workbox-expiration';
|
2024-09-17 04:26:15 +03:00
|
|
|
import * as navigationPreload from 'workbox-navigation-preload';
|
2022-12-19 09:51:56 +03:00
|
|
|
import { RegExpRoute, registerRoute, Route } from 'workbox-routing';
|
2023-01-10 12:08:10 +03:00
|
|
|
import {
|
|
|
|
CacheFirst,
|
|
|
|
NetworkFirst,
|
|
|
|
StaleWhileRevalidate,
|
|
|
|
} from 'workbox-strategies';
|
2022-12-19 09:51:56 +03:00
|
|
|
|
2024-09-17 04:26:15 +03:00
|
|
|
navigationPreload.enable();
|
|
|
|
|
2023-01-02 09:22:31 +03:00
|
|
|
self.__WB_DISABLE_DEV_LOGS = true;
|
|
|
|
|
2023-09-14 18:21:43 +03:00
|
|
|
const assetsRoute = new Route(
|
|
|
|
({ request, sameOrigin }) => {
|
|
|
|
const isAsset =
|
|
|
|
request.destination === 'style' || request.destination === 'script';
|
2024-09-17 12:35:01 +03:00
|
|
|
const hasHash = /-[0-9a-z-]{4,}\./i.test(request.url);
|
2023-09-14 18:21:43 +03:00
|
|
|
return sameOrigin && isAsset && hasHash;
|
|
|
|
},
|
|
|
|
new NetworkFirst({
|
|
|
|
cacheName: 'assets',
|
|
|
|
networkTimeoutSeconds: 5,
|
|
|
|
plugins: [
|
2024-09-17 12:35:01 +03:00
|
|
|
new ExpirationPlugin({
|
|
|
|
maxEntries: 30,
|
|
|
|
purgeOnQuotaError: true,
|
|
|
|
}),
|
2023-09-14 18:21:43 +03:00
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(assetsRoute);
|
|
|
|
|
2022-12-19 09:51:56 +03:00
|
|
|
const imageRoute = new Route(
|
|
|
|
({ request, sameOrigin }) => {
|
2022-12-20 08:21:08 +03:00
|
|
|
const isRemote = !sameOrigin;
|
|
|
|
const isImage = request.destination === 'image';
|
|
|
|
const isAvatar = request.url.includes('/avatars/');
|
2024-02-29 13:18:40 +03:00
|
|
|
const isCustomEmoji = request.url.includes('/custom/_emojis');
|
2022-12-20 08:21:08 +03:00
|
|
|
const isEmoji = request.url.includes('/emoji/');
|
2024-02-29 13:18:40 +03:00
|
|
|
return isRemote && isImage && (isAvatar || isCustomEmoji || isEmoji);
|
2022-12-19 09:51:56 +03:00
|
|
|
},
|
|
|
|
new CacheFirst({
|
|
|
|
cacheName: 'remote-images',
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
2024-09-17 12:35:01 +03:00
|
|
|
maxEntries: 30,
|
2022-12-19 09:51:56 +03:00
|
|
|
purgeOnQuotaError: true,
|
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(imageRoute);
|
|
|
|
|
2023-04-14 12:23:41 +03:00
|
|
|
const iconsRoute = new Route(
|
|
|
|
({ request, sameOrigin }) => {
|
|
|
|
const isIcon = request.url.includes('/icons/');
|
|
|
|
return sameOrigin && isIcon;
|
|
|
|
},
|
|
|
|
new CacheFirst({
|
|
|
|
cacheName: 'icons',
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
2024-05-15 14:38:28 +03:00
|
|
|
maxEntries: 300,
|
2023-04-14 12:23:41 +03:00
|
|
|
maxAgeSeconds: 3 * 24 * 60 * 60, // 3 days
|
|
|
|
purgeOnQuotaError: true,
|
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(iconsRoute);
|
|
|
|
|
2023-02-23 15:54:23 +03:00
|
|
|
// 1-day cache for
|
|
|
|
// - /api/v1/custom_emojis
|
|
|
|
// - /api/v1/lists/:id
|
2023-05-21 03:36:59 +03:00
|
|
|
// - /api/v1/announcements
|
2022-12-19 09:51:56 +03:00
|
|
|
const apiExtendedRoute = new RegExpRoute(
|
2024-09-16 19:45:27 +03:00
|
|
|
/^https?:\/\/[^\/]+\/api\/v\d+\/(custom_emojis|lists\/\d+|announcements)$/,
|
2022-12-19 09:51:56 +03:00
|
|
|
new StaleWhileRevalidate({
|
|
|
|
cacheName: 'api-extended',
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
2024-09-17 12:35:01 +03:00
|
|
|
maxAgeSeconds: 12 * 60 * 60, // 12 hours
|
2024-09-17 12:16:12 +03:00
|
|
|
purgeOnQuotaError: true,
|
2022-12-19 09:51:56 +03:00
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(apiExtendedRoute);
|
|
|
|
|
2024-08-04 13:05:03 +03:00
|
|
|
// Note: expiration is not working as expected
|
|
|
|
// https://github.com/GoogleChrome/workbox/issues/3316
|
|
|
|
//
|
|
|
|
// const apiIntermediateRoute = new RegExpRoute(
|
|
|
|
// // Matches:
|
|
|
|
// // - trends/*
|
|
|
|
// // - timelines/link
|
|
|
|
// /^https?:\/\/[^\/]+\/api\/v\d+\/(trends|timelines\/link)/,
|
|
|
|
// new StaleWhileRevalidate({
|
|
|
|
// cacheName: 'api-intermediate',
|
|
|
|
// plugins: [
|
|
|
|
// new ExpirationPlugin({
|
|
|
|
// maxAgeSeconds: 1 * 60, // 1min
|
|
|
|
// }),
|
|
|
|
// new CacheableResponsePlugin({
|
|
|
|
// statuses: [0, 200],
|
|
|
|
// }),
|
|
|
|
// ],
|
|
|
|
// }),
|
|
|
|
// );
|
|
|
|
// registerRoute(apiIntermediateRoute);
|
2024-06-19 07:22:17 +03:00
|
|
|
|
2023-01-02 09:23:00 +03:00
|
|
|
const apiRoute = new RegExpRoute(
|
|
|
|
// Matches:
|
|
|
|
// - statuses/:id/context - some contexts are really huge
|
|
|
|
/^https?:\/\/[^\/]+\/api\/v\d+\/(statuses\/\d+\/context)/,
|
2023-01-09 20:28:52 +03:00
|
|
|
new NetworkFirst({
|
2023-01-02 09:23:00 +03:00
|
|
|
cacheName: 'api',
|
2023-01-09 20:28:52 +03:00
|
|
|
networkTimeoutSeconds: 5,
|
2023-01-02 09:23:00 +03:00
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
2024-09-17 12:35:01 +03:00
|
|
|
maxEntries: 30,
|
2023-01-02 09:23:00 +03:00
|
|
|
maxAgeSeconds: 5 * 60, // 5 minutes
|
2024-09-17 12:16:12 +03:00
|
|
|
purgeOnQuotaError: true,
|
2023-01-02 09:23:00 +03:00
|
|
|
}),
|
|
|
|
new CacheableResponsePlugin({
|
|
|
|
statuses: [0, 200],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
registerRoute(apiRoute);
|
2023-09-01 10:40:00 +03:00
|
|
|
|
|
|
|
// PUSH NOTIFICATIONS
|
|
|
|
// ==================
|
|
|
|
|
|
|
|
self.addEventListener('push', (event) => {
|
|
|
|
const { data } = event;
|
|
|
|
if (data) {
|
|
|
|
const payload = data.json();
|
|
|
|
console.log('PUSH payload', payload);
|
|
|
|
const {
|
|
|
|
access_token,
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
icon,
|
|
|
|
notification_id,
|
|
|
|
notification_type,
|
|
|
|
preferred_locale,
|
|
|
|
} = payload;
|
|
|
|
|
|
|
|
if (!!navigator.setAppBadge) {
|
|
|
|
if (notification_type === 'mention') {
|
|
|
|
navigator.setAppBadge(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
self.registration.showNotification(title, {
|
|
|
|
body,
|
|
|
|
icon,
|
|
|
|
dir: 'auto',
|
2023-09-20 12:28:42 +03:00
|
|
|
badge: '/logo-badge-72.png',
|
2023-09-01 10:40:00 +03:00
|
|
|
lang: preferred_locale,
|
|
|
|
tag: notification_id,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
data: {
|
|
|
|
access_token,
|
|
|
|
notification_type,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
|
|
const payload = event.notification;
|
|
|
|
console.log('NOTIFICATION CLICK payload', payload);
|
|
|
|
const { badge, body, data, dir, icon, lang, tag, timestamp, title } = payload;
|
|
|
|
const { access_token, notification_type } = data;
|
2023-10-01 18:20:48 +03:00
|
|
|
const url = `/#/notifications?id=${tag}&access_token=${btoa(access_token)}`;
|
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
(async () => {
|
|
|
|
const clients = await self.clients.matchAll({
|
2023-09-01 10:40:00 +03:00
|
|
|
type: 'window',
|
|
|
|
includeUncontrolled: true,
|
2023-10-01 18:20:48 +03:00
|
|
|
});
|
|
|
|
console.log('NOTIFICATION CLICK clients 1', clients);
|
|
|
|
if (clients.length && 'navigate' in clients[0]) {
|
|
|
|
console.log('NOTIFICATION CLICK clients 2', clients);
|
|
|
|
const bestClient =
|
|
|
|
clients.find(
|
|
|
|
(client) => client.focused || client.visibilityState === 'visible',
|
|
|
|
) || clients[0];
|
|
|
|
console.log('NOTIFICATION CLICK navigate', url);
|
|
|
|
if (bestClient) {
|
|
|
|
console.log('NOTIFICATION CLICK postMessage', bestClient);
|
2023-10-19 12:45:27 +03:00
|
|
|
bestClient.focus();
|
2023-10-01 18:20:48 +03:00
|
|
|
bestClient.postMessage?.({
|
|
|
|
type: 'notification',
|
|
|
|
id: tag,
|
|
|
|
accessToken: access_token,
|
|
|
|
});
|
2023-09-01 10:40:00 +03:00
|
|
|
} else {
|
|
|
|
console.log('NOTIFICATION CLICK openWindow', url);
|
2023-10-01 18:20:48 +03:00
|
|
|
await self.clients.openWindow(url);
|
2023-09-01 10:40:00 +03:00
|
|
|
}
|
2023-10-01 18:20:48 +03:00
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
console.log('NOTIFICATION CLICK openWindow', url);
|
|
|
|
await self.clients.openWindow(url);
|
|
|
|
}
|
2023-10-16 16:38:14 +03:00
|
|
|
await event.notification.close();
|
2023-10-01 18:20:48 +03:00
|
|
|
})(),
|
|
|
|
);
|
2023-09-01 10:40:00 +03:00
|
|
|
});
|