InvalidStateError while opening IndexedDB in Firefox

I answer because the problem still exists (in Firefox 54). This happens if you:

  • use Firefox in private mode
  • or switch between different Firefox versions (https://bugzilla.mozilla.org/show_bug.cgi?id=1236557, https://bugzilla.mozilla.org/show_bug.cgi?id=1331103)

To prevent the InvalidStateError a try catch isn't working (but useful for other errors, e.g. disabled cookies), instead you need event.preventDefault(). Yes I know, too easy to be true. :)

if (window.indexedDB) {
    var request = window.indexedDB.open('demo', 1);
    request.onsuccess = function(event) {
        // not raised
    };
    request.onupgradeneeded = function(event) {
        // not raised
    };
    request.onerror = function(event) {
        // raised with no InvalidStateError
        if (request.error && request.error.name === 'InvalidStateError') {
            event.preventDefault();
        }
    };
}

Kudos go to https://bugzilla.mozilla.org/show_bug.cgi?id=1331103#c3.