Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting/preventing Firefox IndexedDB InvalidStateError caused by private browsing

I'm integrating a javascript library that uses IndexedDB, but it fails "ungraciously" when Firefox is in private browsing/window mode. The library returns out a 500 Internal Error and meanwhile Firefox spits out an InvalidStateError to console.

What I'd like to do is add a check before I instantiate this library, and not use the library at all if IndexedDB is not available. i.e. some type of try/catch test. From what I've seen, Firefox appears to spit out the console error even if the offending code is inside a try/catch (but maybe there's still a way..).

I don't actually have an interest whether the user is in a private window session or not, but that seems to be the only time when Firefox causes this InvalidStateError.

like image 613
rgareth Avatar asked Oct 31 '22 00:10

rgareth


1 Answers

You have handle the errors in the onerror function.

This won't tell you for definite that the use is "In Private", but will tell you you can't use indexedDB - from which you could interpolate if you needed to - ie if its FireFox and this throws and error then chances are they are In Private - until the guys at Mozilla fix it.

var db = window.indexedDB.open('test');
db.onerror = function()
{
    console.log("Can't use indexedDB")
}

This still kicks out the InvalidStateError to the console, but the js can handle the consequences.

like image 117
Morvael Avatar answered Nov 04 '22 07:11

Morvael