1
votes

When it comes to Firestore caching using official web SDK, does it optimizes reads so it "sends" read request to the server only if the document changed after the last read? (so it doesn't generate 1 additional read of the document per attempt)

To elaborate a bit, consider the following scenario:

  1. user opens their profile page on the web app
  2. Firestore makes request to the server to fetch the profile document (1 read against pricing)
  3. user navigates away from the page and after a while returns back to the profile
  4. web app needs access to the profile document again

On the step 4. will Firestore request the document from the server by default even if it wasn't changed? (meaning it will count as 1 additional read against pricing every time user navigates to the profile page)

If yes, is it possible to configure Firestore so it only uses the cached object and only updates it when the object changed on server? How to do that?

1
FYI backticks are meant to delimit bits of code in the question, not identify products or emphasize terms.Doug Stevenson

1 Answers

2
votes

By default, the Firestore SDK disables local persistence of cached data for web clients (but enabled by default for Android and iOS). If you want to enable persistence, follow the instructions in the documentation

firebase.firestore().enablePersistence()
  .catch(function(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
          // ...
      }
  });
// Subsequent queries will use persistence, if it was enabled successfully

You will not be charged a document read if the document is up to date in the local cache.