2023-01-19 11:03:51 +00:00
|
|
|
/* eslint-disable no-restricted-globals */
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
import { clientsClaim } from "workbox-core";
|
|
|
|
import { ExpirationPlugin } from "workbox-expiration";
|
|
|
|
import { precacheAndRoute } from "workbox-precaching";
|
|
|
|
import { registerRoute } from "workbox-routing";
|
|
|
|
import { StaleWhileRevalidate, CacheFirst } from "workbox-strategies";
|
2023-01-19 11:03:51 +00:00
|
|
|
|
|
|
|
clientsClaim();
|
|
|
|
precacheAndRoute(self.__WB_MANIFEST);
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
const staticTypes = ["image", "video", "audio"];
|
|
|
|
registerRoute(
|
2023-02-09 12:26:54 +00:00
|
|
|
({ request, url }) => url.origin === self.location.origin && staticTypes.includes(request.destination),
|
2023-01-19 11:03:51 +00:00
|
|
|
new StaleWhileRevalidate({
|
2023-02-07 20:04:50 +00:00
|
|
|
cacheName: "static-content",
|
|
|
|
plugins: [new ExpirationPlugin({ maxEntries: 50 })],
|
2023-01-19 11:03:51 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2023-01-19 11:46:52 +00:00
|
|
|
// External media domains which have unique urls (never changing content) and can be cached forever
|
2023-02-09 12:26:54 +00:00
|
|
|
const externalMediaHosts = ["void.cat", "nostr.build", "imgur.com", "i.imgur.com", "pbs.twimg.com", "i.ibb.co"];
|
2023-02-07 20:04:50 +00:00
|
|
|
registerRoute(
|
|
|
|
({ url }) => externalMediaHosts.includes(url.host),
|
2023-01-19 11:46:52 +00:00
|
|
|
new CacheFirst({
|
2023-02-07 20:04:50 +00:00
|
|
|
cacheName: "ext-content-hosts",
|
|
|
|
})
|
|
|
|
);
|
2023-01-19 11:46:52 +00:00
|
|
|
|
2023-02-09 12:26:54 +00:00
|
|
|
self.addEventListener("message", event => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (event.data && event.data.type === "SKIP_WAITING") {
|
2023-01-19 11:03:51 +00:00
|
|
|
self.skipWaiting();
|
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
});
|