2022-12-18 08:29:16 +09:00
|
|
|
/// <reference lib="WebWorker" />
|
|
|
|
/// <reference types="vite/client" />
|
2024-09-30 17:11:56 +09:00
|
|
|
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
|
|
|
|
import { ExpirationPlugin } from 'workbox-expiration'
|
2022-12-18 08:29:16 +09:00
|
|
|
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching'
|
|
|
|
import { NavigationRoute, registerRoute } from 'workbox-routing'
|
2023-01-17 19:42:42 +09:00
|
|
|
import { NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
|
2023-01-07 04:38:40 +09:00
|
|
|
|
2023-01-15 05:58:52 +09:00
|
|
|
import { onShareTarget } from './share-target'
|
2024-09-30 17:11:56 +09:00
|
|
|
import { onNotificationClick, onPush } from './web-push-notifications'
|
2022-12-18 08:29:16 +09:00
|
|
|
|
|
|
|
declare const self: ServiceWorkerGlobalScope
|
2023-01-03 04:18:33 +09:00
|
|
|
|
2022-12-18 08:29:16 +09:00
|
|
|
self.addEventListener('message', (event) => {
|
|
|
|
if (event.data && event.data.type === 'SKIP_WAITING')
|
|
|
|
self.skipWaiting()
|
|
|
|
})
|
|
|
|
|
|
|
|
const entries = self.__WB_MANIFEST
|
|
|
|
if (import.meta.env.DEV)
|
|
|
|
entries.push({ url: '/', revision: Math.random().toString() })
|
|
|
|
|
|
|
|
precacheAndRoute(entries)
|
|
|
|
|
|
|
|
// clean old assets
|
|
|
|
cleanupOutdatedCaches()
|
|
|
|
|
|
|
|
// allow only fallback in dev: we don't want to cache anything
|
|
|
|
let allowlist: undefined | RegExp[]
|
|
|
|
if (import.meta.env.DEV)
|
|
|
|
allowlist = [/^\/$/]
|
|
|
|
|
|
|
|
// deny api and server page calls
|
|
|
|
let denylist: undefined | RegExp[]
|
2023-05-22 01:28:28 +09:00
|
|
|
if (import.meta.env.PROD) {
|
|
|
|
denylist = [
|
|
|
|
/^\/api\//,
|
|
|
|
/^\/login\//,
|
|
|
|
/^\/oauth\//,
|
|
|
|
/^\/signin\//,
|
|
|
|
/^\/web-share-target\//,
|
2023-12-21 03:54:40 +09:00
|
|
|
// exclude emoji: has its own cache
|
2023-05-22 01:28:28 +09:00
|
|
|
/^\/emojis\//,
|
|
|
|
// exclude sw: if the user navigates to it, fallback to index.html
|
|
|
|
/^\/sw.js$/,
|
2024-09-23 21:45:46 +09:00
|
|
|
/^\/elk-sw.js$/,
|
2023-05-22 01:28:28 +09:00
|
|
|
// exclude webmanifest: has its own cache
|
|
|
|
/^\/manifest-(.*).webmanifest$/,
|
|
|
|
]
|
|
|
|
}
|
2022-12-18 08:29:16 +09:00
|
|
|
|
|
|
|
// only cache pages and external assets on local build + start or in production
|
|
|
|
if (import.meta.env.PROD) {
|
2023-01-17 19:42:42 +09:00
|
|
|
// include webmanifest cache
|
|
|
|
registerRoute(
|
|
|
|
({ request, sameOrigin }) =>
|
|
|
|
sameOrigin && request.destination === 'manifest',
|
|
|
|
new NetworkFirst({
|
|
|
|
cacheName: 'elk-webmanifest',
|
|
|
|
plugins: [
|
|
|
|
new CacheableResponsePlugin({ statuses: [200] }),
|
|
|
|
// we only need a few entries
|
|
|
|
new ExpirationPlugin({ maxEntries: 100 }),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
)
|
2023-01-03 18:42:05 +09:00
|
|
|
// include emoji icons
|
2023-01-03 04:18:33 +09:00
|
|
|
registerRoute(
|
|
|
|
({ sameOrigin, request, url }) =>
|
|
|
|
sameOrigin
|
2024-03-05 23:48:58 +09:00
|
|
|
&& request.destination === 'image'
|
|
|
|
&& url.pathname.startsWith('/emojis/'),
|
2023-01-03 04:18:33 +09:00
|
|
|
new StaleWhileRevalidate({
|
|
|
|
cacheName: 'elk-emojis',
|
|
|
|
plugins: [
|
|
|
|
new CacheableResponsePlugin({ statuses: [200] }),
|
|
|
|
// 15 days max
|
2023-05-22 01:28:28 +09:00
|
|
|
new ExpirationPlugin({ purgeOnQuotaError: true, maxAgeSeconds: 60 * 60 * 24 * 15 }),
|
2023-01-03 04:18:33 +09:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
)
|
2022-12-18 08:29:16 +09:00
|
|
|
// external assets: rn avatars from mas.to
|
|
|
|
// requires <img crossorigin="anonymous".../> and http header: Allow-Control-Allow-Origin: *
|
|
|
|
/*
|
|
|
|
registerRoute(
|
|
|
|
({ sameOrigin, request }) => !sameOrigin && request.destination === 'image',
|
|
|
|
new NetworkFirst({
|
|
|
|
cacheName: 'elk-external-media',
|
|
|
|
plugins: [
|
|
|
|
// add opaque responses?
|
|
|
|
new CacheableResponsePlugin({ statuses: [/!* 0, *!/200] }),
|
|
|
|
// 15 days max
|
|
|
|
new ExpirationPlugin({ maxAgeSeconds: 60 * 60 * 24 * 15 }),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// to allow work offline
|
|
|
|
registerRoute(new NavigationRoute(
|
|
|
|
createHandlerBoundToURL('/'),
|
|
|
|
{ allowlist, denylist },
|
|
|
|
))
|
|
|
|
|
|
|
|
self.addEventListener('push', onPush)
|
|
|
|
self.addEventListener('notificationclick', onNotificationClick)
|
2023-01-15 05:58:52 +09:00
|
|
|
self.addEventListener('fetch', onShareTarget)
|