entry.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { ExpirationPlugin } from 'workbox-expiration';
  2. import { precacheAndRoute } from 'workbox-precaching';
  3. import { registerRoute } from 'workbox-routing';
  4. import { CacheFirst } from 'workbox-strategies';
  5. import { handleNotificationClick, handlePush } from './web_push_notifications';
  6. const CACHE_NAME_PREFIX = 'mastodon-';
  7. function openWebCache() {
  8. return caches.open(`${CACHE_NAME_PREFIX}web`);
  9. }
  10. function fetchRoot() {
  11. return fetch('/', { credentials: 'include', redirect: 'manual' });
  12. }
  13. precacheAndRoute(self.__WB_MANIFEST);
  14. registerRoute(
  15. /locale_.*\.js$/,
  16. new CacheFirst({
  17. cacheName: `${CACHE_NAME_PREFIX}locales`,
  18. plugins: [
  19. new ExpirationPlugin({
  20. maxAgeSeconds: 30 * 24 * 60 * 60, // 1 month
  21. maxEntries: 5,
  22. }),
  23. ],
  24. }),
  25. );
  26. registerRoute(
  27. ({ request }) => request.destination === 'font',
  28. new CacheFirst({
  29. cacheName: `${CACHE_NAME_PREFIX}fonts`,
  30. plugins: [
  31. new ExpirationPlugin({
  32. maxAgeSeconds: 30 * 24 * 60 * 60, // 1 month
  33. maxEntries: 5,
  34. }),
  35. ],
  36. }),
  37. );
  38. registerRoute(
  39. ({ request }) => ['audio', 'image', 'track', 'video'].includes(request.destination),
  40. new CacheFirst({
  41. cacheName: `m${CACHE_NAME_PREFIX}media`,
  42. plugins: [
  43. new ExpirationPlugin({
  44. maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
  45. maxEntries: 256,
  46. }),
  47. ],
  48. }),
  49. );
  50. // Cause a new version of a registered Service Worker to replace an existing one
  51. // that is already installed, and replace the currently active worker on open pages.
  52. self.addEventListener('install', function(event) {
  53. event.waitUntil(Promise.all([openWebCache(), fetchRoot()]).then(([cache, root]) => cache.put('/', root)));
  54. });
  55. self.addEventListener('activate', function(event) {
  56. event.waitUntil(self.clients.claim());
  57. });
  58. self.addEventListener('fetch', function(event) {
  59. const url = new URL(event.request.url);
  60. if (url.pathname.startsWith('/web/')) {
  61. const asyncResponse = fetchRoot();
  62. const asyncCache = openWebCache();
  63. event.respondWith(asyncResponse.then(
  64. response => {
  65. const clonedResponse = response.clone();
  66. asyncCache.then(cache => cache.put('/', clonedResponse)).catch();
  67. return response;
  68. },
  69. () => asyncCache.then(cache => cache.match('/'))));
  70. } else if (url.pathname === '/auth/sign_out') {
  71. const asyncResponse = fetch(event.request);
  72. const asyncCache = openWebCache();
  73. event.respondWith(asyncResponse.then(response => {
  74. if (response.ok || response.type === 'opaqueredirect') {
  75. return Promise.all([
  76. asyncCache.then(cache => cache.delete('/')),
  77. indexedDB.deleteDatabase('mastodon'),
  78. ]).then(() => response);
  79. }
  80. return response;
  81. }));
  82. }
  83. });
  84. self.addEventListener('push', handlePush);
  85. self.addEventListener('notificationclick', handleNotificationClick);