Which storage to use
| localStorage | IndexedDB | |
|---|---|---|
| Best for | Settings, scores, small lists | Many records, files, images |
| API | Synchronous key/value strings | Asynchronous database |
| Practical size | Around 5 MB | Much larger (a share of free disk) |
| Difficulty | One line | Use a small wrapper library |
A safe localStorage pattern
const KEY = 'myapp:v1';
function load() {
try { return JSON.parse(localStorage.getItem(KEY)) || {}; }
catch (e) { return {}; } // corrupted or blocked storage
}
function save(state) {
try { localStorage.setItem(KEY, JSON.stringify(state)); }
catch (e) { alert('Storage is full'); }
}
Prefix keys with your app name and a version (myapp:v1) so you can migrate data later without clashes.
How long the data lasts
- Survives: closing the app, restarting the phone, installing an update with the same package name and signing key.
- Deleted: uninstalling, "Clear storage" in app settings, or installing a build with a different package name (it's a different app).
Give users a backup
// Export
const blob = new Blob([JSON.stringify(load())], { type: 'application/json' });
const a = Object.assign(document.createElement('a'), {
href: URL.createObjectURL(blob), download: 'backup.json' });
a.click();
For import, use <input type="file" accept=".json"> and FileReader. Whether downloads and file pickers work depends on the app shell, so test both after building. Copying the JSON to the clipboard is a reliable fallback.
When you need sync
If users expect their data on several phones, you need a backend (a database service with sign-in). At that point a URL app pointing to your web app is usually easier than a bundled one.
Using IndexedDB without the boilerplate
IndexedDB's raw API is verbose. A small promise wrapper (a few hundred bytes) gives you get, set and del like localStorage, but asynchronous and able to store objects, arrays and binary data. Download the library file into your project rather than loading it from a CDN, so the app keeps working offline.
Changing your data format later
const state = load();
if (!state.version) { // data saved by version 1 of the app
state.items = state.list || [];
delete state.list;
state.version = 2;
save(state);
}
Store a version number with your data and upgrade old shapes on launch. Users who skip several app updates still end up with data your new code understands.
What not to store
- Passwords and API secrets — anything in the app's files or storage can be extracted from the phone.
- Huge media files — they make backups slow and fill the user's storage; stream them instead.
- Data you can't afford to lose without an export or a server copy.
The habit tracker, event schedule and ebook reader blueprints all use the safe localStorage pattern shown above.
Questions
Does localStorage work inside an Android APK?
Yes. WebView apps support localStorage and IndexedDB, and the data persists between launches.
Is the data kept when I update the app?
Yes, if the update uses the same package name and signing key.
How much can I store in localStorage?
Plan for about 5 MB. Use IndexedDB for anything larger.