Skip to content

Commit 334ba7b

Browse files
jamesdanielsdavideast
authored andcommitted
fix(afs): Better handle enablePersistence failures, esp. for Universal (angular#1850)
1 parent 59031cf commit 334ba7b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/firestore/firestore.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { InjectionToken, NgZone, PLATFORM_ID, Injectable, Inject, Optional } fro
22

33
import { Observable, of, from } from 'rxjs';
44
import { catchError } from 'rxjs/operators';
5-
import { firestore } from 'firebase';
5+
import { firestore } from 'firebase/app';
66

77
import { Settings, CollectionReference, DocumentReference, QueryFn, AssociatedReference } from './interfaces';
88
import { AngularFirestoreDocument } from './document/document';
99
import { AngularFirestoreCollection } from './collection/collection';
1010

1111
import { FirebaseFirestore, FirebaseOptions, FirebaseAppConfig, FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
12+
import { isPlatformBrowser } from '@angular/common';
1213

1314
/**
1415
* The value of this token determines whether or not the firestore will have persistance enabled
@@ -119,13 +120,20 @@ export class AngularFirestore {
119120
return firestore;
120121
});
121122

122-
this.persistenceEnabled$ = zone.runOutsideAngular(() =>
123-
shouldEnablePersistence ? from(this.firestore.enablePersistence().then(() => true, () => false))
124-
: of(false)
125-
)
126-
.pipe(
127-
catchError(() => of(false))
128-
); // https://github.com/firebase/firebase-js-sdk/issues/608
123+
if (shouldEnablePersistence && isPlatformBrowser(platformId)) {
124+
// We need to try/catch here because not all enablePersistence() failures are caught
125+
// https://github.com/firebase/firebase-js-sdk/issues/608
126+
const enablePersistence = () => {
127+
try {
128+
return from(this.firestore.enablePersistence().then(() => true, () => false));
129+
} catch(e) {
130+
return of(false);
131+
}
132+
};
133+
this.persistenceEnabled$ = zone.runOutsideAngular(enablePersistence);
134+
} else {
135+
this.persistenceEnabled$ = of(false);
136+
}
129137
}
130138

131139
/**

0 commit comments

Comments
 (0)