🤖 The AI Prompt That Debugs IndexedDB Version Clashes
Opening an IndexedDB database with a version number lower than (or equal to, with schema changes pending) what’s already stored triggers a VersionError that most apps surface only as a vague “failed to open database” message, because the actual conflict detail lives in the rejected request’s error object rather than anywhere obviously logged. This happens constantly in multi-tab apps, where Tab A upgrades the schema to version 4 mid-session while Tab B, still running older cached JavaScript, tries to open version 3 — or during a deploy where a new service worker ships a schema bump before every open tab has reloaded to pick up the matching client code, leaving some tabs permanently stuck unable to open their own local database.
🔎 The Problem
Paste this prompt along with the IndexedDB open/upgrade code and the exact error you're seeing in the console: "Here is my IndexedDB open logic and the error I'm hitting: [paste code + error, e.g. 'VersionError: The requested version (3) is less than the existing version (4)']. Walk through exactly which tab, service worker, or code path most likely already upgraded this database to the higher version, explain why onupgradeneeded isn't firing the way I expected, and give me a safe open() pattern that detects a version mismatch, prompts the user to reload instead of silently failing, and won't corrupt data if two tabs try to upgrade at the same time." Attach: the exact open() call, any onupgradeneeded/onblocked handlers, and whether a service worker in this app also touches this database.
✅ Fix: Detect the Mismatch and Coordinate the Upgrade Across Tabs
- Add an
onversionchangehandler on every open connection that closes the connection and prompts the user to reload when another tab starts an upgrade, instead of leaving the old tab holding a connection that blocks the new tab’sonupgradeneededfrom ever firing. - Read the current version off
indexedDB.databases()(or a stored version marker) before deciding whether to open with a specific version number at all — opening without specifying a version lets the browser use whatever version already exists, sidestepping a hardcoded number that’s gone stale in cached JS. - Surface the actual
event.target.error.nameandmessagein whatever error UI the app shows, rather than a generic “couldn’t load your data” — a VersionError is a completely different problem from a quota error or a corrupted database, and the fix for each is different.
🚨 Why This Is Dangerous
- A tab stuck in a VersionError loop after a deploy looks to the user like the app has silently lost all their locally stored data, when the data is completely intact — it’s just unreachable from that specific tab’s stale connection until it reloads.
IndexedDB doesn’t merge two different ideas of what version a database should be — it picks whichever tab got there first and leaves every other open connection to find out the hard way.




