Skip to content

Commit 7569e33

Browse files
committed
feat: request persistent storage to prevent browser eviction of cached AI models
1 parent 569fd8b commit 7569e33

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Persistent Storage for Cached AI Models
2+
3+
## Summary
4+
5+
Added `navigator.storage.persist()` request when downloading local AI models, preventing the browser from silently evicting cached model files under storage pressure.
6+
7+
## Changes
8+
9+
### Modified: `js/ai-assistant.js`
10+
- Made consent download handler `async`
11+
- Added `navigator.storage.persist()` call before `initAiWorker()`
12+
- Wrapped in try/catch — non-critical, download proceeds regardless
13+
- Logs result to console (`💾 Persistent storage granted` / `⚠️ ... not granted`)
14+
15+
## Impact
16+
17+
- **Chrome**: Auto-grants persistent storage for frequently-visited sites and installed PWAs
18+
- **Firefox**: May show a one-time user prompt
19+
- **Safari**: Supports the API; behavior varies
20+
- Prevents browsers from garbage-collecting cached models (80 MB – 2.7 GB) when disk space is low
21+
22+
| File | Changes |
23+
|------|---------|
24+
| `js/ai-assistant.js` | +11 −1 |

js/ai-assistant.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,23 @@
587587
});
588588
}
589589

590-
aiConsentDownload.addEventListener('click', () => {
590+
aiConsentDownload.addEventListener('click', async () => {
591591
const targetModel = _consentTargetModel || currentAiModel;
592592
aiConsentDownload.disabled = true;
593593
aiConsentDownload.innerHTML = '<span class="ai-status-spinner"></span> Loading...';
594594
aiProgressSection.style.display = 'block';
595595
localStorage.setItem(M.KEYS.AI_CONSENTED_PREFIX + targetModel, 'true');
596596
if (targetModel === 'qwen-local') localStorage.setItem(M.KEYS.AI_CONSENTED, 'true');
597+
598+
// Request persistent storage so the browser won't auto-evict cached models
599+
// under storage pressure. Chrome auto-grants for frequent sites; Firefox prompts.
600+
try {
601+
if (navigator.storage && navigator.storage.persist) {
602+
const granted = await navigator.storage.persist();
603+
console.log(granted ? '💾 Persistent storage granted — cached models are safe' : '⚠️ Persistent storage not granted — models may be evicted');
604+
}
605+
} catch (e) { /* non-critical */ }
606+
597607
initAiWorker(targetModel);
598608
});
599609

0 commit comments

Comments
 (0)