orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 134 forks source link

"supportsIndexedDB" is malfunctioning in some cases #990

Open xulww opened 1 year ago

xulww commented 1 year ago

While using Orbit, I encountered an issue with the "supportsIndexedDB" function, which seems to be malfunctioning.

Upon testing in Chrome's incognito mode, the function correctly returns true, indicating support for indexedDB. However, when testing Firefox's private browsing mode, the function also returns true, which is incorrect. In reality, Firefox's private browsing mode does not allow the use of indexedDB, resulting in error messages such as error opening indexedDB *db-name* and DOMException: A mutation operation was attempted on a database that did not allow mutations.

To clarify, Firefox explicitly prohibits mutations to indexedDB while in private browsing mode. As a result, the "supportsIndexedDB" function should accurately reflect this behavior, returning false in such scenarios.

In my opinion, a more effective approach to handle checks for indexedDB support would involve something similar to this code:

function getIsIndexedDBSupported() {
    return new Promise((resolve) => {
        if (!window.indexedDB) {
            resolve(false);
            return;
        }

        try {
            const dbName = "testDB";
            const request = window.indexedDB.open(dbName, 1);

            request.onerror = () => {
                resolve(false);
            };

            request.onsuccess = (event) => {
                const db = event.target.result;

                db.close();

                window.indexedDB.deleteDatabase(dbName);

                resolve(true);
            };
        } catch (error) {
            resolve(false);
        }
    });
};