diff --git a/src/lib/components/views/HomeView.svelte b/src/lib/components/views/HomeView.svelte index 19f605d3a..918a6ddb3 100644 --- a/src/lib/components/views/HomeView.svelte +++ b/src/lib/components/views/HomeView.svelte @@ -6,8 +6,9 @@ getHomeCache, setHomeCache, clearHomeCache, - isHomeCacheValid, - updateHomeCacheScrollTop + getHomeCacheStatus, + updateHomeCacheScrollTop, + type HomeCacheData } from '$lib/stores/homeDataCache'; import { t } from '$lib/i18n'; import HorizontalScrollRow from '../HorizontalScrollRow.svelte'; @@ -309,6 +310,46 @@ let homeViewEl: HTMLDivElement | undefined; + function restoreFromCache(cached: HomeCacheData) { + newReleases = cached.newReleases; + pressAwards = cached.pressAwards; + mostStreamed = cached.mostStreamed; + qobuzissimes = cached.qobuzissimes; + editorPicks = cached.editorPicks; + recentAlbums = cached.recentAlbums; + continueTracks = cached.continueTracks; + topArtists = cached.topArtists; + favoriteAlbums = cached.favoriteAlbums; + qobuzPlaylists = cached.qobuzPlaylists; + essentialDiscography = cached.essentialDiscography; + playlistTags = cached.playlistTags; + + loadingNewReleases = false; + loadingPressAwards = false; + loadingMostStreamed = false; + loadingQobuzissimes = false; + loadingEditorPicks = false; + loadingRecentAlbums = false; + loadingContinueTracks = false; + loadingTopArtists = false; + loadingFavoriteAlbums = false; + loadingQobuzPlaylists = false; + loadingEssentialDiscography = false; + + requestAnimationFrame(() => { + if (homeViewEl && cached.scrollTop > 0) { + homeViewEl.scrollTop = cached.scrollTop; + } + }); + + const allAlbums = [ + ...cached.newReleases, ...cached.pressAwards, ...cached.mostStreamed, + ...cached.qobuzissimes, ...cached.editorPicks, + ...cached.recentAlbums, ...cached.favoriteAlbums + ]; + loadAllAlbumDownloadStatuses(allAlbums); + } + onMount(() => { // Subscribe to home settings changes — invalidate cache on change const unsubscribe = subscribeHomeSettings(() => { @@ -317,52 +358,19 @@ clearHomeCache(); }); - // Try to restore from cache const currentGenreIds = Array.from(getSelectedGenreIds()); - if (isHomeCacheValid(currentGenreIds)) { - const cached = getHomeCache()!; - // Restore all data instantly - newReleases = cached.newReleases; - pressAwards = cached.pressAwards; - mostStreamed = cached.mostStreamed; - qobuzissimes = cached.qobuzissimes; - editorPicks = cached.editorPicks; - recentAlbums = cached.recentAlbums; - continueTracks = cached.continueTracks; - topArtists = cached.topArtists; - favoriteAlbums = cached.favoriteAlbums; - qobuzPlaylists = cached.qobuzPlaylists; - essentialDiscography = cached.essentialDiscography; - playlistTags = cached.playlistTags; + const cacheStatus = getHomeCacheStatus(currentGenreIds); - // Mark all sections as loaded - loadingNewReleases = false; - loadingPressAwards = false; - loadingMostStreamed = false; - loadingQobuzissimes = false; - loadingEditorPicks = false; - loadingRecentAlbums = false; - loadingContinueTracks = false; - loadingTopArtists = false; - loadingFavoriteAlbums = false; - loadingQobuzPlaylists = false; - loadingEssentialDiscography = false; - - // Restore scroll position after DOM renders - requestAnimationFrame(() => { - if (homeViewEl && cached.scrollTop > 0) { - homeViewEl.scrollTop = cached.scrollTop; - } - }); - - // Fire-and-forget: refresh download statuses in background - const allAlbums = [ - ...cached.newReleases, ...cached.pressAwards, ...cached.mostStreamed, - ...cached.qobuzissimes, ...cached.editorPicks, - ...cached.recentAlbums, ...cached.favoriteAlbums - ]; - loadAllAlbumDownloadStatuses(allAlbums); + if (cacheStatus === 'fresh') { + // Fresh: use cache as-is, no revalidation + restoreFromCache(getHomeCache()!); + } else if (cacheStatus === 'stale') { + // Stale: show cache immediately, then revalidate silently in background + restoreFromCache(getHomeCache()!); + // Background revalidation — no skeletons, just update data if changed + loadHome({ showSkeletons: false }); } else { + // Empty: full skeleton load loadHome(); } @@ -743,19 +751,22 @@ } } - async function loadHome() { + async function loadHome(options?: { showSkeletons?: boolean }) { + const showSkeletons = options?.showSkeletons ?? true; error = null; - loadingNewReleases = true; - loadingPressAwards = true; - loadingMostStreamed = true; - loadingQobuzissimes = true; - loadingEditorPicks = true; - loadingRecentAlbums = true; - loadingContinueTracks = true; - loadingTopArtists = true; - loadingFavoriteAlbums = true; - loadingQobuzPlaylists = true; - loadingEssentialDiscography = true; + if (showSkeletons) { + loadingNewReleases = true; + loadingPressAwards = true; + loadingMostStreamed = true; + loadingQobuzissimes = true; + loadingEditorPicks = true; + loadingRecentAlbums = true; + loadingContinueTracks = true; + loadingTopArtists = true; + loadingFavoriteAlbums = true; + loadingQobuzPlaylists = true; + loadingEssentialDiscography = true; + } // Get current genre filter (array of IDs for multi-select) const genreIds = Array.from(getSelectedGenreIds()); diff --git a/src/lib/stores/homeDataCache.ts b/src/lib/stores/homeDataCache.ts index ace0c1628..7110c7275 100644 --- a/src/lib/stores/homeDataCache.ts +++ b/src/lib/stores/homeDataCache.ts @@ -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 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 } }