Skip to content

ReferenceError: "Can't find variable: indexedDB" in restricted/private browser environments #2779

Description

@uriva

Description

In certain browser environments, the InstantDB client SDK throws a fatal ReferenceError: Can't find variable: indexedDB (or ReferenceError: indexedDB is not defined) during storage initialization, which completely crashes the client application.

Cause

In IndexedDBStorage.ts (specifically in @instantdb/core), the global indexedDB variable is referenced directly as a free variable:

const request = indexedDB.open(name);

and:

const request = indexedDB.open(this.dbName, 1);

In some browsers or restricted contexts (such as Safari private browsing, certain iOS embedded webviews, or environments with storage/cookies completely disabled), the indexedDB global variable is not defined on the global scope.

In JavaScript, accessing an undefined free variable throws a fatal ReferenceError, stopping code execution immediately.

Stack Trace

Here is the stack trace captured from production client-side error reporting:

@https://topicmatch.xyz/assets/index-DcJ47QhO.js:1:76970
Promise@[native code]
_init@https://topicmatch.xyz/assets/index-DcJ47QhO.js:1:76935
K0@https://topicmatch.xyz/assets/index-DcJ47QhO.js:1:76906
_initStorage@https://topicmatch.xyz/assets/index-DcJ47QhO.js:1:123427
L2@https://topicmatch.xyz/assets/index-DcJ47QhO.js:1:121721
fb@https://topicmatch.xyz/assets/index-DcJ47QhO.js:7:9342
oa@https://topicmatch.xyz/assets/index-DcJ47QhO.js:7:26894
Pf@
o6@https://topicmatch.xyz/assets/index-DcJ47QhO.js:16:6459
module code@https://topicmatch.xyz/assets/index-DcJ47QhO.js:196:102060

And the error message is:
ReferenceError: Can't find variable: indexedDB (or ReferenceError: indexedDB is not defined in Chrome).

Proposed Solution

Rather than referencing the free variable indexedDB directly, it should be accessed safely via globalThis or window, with a check for its existence:

const idb = typeof globalThis !== "undefined" ? globalThis.indexedDB : undefined;

If idb is undefined, the SDK could gracefully fall back to an in-memory/no-op storage provider, preventing the entire client application from crashing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions