question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firestore offline data access not working

See original GitHub issue

Following the guides for using reactfire, I have tried enable offline access to data https://github.com/FirebaseExtended/reactfire/blob/main/docs/use.md#access-data-offline .

However, this is not working, and I get next exception:

Unhandled Rejection (FirebaseError): Firestore has already been started and persistence can no longer be enabled. You can only enable persistence before calling any other methods on a Firestore object.
....

The line causing this issue is here:

await enableIndexedDbPersistence(db);

Removing previous line and the problem is solved, however, by doing so the offline access is bypassed.

Version info

Latest versions for react, firebase, reactfire.

Test case

In order to reproduce this issue, I have created 2 GitHub repositories

  1. https://github.com/constantin-ungureanu-github/test

    • simple react app of the Burito quickstart example, where I also tried to use the offline data access.
  2. https://github.com/constantin-ungureanu-github/test-pwa-typescript

    • simple react app where I copied the examples from reactfire source code (non-concurrent ones), and the result is the same, enableIndexedDbPersistence produces the same exception.

Steps to reproduce

Simply run the react examples described above. npm install npm start

Expected behaviour

Guide for data offline access to work as described.

Actual behaviour

Enabling offline access is not working, exception is thrown.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jerryeechancommented, Dec 24, 2021

Connect to emulator before enable Persistence works

const { status, data: firestore } = useInitFirestore(async (firebaseApp) => {
    const db = initializeFirestore(firebaseApp, {});
    connectFirestoreEmulator(db, "localhost", 8080);
    await enableIndexedDbPersistence(db);
    return db;
  });
0reactions
bruceharrison1984commented, Dec 13, 2022

I got this working with the following:

// utils/firestore.ts
export const getInitializedFirestore = () => {
  const firestore = getFirestore();

  // only enable emulator if in dev mode, and we haven't already enabled it
  if (
    process.env.NODE_ENV === 'development' &&
    (firestore as any)._getSettings().host === 'firestore.googleapis.com'
  ) {
    connectFirestoreEmulator(getFirestore(), '127.0.0.1', 8080);
    console.log('firestore emulator attached');
  }

  // only enable indexeddb if the firestore client hasn't been fully initialized
  if (!(firestore as any)._firestoreClient) {
    enableMultiTabIndexedDbPersistence(firestore);
    console.log('persistence enabled');
  }
  return firestore;
};

Replace any calls to getFirestore() with getInitializedFirestore(). This is also HMR and page refresh safe, so dev mode is pleasant, and it can safely be deployed without having to touch anything.

I don’t know why the Firebase SDK doesn’t make these properties visible, but this lets you check the status without keeping track of global variables.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access data offline | Firestore - Firebase
Listen to offline data; Get offline data; Query offline data; Configure offline ... Cloud Firestore's cache isn't automatically cleared between sessions.
Read more >
Enabling offline data | Firestore - Google Cloud
Firestore supports offline data persistence. This feature caches a copy of the Firestore data that your app is actively using, so your app...
Read more >
Firestore Database Offline Mode Broken - Stack Overflow
We are having serious problems with Firestore Database offline mode in our app, and I am hoping we are just making a stupid...
Read more >
Cloud Firestore | FlutterFire
To learn more about reading data whilst offline, view the Access Data Offline ... Cloud Firestore does not support the following types of...
Read more >
A Few Gotchas To Consider When Working With Firestore's ...
How does Firestore's offline persistence work? ... the user has not interacted with any query or document, the data won't still be available...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found