firebase firestore offline persistence FirebaseError

I searched for several hours to implement firestore offline persistence because its crucial for my app to work. I handle multiple chats inside my app and its a fatal error to download data every time again.

This is my code sample, its the root App.js file which is the first one beeing called:

....
//imports related to firebase
import * as firebase from "firebase";
import ApiKeys from "./constants/ApiKeys";


export default function App() {
  //firebase project initialisation
  if (!firebase.apps.length) {
    firebase.initializeApp(ApiKeys);
    var db = firebase.firestore();
    db.settings({ experimentalForceLongPolling: true });
    console.log("DB INITIALISED");
    db.enablePersistence() //HERE IS THE PROBLEM
        .catch((err) => {
            if (err.code == 'failed-precondition') {
                // Multiple tabs open, persistence can only be enabled
                // in one tab at a a time.
                // ...
            } else if (err.code == 'unimplemented') {
                // The current browser does not support all of the
                // features required to enable persistence
                // ...
            }
      })
  };.....

The API Keys look like this:

const firebaseConfig = {
    apiKey: "***",
    authDomain: "***",
    databaseURL: "https://***",
    projectId: "***",
    storageBucket: "***",
    messagingSenderId: "***",
    appId: "***",
    measurementId: "***"
  };

  export default firebaseConfig;

I receive the following error which I can not work around:

FirebaseError: You are using the memory-only build of Firestore. Persistence support is only available via the @firebase/firestore bundle or the firebase-firestore.js build.

I have installed the complete "firebase" bundle and also access to @firebase/firestore, I checked this already, and I implemented it same as in the official docs. Everything works but when I want to enable the persistence... It crashes with the error mentioned above.

What can I do now? Do I miss another package, have I done something wrong?

As a sidenote, authentification persistence works perfectly inside my app, just the firestore persistence is failing.

Currently using firebase SDK 8.2.3

Upvotes: 2

Views: 1489

Answers (1)

Greg Fenton
Greg Fenton

Reputation: 2808

I have developed my app with Expo and was pointed to expo-firestore-offline-persistence. I notice on their page they talk about react-native-firebase for enabling persistence on RN - though that seems to require ejecting from Expo.

It seems this is an issue with polyfill on the RN platform. In other words, the Javascript that Firestore requires doesn't have everything necessary on the React Native platform - something that common browsers will have. So the "fix" is to leverage a component like react-native-firebase or expo-firestore-offline-persistence that give you that extra capability on native.

Upvotes: 2

Related Questions