diff --git a/app/documents/page.tsx b/app/documents/page.tsx index 3f286c8..2f14e12 100644 --- a/app/documents/page.tsx +++ b/app/documents/page.tsx @@ -42,6 +42,7 @@ export default function Documents() { } setCachedFiles(cached); if (cached.size === docs.length) setSyncStatus('done'); + else setSyncStatus('idle'); } catch (error) { console.error("Cache check failed", error); } @@ -57,6 +58,12 @@ export default function Documents() { checkCacheStatus(); + const handleVisibility = () => { + if (document.visibilityState === 'visible') checkCacheStatus(); + }; + document.addEventListener('visibilitychange', handleVisibility); + window.addEventListener('focus', checkCacheStatus); + const conn = (navigator as any).connection; if (conn && (conn.type === 'wifi' || conn.type === 'ethernet') && !conn.saveData) { handleSyncAll(); @@ -65,6 +72,8 @@ export default function Documents() { return () => { window.removeEventListener('online', handleStatus); window.removeEventListener('offline', handleStatus); + document.removeEventListener('visibilitychange', handleVisibility); + window.removeEventListener('focus', checkCacheStatus); }; }, []); @@ -73,25 +82,24 @@ export default function Documents() { const updatedCache = new Set(cachedFiles); for (const doc of docs) { - try { - const response = await fetch(doc.file); - if (response.ok) { - updatedCache.add(doc.file); - setCachedFiles(new Set(updatedCache)); + if (!updatedCache.has(doc.file)) { + try { + const response = await fetch(doc.file); + if (response.ok) { + await response.blob(); + updatedCache.add(doc.file); + setCachedFiles(new Set(updatedCache)); + } + } catch (error) { + console.error("Kunde inte ladda ner:", doc.file); } - } catch (error) { - console.error("Kunde inte ladda ner:", doc.file); } } - setTimeout(async () => { - await checkCacheStatus(); - setSyncStatus('done'); - setShowNotification(true); - setTimeout(() => { - setShowNotification(false); - }, 4000); - }, 500); + await checkCacheStatus(); + setSyncStatus('done'); + setShowNotification(true); + setTimeout(() => setShowNotification(false), 4000); }; if (!isHydrated) return
;