feat(settings): clean web app service worker cache

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2024-12-16 20:15:13 +08:00
parent 5ac203f6ee
commit 8ad40c004e
4 changed files with 46 additions and 7 deletions
@@ -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 = () => {
</div>
)
}
const CleanCache = () => {
/**
* @description clean web app service worker cache
*/
const CleanCacheStorage = () => {
const { t } = useTranslation("settings")
return (
<SettingItemGroup>
<SettingActionItem
label={
<span className="flex items-center gap-1">{t("data_control.clean_cache.button")}</span>
}
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")}
/>
<SettingDescription>{t("data_control.clean_cache.description_web")}</SettingDescription>
</SettingItemGroup>
)
}
const CleanElectronCache = () => {
const { t } = useTranslation("settings")
return (
+13 -3
View File
@@ -1,5 +1,7 @@
/// <reference lib="webworker" />
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()
+1 -2
View File
@@ -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}`
+1
View File
@@ -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",