diff --git a/packages/app/package.json b/packages/app/package.json index 9f21a403..ae8a1c1f 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -43,6 +43,7 @@ "use-sync-external-store": "^1.2.0", "uuid": "^9.0.0", "workbox-core": "^6.4.2", + "workbox-expiration": "^7.0.0", "workbox-precaching": "^7.0.0", "workbox-routing": "^6.4.2", "workbox-strategies": "^6.4.2" diff --git a/packages/app/src/service-worker.ts b/packages/app/src/service-worker.ts index 5376c200..53679238 100644 --- a/packages/app/src/service-worker.ts +++ b/packages/app/src/service-worker.ts @@ -7,10 +7,34 @@ import { NostrLink, NostrPrefix, TLVEntryType, encodeTLVEntries, tryParseNostrLi import { formatShort } from "@/Number"; import { defaultAvatar, hexToBech32 } from "@/SnortUtils"; import { clientsClaim } from "workbox-core"; +import { registerRoute } from "workbox-routing"; +import { CacheFirst, StaleWhileRevalidate } from "workbox-strategies"; import { PrecacheEntry, precacheAndRoute } from "workbox-precaching"; +import { ExpirationPlugin } from "workbox-expiration"; precacheAndRoute(self.__WB_MANIFEST); clientsClaim(); +registerRoute( + ({ url }) => url.pathname.endsWith("/.well-known/nostr.json"), + new StaleWhileRevalidate({ + cacheName: "nostr-json-cache", + plugins: [new ExpirationPlugin({ maxAgeSeconds: 4 * 60 * 60 })], + }), +); + +// Cache images from any domain +registerRoute( + // Match any image request regardless of the origin + ({ request }) => request.destination === "image", + new CacheFirst({ + cacheName: "image-cache", + plugins: [ + new ExpirationPlugin({ + maxEntries: 100, + }), + ], + }), +); self.addEventListener("message", event => { if (event.data && event.data.type === "SKIP_WAITING") { diff --git a/yarn.lock b/yarn.lock index 33dd7875..a87e195f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2996,6 +2996,7 @@ __metadata: vite-plugin-version-mark: ^0.0.10 vitest: ^0.34.6 workbox-core: ^6.4.2 + workbox-expiration: ^7.0.0 workbox-precaching: ^7.0.0 workbox-routing: ^6.4.2 workbox-strategies: ^6.4.2 @@ -11583,7 +11584,7 @@ __metadata: languageName: node linkType: hard -"workbox-expiration@npm:7.0.0": +"workbox-expiration@npm:7.0.0, workbox-expiration@npm:^7.0.0": version: 7.0.0 resolution: "workbox-expiration@npm:7.0.0" dependencies: