feat: SWR cache for Home with stale-while-revalidate
homeDataCache now has three states: - fresh (<5 min): use cache, no revalidation - stale (5-60 min): show cache immediately, revalidate in background - empty (>60 min / genre mismatch / no data): full skeleton load Changes: - Replace isHomeCacheValid() with getHomeCacheStatus() -> fresh/stale/empty - Add sessionStorage persistence for cross-navigation survival - Stop destroying cache data on staleness (was nullifying at 5 min) - HomeView onMount uses three-state logic - loadHome() accepts showSkeletons option for silent revalidation - Extract restoreFromCache() helper for DRY cache restoration
This commit is contained in:
1 parent
1aa23dae9b
commit
4aa32d388b
2 files changed
+138
-82
No files matched your search
@@ -1,9 +1,12 @@
|
||||
/**
|
||||
* Home Data Cache Store
|
||||
* Home Data Cache Store (SWR)
|
||||
*
|
||||
* Module-level in-memory cache for HomeView data.
|
||||
* Prevents re-fetching all 11 sections when navigating back to Home.
|
||||
* Same pattern as searchState.ts — plain module state, no Svelte stores.
|
||||
* Module-level in-memory cache for HomeView data with stale-while-revalidate.
|
||||
* - fresh (< 5 min): Use cache, no revalidation
|
||||
* - stale (5-60 min): Show cache immediately, revalidate in background
|
||||
* - empty (no data / >60 min / genre mismatch): Full skeleton load
|
||||
*
|
||||
* Persisted to sessionStorage for cross-navigation survival.
|
||||
*/
|
||||
|
||||
import type { DisplayTrack, DiscoverPlaylist, DiscoverAlbum, PlaylistTag } from '$lib/types';
|
||||
@@ -51,10 +54,69 @@ export interface HomeCacheData {
|
||||
scrollTop: number;
|
||||
}
|
||||
|
||||
const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||
export type HomeCacheStatus = 'fresh' | 'stale' | 'empty';
|
||||
|
||||
const FRESH_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||
const MAX_TTL_MS = 60 * 60 * 1000; // 60 minutes
|
||||
const SESSION_KEY = 'qbz-home-cache';
|
||||
|
||||
let cache: HomeCacheData | null = null;
|
||||
|
||||
// Restore from sessionStorage on module load
|
||||
function restoreFromSession(): void {
|
||||
try {
|
||||
const stored = sessionStorage.getItem(SESSION_KEY);
|
||||
if (stored) {
|
||||
cache = JSON.parse(stored);
|
||||
}
|
||||
} catch {
|
||||
// sessionStorage not available or corrupted
|
||||
}
|
||||
}
|
||||
|
||||
function persistToSession(): void {
|
||||
if (!cache) {
|
||||
try { sessionStorage.removeItem(SESSION_KEY); } catch {}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
sessionStorage.setItem(SESSION_KEY, JSON.stringify(cache));
|
||||
} catch {
|
||||
// sessionStorage full or not available
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize from sessionStorage
|
||||
restoreFromSession();
|
||||
|
||||
function genreIdsMatch(cachedIds: number[], currentIds: number[]): boolean {
|
||||
const cachedSorted = [...cachedIds].sort((a, b) => a - b);
|
||||
const currentSorted = [...currentIds].sort((a, b) => a - b);
|
||||
if (cachedSorted.length !== currentSorted.length) return false;
|
||||
for (let i = 0; i < cachedSorted.length; i++) {
|
||||
if (cachedSorted[i] !== currentSorted[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function getHomeCacheStatus(currentGenreIds: number[]): HomeCacheStatus {
|
||||
if (!cache) return 'empty';
|
||||
|
||||
// Genre filter mismatch = empty (data is for a different genre)
|
||||
if (!genreIdsMatch(cache.genreIds, currentGenreIds)) return 'empty';
|
||||
|
||||
const age = Date.now() - cache.timestamp;
|
||||
|
||||
// Too old = empty
|
||||
if (age > MAX_TTL_MS) return 'empty';
|
||||
|
||||
// Fresh = use as-is
|
||||
if (age <= FRESH_TTL_MS) return 'fresh';
|
||||
|
||||
// In between = stale (show cache, revalidate)
|
||||
return 'stale';
|
||||
}
|
||||
|
||||
export function getHomeCache(): HomeCacheData | null {
|
||||
return cache;
|
||||
}
|
||||
@@ -65,34 +127,17 @@ export function setHomeCache(data: Omit<HomeCacheData, 'timestamp' | 'scrollTop'
|
||||
timestamp: Date.now(),
|
||||
scrollTop: cache?.scrollTop ?? 0,
|
||||
};
|
||||
persistToSession();
|
||||
}
|
||||
|
||||
export function clearHomeCache(): void {
|
||||
cache = null;
|
||||
}
|
||||
|
||||
export function isHomeCacheValid(currentGenreIds: number[]): boolean {
|
||||
if (!cache) return false;
|
||||
|
||||
// Check TTL
|
||||
if (Date.now() - cache.timestamp > CACHE_TTL_MS) {
|
||||
cache = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check genre filter match
|
||||
const cachedSorted = [...cache.genreIds].sort((a, b) => a - b);
|
||||
const currentSorted = [...currentGenreIds].sort((a, b) => a - b);
|
||||
if (cachedSorted.length !== currentSorted.length) return false;
|
||||
for (let i = 0; i < cachedSorted.length; i++) {
|
||||
if (cachedSorted[i] !== currentSorted[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
persistToSession();
|
||||
}
|
||||
|
||||
export function updateHomeCacheScrollTop(scrollTop: number): void {
|
||||
if (cache) {
|
||||
cache.scrollTop = scrollTop;
|
||||
// Don't persist on every scroll - too frequent
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user