diff --git a/docs/content/en/docs/Other engines/web.md b/docs/content/en/docs/Other engines/web.md index 0371a313..b7ac3fc4 100644 --- a/docs/content/en/docs/Other engines/web.md +++ b/docs/content/en/docs/Other engines/web.md @@ -90,7 +90,7 @@ performant, since we don't have to encode binary blobs as strings. To use this implementation on browsers that support it, replace `WebDatabase(name)` with: ```dart -WebDatabase.withStorage(MoorWebStorage.indexedDbIfSupported(name)) +WebDatabase.withStorage(await MoorWebStorage.indexedDbIfSupported(name)) ``` Moor will automatically migrate data from local storage to `IndexeDb` when it is available. \ No newline at end of file diff --git a/moor/lib/src/web/storage.dart b/moor/lib/src/web/storage.dart index f4f8b642..7d976e20 100644 --- a/moor/lib/src/web/storage.dart +++ b/moor/lib/src/web/storage.dart @@ -47,22 +47,30 @@ abstract class MoorWebStorage { /// Uses [MoorWebStorage.indexedDb] if the current browser supports it. /// Otherwise, falls back to the local storage based implementation. - factory MoorWebStorage.indexedDbIfSupported(String name, - {bool inWebWorker = false}) { - return supportsIndexedDb(inWebWorker: inWebWorker) + static Future indexedDbIfSupported(String name, + {bool inWebWorker = false}) async { + return await supportsIndexedDb(inWebWorker: inWebWorker) ? MoorWebStorage.indexedDb(name, inWebWorker: inWebWorker) : MoorWebStorage(name); } /// Attempts to check whether the current browser supports the /// [MoorWebStorage.indexedDb] storage implementation. - static bool supportsIndexedDb({bool inWebWorker = false}) { + static Future supportsIndexedDb({bool inWebWorker = false}) async { var isIndexedDbSupported = false; if (inWebWorker && WorkerGlobalScope.instance.indexedDB != null) { isIndexedDbSupported = true; } else { try { isIndexedDbSupported = IdbFactory.supported; + + if (isIndexedDbSupported) { + // Try opening a mock database to check if IndexedDB is really + // available. This avoids the problem with Firefox incorrectly + // reporting IndexedDB as supported in private mode. + final mockDb = await window.indexedDB!.open('moor_mock_db'); + mockDb.close(); + } } catch (error) { isIndexedDbSupported = false; }