firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Error when enabling the offline persistence for firestore, but only on prod

Open danielo515 opened this issue 3 years ago • 3 comments

[REQUIRED] Describe your environment

  • Operating System version: MacOS 11.5
  • Browser version: Version 103.0.5060.134 (Official Build) (x86_64)
  • Firebase SDK version: 9.6.3
  • Firebase Product: auth, firestore

[REQUIRED] Describe the problem

This error is thrown, but only on the production app, in development it works fine:

FirebaseError: Firestore has already been started and persistence can no longer be enabled.

Steps to reproduce:

In my case, all that is needed is to get firestore database instance, then try to enable offline persistence and that will make it throw this error. However, for some reason this does not happen while in dev mode, when the app is connecting to the local emulators.

Relevant Code:

import {
  getFirestore,
  connectFirestoreEmulator,
  Firestore,
  enableIndexedDbPersistence,
} from 'firebase/firestore';
let db: ReturnType<typeof getFirestore>;

export function getFirestoreDb() {
  //This avoids setting up the emulators more than once
  if (db) return db;
  db = getFirestore();
  // This will use an emulated database when running in dev mode
  // make sure to start it with firebase emulators:start --only firestore
  if (import.meta.env.DEV) connectFirestoreEmulator(db, 'localhost', __FIRESTORE_EMULATOR_PORT__);
  return db;
}

export async function enableOffline(db: Firestore): Promise<{ success: boolean; message: string }> {
  try {
    return await enableIndexedDbPersistence(db)
      .then(() => ({ success: true, message: 'Succesfully enabled offline datababse' }))
      .catch((err) => {
        if (err.code == 'failed-precondition') {
          return {
            success: false,
            message: 'Can not enable offline database, please close other tabs and retry',
          };
        }
        if (err.code == 'unimplemented') {
          return {
            success: false,
            message: 'Can not enable offline mode, not supported by the browser',
          };
        }
        throw err;
      });
  } catch (e) {
    return { success: false, message: 'Unknown error happened while enabling offline database' };
  }
}

Then, in another file where this si called

const App: Component = () => {
  const auth = getAuth();
  const state = useAuth(auth);
  const shouldLogin = () => !state.data || state.error;
  createEffect(async () => {
    if (state.data) {
      const user = state.data;
      const firestore = await import('./getFirestoreDb');
      const { doc, setDoc } = await import('firebase/firestore');
      const db = firestore.getFirestoreDb();
      await firestore.enableOffline(db).then((result) => {
        if (result.success) {
          return infoToast('Database offline is ready', result.message);
        }
        failToast('Can not sue offline database', result.message);
      });
      setDoc(doc(db, `users`, user.uid), {
                   // bla bla bbla
      });
    }
  });

  return (
    <Switch>
      <Match when={state.loading}>
        <LoadingApp />
      </Match>
      <Match when={shouldLogin()}>
        <Login />
      </Match>
      <Match when={state.data}>
        <Main />
      </Match>
    </Switch>
  );
};

danielo515 avatar Jul 29 '22 10:07 danielo515

I was not able to reproduce the described behavior. Can you provide a minimal reproducible example?

MarkDuckworth avatar Jul 29 '22 15:07 MarkDuckworth

Hey @danielo515. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Aug 05 '22 01:08 google-oss-bot

This is a bit hard to reproduce since it requires access to a production firebase environment. So you have any guidelines about how to setup a reproducible example?

El vie., 5 ago. 2022 3:02, Google Open Source Bot @.***> escribió:

Hey @danielo515 https://github.com/danielo515. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

— Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-js-sdk/issues/6489#issuecomment-1205927963, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKJWJOSOWRKXQ5TFJCA7DVXRR3JANCNFSM55AIFLOA . You are receiving this because you were mentioned.Message ID: @.***>

danielo515 avatar Aug 09 '22 18:08 danielo515

You can just provide a repo that contains the code that causes the problem, and link it here, and let us know to run the code in a production environment. From your first post, it just sounds like you mean the app is connecting to a live backend, and not the emulators, as opposed to actually having to deploy the app to hosting, is that correct? Either way, just provide the code and instructions on what environment it needs to be run in to reproduce the error.

hsubox76 avatar Aug 24 '22 17:08 hsubox76

Hey @danielo515. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Aug 31 '22 01:08 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@danielo515 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Sep 07 '22 01:09 google-oss-bot