2022-08-25 18:10:01 +00:00
|
|
|
import { ExpirationPlugin } from 'workbox-expiration';
|
|
|
|
import { precacheAndRoute } from 'workbox-precaching';
|
|
|
|
import { registerRoute } from 'workbox-routing';
|
|
|
|
import { CacheFirst } from 'workbox-strategies';
|
2023-05-23 15:15:17 +00:00
|
|
|
|
2022-08-25 18:10:01 +00:00
|
|
|
import { handleNotificationClick, handlePush } from './web_push_notifications';
|
2017-07-17 22:19:17 +00:00
|
|
|
|
2022-08-25 18:10:01 +00:00
|
|
|
const CACHE_NAME_PREFIX = 'mastodon-';
|
2018-03-27 10:32:30 +00:00
|
|
|
|
|
|
|
function openWebCache() {
|
2022-08-25 18:10:01 +00:00
|
|
|
return caches.open(`${CACHE_NAME_PREFIX}web`);
|
2018-03-18 14:14:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:35:13 +00:00
|
|
|
function fetchRoot() {
|
2018-05-16 02:59:44 +00:00
|
|
|
return fetch('/', { credentials: 'include', redirect: 'manual' });
|
2018-03-17 11:35:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 18:10:01 +00:00
|
|
|
precacheAndRoute(self.__WB_MANIFEST);
|
|
|
|
|
|
|
|
registerRoute(
|
|
|
|
/locale_.*\.js$/,
|
|
|
|
new CacheFirst({
|
|
|
|
cacheName: `${CACHE_NAME_PREFIX}locales`,
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60, // 1 month
|
|
|
|
maxEntries: 5,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
registerRoute(
|
|
|
|
({ request }) => request.destination === 'font',
|
|
|
|
new CacheFirst({
|
|
|
|
cacheName: `${CACHE_NAME_PREFIX}fonts`,
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60, // 1 month
|
|
|
|
maxEntries: 5,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
registerRoute(
|
2022-10-26 16:21:36 +00:00
|
|
|
({ request }) => request.destination === 'image',
|
2022-08-25 18:10:01 +00:00
|
|
|
new CacheFirst({
|
|
|
|
cacheName: `m${CACHE_NAME_PREFIX}media`,
|
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
|
|
|
|
maxEntries: 256,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
2018-05-15 18:14:30 +00:00
|
|
|
|
2017-07-17 22:19:17 +00:00
|
|
|
// Cause a new version of a registered Service Worker to replace an existing one
|
|
|
|
// that is already installed, and replace the currently active worker on open pages.
|
|
|
|
self.addEventListener('install', function(event) {
|
2018-03-27 10:32:30 +00:00
|
|
|
event.waitUntil(Promise.all([openWebCache(), fetchRoot()]).then(([cache, root]) => cache.put('/', root)));
|
2017-07-17 22:19:17 +00:00
|
|
|
});
|
2022-10-23 12:05:18 +00:00
|
|
|
|
2017-07-17 22:19:17 +00:00
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
event.waitUntil(self.clients.claim());
|
|
|
|
});
|
2022-10-23 12:05:18 +00:00
|
|
|
|
2018-03-17 11:35:13 +00:00
|
|
|
self.addEventListener('fetch', function(event) {
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
|
2022-10-23 12:05:18 +00:00
|
|
|
if (url.pathname === '/auth/sign_out') {
|
2018-03-18 14:14:38 +00:00
|
|
|
const asyncResponse = fetch(event.request);
|
2018-03-27 10:32:30 +00:00
|
|
|
const asyncCache = openWebCache();
|
2018-03-18 14:14:38 +00:00
|
|
|
|
2018-05-14 17:58:11 +00:00
|
|
|
event.respondWith(asyncResponse.then(response => {
|
2018-03-18 14:14:38 +00:00
|
|
|
if (response.ok || response.type === 'opaqueredirect') {
|
2018-05-14 17:58:11 +00:00
|
|
|
return Promise.all([
|
2018-04-08 11:32:39 +00:00
|
|
|
asyncCache.then(cache => cache.delete('/')),
|
|
|
|
indexedDB.deleteDatabase('mastodon'),
|
2018-05-14 17:58:11 +00:00
|
|
|
]).then(() => response);
|
2018-03-18 14:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}));
|
2022-08-25 18:10:01 +00:00
|
|
|
}
|
2018-03-17 11:35:13 +00:00
|
|
|
});
|
2022-08-25 18:10:01 +00:00
|
|
|
|
|
|
|
self.addEventListener('push', handlePush);
|
|
|
|
self.addEventListener('notificationclick', handleNotificationClick);
|