Skip to content

Commit

Permalink
feat(settings): clean web app service worker cache
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Dec 16, 2024
1 parent 5ac203f commit 8ad40c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
33 changes: 31 additions & 2 deletions apps/renderer/src/modules/settings/tabs/data-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 (
Expand Down
16 changes: 13 additions & 3 deletions apps/renderer/src/workers/sw/index.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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()
3 changes: 1 addition & 2 deletions apps/server/helper/meta-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
1 change: 1 addition & 0 deletions locales/settings/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8ad40c0

Please sign in to comment.