diff --git a/src/shared/utils/firebase.tsx b/src/shared/utils/firebase.tsx index 457ad1515..812edea8a 100644 --- a/src/shared/utils/firebase.tsx +++ b/src/shared/utils/firebase.tsx @@ -1,8 +1,15 @@ +import { getAuth, connectAuthEmulator } from "firebase/auth"; import firebase from "firebase/compat/app"; import "firebase/compat/auth"; import "firebase/compat/firestore"; import "firebase/compat/performance"; import "firebase/compat/storage"; +import { + initializeFirestore, + connectFirestoreEmulator, + persistentLocalCache, + persistentSingleTabManager, +} from "firebase/firestore"; import { local } from "@/config"; import { Environment, REACT_APP_ENV } from "@/shared/constants"; import config from "../../config"; @@ -11,31 +18,28 @@ interface FirebaseError extends Error { code: string; } -firebase.initializeApp(config.firebase); -firebase.firestore().settings({ +const firebaseApp = firebase.initializeApp(config.firebase); +const firestore = initializeFirestore(firebaseApp, { experimentalForceLongPolling: true, + localCache: persistentLocalCache( + /*settings*/ { + tabManager: persistentSingleTabManager({ + forceOwnership: true, + }), + }, + ), }); if (REACT_APP_ENV === Environment.Local) { - firebase.auth().useEmulator(local.firebase.authDomain); - firebase - .firestore() - .useEmulator( - "localhost", - Number(local.firebase.databaseURL.split(/:/g)[2]), - ); -} else { - firebase - .firestore() - .enablePersistence({ - synchronizeTabs: true, - experimentalForceOwningTab: false, - }) - .catch((error) => { - console.error("Error enabling persistence", error); - }); -} + const auth = getAuth(); + connectAuthEmulator(auth, local.firebase.authDomain); + connectFirestoreEmulator( + firestore, + "localhost", + Number(local.firebase.databaseURL.split(/:/g)[2]), + ); +} // firebase.firestore.setLogLevel("debug"); export const isFirebaseError = (error: any): error is FirebaseError =>