From 31d71273963afe3a8d35c019a790be3836d146a7 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Sun, 5 Apr 2026 00:55:15 +0700 Subject: [PATCH] feat: clear expired daily game cache from localStorage on app load --- web/components/game-board.jsx | 2 ++ web/lib/game-engine.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/web/components/game-board.jsx b/web/components/game-board.jsx index b54565c..dcd7962 100644 --- a/web/components/game-board.jsx +++ b/web/components/game-board.jsx @@ -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); }) diff --git a/web/lib/game-engine.js b/web/lib/game-engine.js index 723333d..d6de95f 100644 --- a/web/lib/game-engine.js +++ b/web/lib/game-engine.js @@ -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`;