To detect stale data in IndexedDB during synchronization with a remote server, the most common and reliable technique is to use
timestamps or versioning metadata on each record. Here’s how to handle it effectively:
1. Add a lastModified or version Field per Record
Every record stored in IndexedDB and on the server should include one of:
lastModified: A UTC timestamp (e.g., ISO 8601 or Unix Epoch).
version: A monotonically increasing number (e.g., int or
UUID/ETag).
Fetch latest server records (with their lastModified or version).
Compare with the corresponding IndexedDB record.
If the local version is older, update from the server.
If the local version is newer, push to server.
If both changed, trigger conflict resolution.
Example (timestamp comparison):
if (local.lastModified < server.lastModified) {
// Update local IndexedDB with server data
} else if (local.lastModified > server.lastModified) {
// Push local changes to server
} else {
// Data is in sync – no action needed
}
// Schema
db.tasks = new Dexie.Table("tasks", "++id, title, lastModified");
// Update with server data if newer
if (new Date(serverTask.lastModified) > new Date(localTask.lastModified)) {
await db.tasks.put(serverTask);
}
Summary
Field
Purpose
Format
lastModified
Detect stale updates
ISO Date or Unix Timestamp
version
Optional alternative to timestamps
Integer or UUID
syncStatus
Optional (e.g., pending, synced, conflict)
String
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To detect stale data in IndexedDB during synchronization with a remote server, the most common and reliable technique is to use timestamps or versioning metadata on each record. Here’s how to handle it effectively:
1. Add a
lastModifiedorversionField per RecordEvery record stored in IndexedDB and on the server should include one of:
lastModified: A UTC timestamp (e.g., ISO 8601 or Unix Epoch).version: A monotonically increasing number (e.g.,intorUUID/ETag).Example Record:
2. Compare Timestamps during Sync
During sync:
lastModifiedorversion).Example (timestamp comparison):
3. Batch Versioning for Sync Tokens
If you want to track updates more efficiently:
lastModified > lastSyncTimestamp.lastSyncTimestamp.4. Handle Clock Skew (if using timestamps)
5. Optional: Use Hashing for Change Detection
For large documents:
Dexie.js Example
Summary
lastModifiedversionsyncStatuspending,synced,conflict)