Skip to content

Commit dc50232

Browse files
author
Michael Lehenbauer
authored
Gracefully fall back to memory persistence for Firefox Private Browsing. (firebase#1801)
Fixes firebase#801.
1 parent 5f5f1c1 commit dc50232

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ import { ViewSnapshot } from './view_snapshot';
6565

6666
const LOG_TAG = 'FirestoreClient';
6767

68-
/** The DOMException code for an aborted operation. */
68+
/** DOMException error code constants. */
69+
const DOM_EXCEPTION_INVALID_STATE = 11;
6970
const DOM_EXCEPTION_ABORTED = 20;
70-
71-
/** The DOMException code for quota exceeded. */
7271
const DOM_EXCEPTION_QUOTA_EXCEEDED = 22;
7372

7473
export class IndexedDbPersistenceSettings {
@@ -255,8 +254,8 @@ export class FirestoreClient {
255254
}
256255

257256
console.warn(
258-
'Error enabling offline storage. Falling back to' +
259-
' storage disabled: ' +
257+
'Error enabling offline persistence. Falling back to' +
258+
' persistence disabled: ' +
260259
error
261260
);
262261
return this.startMemoryPersistence();
@@ -285,15 +284,22 @@ export class FirestoreClient {
285284
typeof DOMException !== 'undefined' &&
286285
error instanceof DOMException
287286
) {
288-
// We fall back to memory persistence if we cannot write the primary
289-
// lease. This can happen can during a schema migration, or if we run out
290-
// of quota when we try to write the primary lease.
291-
// For both the `QuotaExceededError` and the `AbortError`, it is safe to
292-
// fall back to memory persistence since all modifications to IndexedDb
293-
// failed to commit.
287+
// There are a few known circumstances where we can open IndexedDb but
288+
// trying to read/write will fail (e.g. quota exceeded). For
289+
// well-understood cases, we attempt to detect these and then gracefully
290+
// fall back to memory persistence.
291+
// NOTE: Rather than continue to add to this list, we could decide to
292+
// always fall back, with the risk that we might accidentally hide errors
293+
// representing actual SDK bugs.
294294
return (
295+
// When the browser is out of quota we could get either quota exceeded
296+
// or an aborted error depending on whether the error happened during
297+
// schema migration.
295298
error.code === DOM_EXCEPTION_QUOTA_EXCEEDED ||
296-
error.code === DOM_EXCEPTION_ABORTED
299+
error.code === DOM_EXCEPTION_ABORTED ||
300+
// Firefox Private Browsing mode disables IndexedDb and returns
301+
// INVALID_STATE for any usage.
302+
error.code === DOM_EXCEPTION_INVALID_STATE
297303
);
298304
}
299305

0 commit comments

Comments
 (0)