John Smith
John Smith

Reputation: 4353

Firestore for web - on offline write, throw rather than save locally & sync on connection restore

As far as I understand, Firestore, by default, works in such a way that if a write is made, but the client is offline, the write is stored locally, then sent to the server once the connection's restored.

It's an undesired behaviour for me, since:

Therefore, I'd like to completely disable the offline features. If a write directly to the servers could not be completed for whatever reason, I'd like to have an error thrown, or be signaled in any other way, so I can simply inform the user. How do I do that?

My current code:

    firestore
      .collection("someData")
      .doc(props.id)
      .set({ contentState: content }, { merge: true })
      .catch(x => {
        console.log("Error!");
        setSyncError(true);
      });

The catch func never fires, I assume it's because the buffer-offline-writes capability is enabled by default.

Upvotes: 1

Views: 94

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

You mention "Firestore for web" in your question's title.

Note that for the web, offline persistence is disabled by default, as explained in the doc:

When you initialize Cloud Firestore, you can enable or disable offline persistence:

  • For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

  • For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.


For the case when there is no connection between your browser and the Firestore "backend"/service, please see the following Stack Overflow Q/A: How to detect firestore connection failure

Upvotes: 1

Related Questions