Storage Quotas & Persistence Policies
Storage Quotas & Persistence Policies in Modern JavaScript
When web applications store hundreds of megabytes of media, offline databases, and application assets via IndexedDB and the Cache API, browsers enforce storage management policies. Understanding Storage Quotas, eviction algorithms, and requesting Persistent Storage ensures your app's offline data is never unexpectedly purged by the browser.
1. Storage Quotas: Best-Effort vs. Persistent
By default, all web storage (IndexedDB, Cache API, LocalStorage) operates in Best-Effort mode:
- Best-Effort Storage: If device disk space runs critically low, the browser's storage manager automatically evicts (deletes) entire origin storage trees without asking the user, using an LRU (Least Recently Used) policy!
- Persistent Storage: Data is guaranteed not to be deleted by automatic browser clearing. It can only be cleared if the user explicitly clears browser data.
2. Inspecting Storage Quota with navigator.storage.estimate()
To monitor available space and consumption in bytes:
3. Requesting Persistent Storage: navigator.storage.persist()
To transition your origin from Best-Effort to Persistent storage, request persistence:
Browser Granting Heuristics:
Browsers (Chrome, Firefox, Safari) may grant persistent storage automatically without a prompt if:
- The site is installed as a PWA (added to home screen).
- The user has bookmarked the site.
- The user has high engagement (frequent visits).
- Push notification permissions have been granted.
4. Handling Storage Overflows: QuotaExceededError
When attempting to store data beyond the allowed quota, operations throw a QuotaExceededError (DOMException code 22). Always catch this error defensively:
Practice Quiz
Custom Events & Event Dispatching
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Cache API & Service Worker Storage | Custom Events & Event Dispatching |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.