From 8ad40c004ef2bf8924e2362729f42be83c09d102 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 16 Dec 2024 20:15:00 +0800 Subject: [PATCH] feat(settings): clean web app service worker cache Signed-off-by: Innei --- .../modules/settings/tabs/data-control.tsx | 33 +++++++++++++++++-- apps/renderer/src/workers/sw/index.ts | 16 +++++++-- apps/server/helper/meta-map.ts | 3 +- locales/settings/en.json | 1 + 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/apps/renderer/src/modules/settings/tabs/data-control.tsx b/apps/renderer/src/modules/settings/tabs/data-control.tsx index 24aee6ba3..dcabec5d5 100644 --- a/apps/renderer/src/modules/settings/tabs/data-control.tsx +++ b/apps/renderer/src/modules/settings/tabs/data-control.tsx @@ -111,7 +111,7 @@ export const SettingDataControl = () => { value: t("general.cache"), }, isElectronBuild && AppCacheLimit, - isElectronBuild && CleanCache, + isElectronBuild ? CleanElectronCache : CleanCacheStorage, isElectronBuild && { type: "title", value: t("general.data_file.label"), @@ -129,7 +129,36 @@ export const SettingDataControl = () => { ) } -const CleanCache = () => { + +/** + * @description clean web app service worker cache + */ +const CleanCacheStorage = () => { + const { t } = useTranslation("settings") + + return ( + + {t("data_control.clean_cache.button")} + } + action={async () => { + const keys = await caches.keys() + return Promise.all( + keys.map((key) => { + if (key.startsWith("workbox-precache-")) return null + return caches.delete(key) + }), + ) + }} + buttonText={t("data_control.clean_cache.button")} + /> + {t("data_control.clean_cache.description_web")} + + ) +} + +const CleanElectronCache = () => { const { t } = useTranslation("settings") return ( diff --git a/apps/renderer/src/workers/sw/index.ts b/apps/renderer/src/workers/sw/index.ts index d0003b605..9e0503975 100644 --- a/apps/renderer/src/workers/sw/index.ts +++ b/apps/renderer/src/workers/sw/index.ts @@ -1,5 +1,7 @@ /// -import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching" +import { CacheableResponsePlugin } from "workbox-cacheable-response" +import { ExpirationPlugin } from "workbox-expiration" +import { precacheAndRoute } from "workbox-precaching" import { registerRoute } from "workbox-routing" import { CacheFirst } from "workbox-strategies" @@ -19,9 +21,17 @@ registerRoute( ({ request }) => request.destination === "image", new CacheFirst({ cacheName: "image-assets", + plugins: [ + new CacheableResponsePlugin({ + statuses: [0, 200], + }), + new ExpirationPlugin({ + maxEntries: 300, + maxAgeSeconds: 30 * 24 * 60 * 60, + purgeOnQuotaError: true, + }), + ], }), ) precacheAndRoute(precacheManifest) - -cleanupOutdatedCaches() diff --git a/apps/server/helper/meta-map.ts b/apps/server/helper/meta-map.ts index ec6b64b87..0b3f957e6 100644 --- a/apps/server/helper/meta-map.ts +++ b/apps/server/helper/meta-map.ts @@ -21,8 +21,7 @@ async function generateMetaMap() { .replace("client/pages/", "") .replace("/metadata.ts", "") .replaceAll(/\[([^\]]+)\]/g, ":$1") - - .replaceAll(/\(([^)]+)\)\//g, "") + .replaceAll(/\([^)]+\)\//g, "") .replace(/^\./, "") const importName = `i${index}` diff --git a/locales/settings/en.json b/locales/settings/en.json index 1c770ab96..b10f4e61a 100644 --- a/locales/settings/en.json +++ b/locales/settings/en.json @@ -102,6 +102,7 @@ "data_control.app_cache_limit.label": "App Cache Limit", "data_control.clean_cache.button": "Clean Cache", "data_control.clean_cache.description": "Clean the app cache to free up space.", + "data_control.clean_cache.description_web": "Clean the web app service worker cache to free up space.", "feeds.claimTips": "To claim your feeds and receive tips, right-click on the feed in your subscription list and select Claim.", "feeds.noFeeds": "No claimed feeds", "feeds.tableHeaders.name": "Name",