feat: clear expired daily game cache from localStorage on app load

This commit is contained in:
tiennm99 committed 2026-04-05 00:55:15 +07:00
1 parent e502d43a9c
commit 31d7127396
2 files changed
+19

No files matched your search

+2
View File
@@ -14,6 +14,7 @@ import {
clearUnlimitedState,
saveUnlimitedStats,
loadUnlimitedStats,
clearExpiredCache,
} from "@/lib/game-engine";
import { compareChampions } from "@/lib/classic-mode";
import ChampionSearch from "./champion-search";
@@ -50,6 +51,7 @@ export default function GameBoard() {
useEffect(() => {
loadChampions()
.then(() => {
clearExpiredCache(getTodaySeed());
initGame("daily");
setLoading(false);
})
+17
View File
@@ -173,6 +173,23 @@ function loadState(mode, seed) {
}
}
/** Remove stale daily entries from localStorage (keeps only today's) */
export function clearExpiredCache(todaySeed) {
try {
const todayKey = `${STORAGE_KEY_PREFIX}daily_${todaySeed}`;
const keysToRemove = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && key.startsWith(`${STORAGE_KEY_PREFIX}daily_`) && key !== todayKey) {
keysToRemove.push(key);
}
}
keysToRemove.forEach((key) => localStorage.removeItem(key));
} catch {
// Ignore
}
}
function getStorageKey(mode, seed) {
if (mode === "daily") return `${STORAGE_KEY_PREFIX}daily_${seed}`;
return `${STORAGE_KEY_PREFIX}unlimited_current`;