Safari ITP deleted our IndexedDB drafts after 7 days without a visit
Weekly users on Safari kept losing offline drafts stored in IndexedDB. Tracking prevention caps script-writable storage at seven days of browser use without interaction. We moved the source of truth to the server and treated local data as a cache.
Our field inspection app lets inspectors write reports offline on iPads and laptops. Drafts live in IndexedDB until the device is online, then sync. Most inspectors use it daily. A smaller group, mostly regional managers, open it once a week or less to review and add notes.
That group started reporting that unsent drafts had vanished. No error, no crash. They opened the app, and the draft list was empty. Our sync logs showed the drafts had never reached the server. Every affected device was running Safari or a home screen shortcut that, it turned out, had been opened as a regular Safari tab.
Reproducing it
The direct reproduction takes a week, which is part of why it hid for so long:
Open the app in Safari on macOS or iOS, create a draft while offline, close the tab.
Use Safari normally on other sites for at least seven days, without opening our app.
Open the app again. The IndexedDB database is gone, along with localStorage and the service worker registration.
To confirm it faster we checked Safari's Develop menu, Storage tab, on a test Mac before and after the window passed, and kept a small "storage birth date" record in IndexedDB that would be missing if the database had been recreated.
What we ruled out
Storage pressure. The devices had plenty of free space, and storage-pressure eviction would also hit daily users. It did not.
Our own migration code. A schema upgrade could drop object stores. We checked the
onupgradeneededhandler and our version history; no release in the window touched stores.Private browsing. Private windows lose everything at close, but affected users were in normal windows.
Clearing history. We asked. Nobody had cleared website data manually.
Root cause
WebKit's tracking prevention puts a seven-day cap on all script-writable storage for a site when the user has not interacted with that site during seven days of Safari use. IndexedDB, localStorage, sessionStorage, the Cache API and service worker registrations are all included. Our weekly managers crossed the seven-day line often enough to lose drafts that had not yet synced. Daily users never did, because each visit with interaction resets the clock.
What is happening under the hood
WebKit introduced the cap in 2020, when it blocked third-party cookies by default. Trackers had started moving identifiers from cookies into first-party script storage, so WebKit treats any data that JavaScript can write as expiring unless the user keeps engaging with the site.
The rule has details that matter for app design:
The count is days of Safari use, not calendar days. A user who does not open Safari for a week does not lose data; a user who browses daily but never visits you does.
User interaction with your site as a first party resets the timer. A visit where the user taps or types counts.
The cap applies to script-writable storage. Cookies set by your server with
Set-Cookiefollow different rules.Web apps added to the Home Screen run outside Safari and are not subject to this deletion in the same way, because the user is using the app itself. A bookmark that opens in a Safari tab is not a home screen web app, and several of our managers had exactly that.
Safari 17 also introduced new storage policies, including a larger per-origin quota and support for navigator.storage.persist(). Persistence protects data from eviction when the device is low on space. We did not find anything in WebKit's documentation that promises persistence overrides the tracking-prevention cap, so we do not rely on it for that.
Environment
Component | Version / value |
|---|---|
Safari (macOS) | 26.1 on macOS Tahoe 26.1 |
Safari (iPadOS) | iPadOS 26.1 |
App | React 19 PWA, Workbox 7 service worker |
Local storage | IndexedDB via the |
Sync | REST |
Minimal reproduction
The code had no bug in the usual sense. The design assumed local storage was durable:
// Draft saved locally, synced "later". The only copy lives in IndexedDB until then.
export async function saveDraft(draft: Draft) {
const db = await openDB('inspections', 3);
await db.put('drafts', { ...draft, synced: false, updatedAt: Date.now() });
scheduleSync(); // runs when online; may never run if the user closes the tab offline
}
Who is affected
Every Safari and every iOS browser (all use WebKit) for sites opened in a normal tab.
Apps used less often than weekly, or with long offline stretches.
Any design where IndexedDB or localStorage is the only copy of user data.
Chrome and Firefox do not have this seven-day rule, although they evict under storage pressure.
The fix
We made the server the source of truth and turned local storage into a cache that can disappear.
Sync on every save when online, not on a timer. A draft is "safe" only after the server acknowledges it.
Show sync state in the UI. Unsynced drafts get a visible badge and a warning before the user leaves.
Detect a wiped store and re-hydrate from the server instead of showing an empty list.
Ask the user to install the app to the Home Screen on iPad, where it runs as a web app.
export async function saveDraft(draft: Draft) {
const db = await openDB('inspections', 4);
- await db.put('drafts', { ...draft, synced: false, updatedAt: Date.now() });
- scheduleSync();
+ const record = { ...draft, synced: false, updatedAt: Date.now() };
+ await db.put('drafts', record);
+ if (navigator.onLine) {
+ try {
+ await pushDraft(record); // server is the source of truth
+ await db.put('drafts', { ...record, synced: true });
+ } catch {
+ scheduleSync(); // retry, badge stays visible
+ }
+ }
+ notifySyncState();
}
Detecting a wiped database uses a marker written on first run plus the server's view:
export async function ensureLocalState(userId: string) {
const db = await openDB('inspections', 4);
const meta = await db.get('meta', 'install');
if (!meta) {
// Fresh or wiped store: rebuild from the server, then record when we did it.
const drafts = await fetchServerDrafts(userId);
const tx = db.transaction('drafts', 'readwrite');
await Promise.all(drafts.map((d) => tx.store.put({ ...d, synced: true })));
await tx.done;
await db.put('meta', { id: 'install', createdAt: Date.now() });
reportEvent('local_store_rebuilt');
}
}
We also request persistence. It does not solve the seven-day rule, but it reduces eviction under storage pressure, and the result is useful telemetry:
if (navigator.storage?.persist) {
const granted = await navigator.storage.persist();
reportEvent('storage_persist', { granted });
}
Alternatives and trade-offs
Home Screen install only. Requiring install avoids the cap but adds friction and does not help desktop Safari users.
Server-set cookies for small state. Fine for a session token, useless for megabytes of drafts with photos.
Native wrapper. A thin native app with a web view stores data in the app container. More work, separate release process.
How to test the fix
Unit tests for
ensureLocalStatewith an empty fake IndexedDB (fake-indexeddb) confirm re-hydration.In Safari, Develop menu, delete the site's storage manually and reload: the draft list must come back from the server and
local_store_rebuiltmust fire.Go offline, create a draft, close the tab: the next open must show the unsynced badge, and syncing must clear it.
npx vitest run src/storage/ensureLocalState.test.ts
Confirming in production
Two weeks after release we compared server drafts with client events:
local_store_rebuilt{browser="Safari"} ~35/week (expected: the cap still fires)
draft_lost_reports (support tickets) 0 (was 6-10/week)
unsynced_draft_age_p95 3 min (was 2.1 days)
The cap still clears storage, as designed. Users no longer notice because the server has the data.
Regression guard
A code review rule: no feature may store user-created data only in IndexedDB or localStorage.
An end-to-end test wipes storage between two steps and asserts the UI recovers.
An alert fires if
unsynced_draft_age_p95goes over 30 minutes.
Security implications
Moving drafts to the server means inspection notes, which can contain addresses and photos, now sit in our database sooner. We confirmed they are encrypted at rest, access-controlled per organisation and covered by our retention policy. Re-hydration fetches drafts over the existing authenticated API, so a wiped store never leaks another user's data. Do not try to dodge the cap by fingerprinting or by bouncing users through redirects: that is the tracking behaviour the cap exists to stop.
Prevention checklist
Treat browser storage as a cache that can vanish; keep the source of truth on the server.
Sync on save while online and show unsynced state clearly.
Detect a missing store and rebuild it from the server.
Offer Home Screen install for iOS users who need offline work.
Test with storage cleared between visits.
References
WebKit blog, Full Third-Party Cookie Blocking and More: https://webkit.org/blog/10218/full-third-party-cookie-blocking-and-more/
WebKit, Tracking Prevention in WebKit: https://webkit.org/tracking-prevention/
WebKit blog, Updates to Storage Policy: https://webkit.org/blog/14403/updates-to-storage-policy/
MDN, Storage quotas and eviction criteria: https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria
MDN, StorageManager.persist(): https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/persist
Add a label
Embed this post
<iframe src="https://hibug.me/safari-itp-deletes-indexeddb-after-7-days" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I build and operate event-driven systems on AWS: Lambda, Step Functions, SQS, SNS, EventBridge and ECS on Fargate, mostly for e-commerce and logistics workloads where a stuck message means a stuck order. Serverless removes a lot of work, but it also hides limits until the day you hit them: payload sizes, trigger timeouts, scale-in behaviour, resource policies that silently drop events. I write down each one we hit, with the exact error, the configuration that caused it and the change that fixed it, so you can find the limit in a blog post instead of an incident channel.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.