4

Let's say i have a web/mobile app running firebase firestore database.

My app is set to serve mostly dynamic content fully stored in firestore.

So we are talking about caching dynamic content

If a user load Page A, it's will make 1 request to firestore (for example). If this user go to Page B and then go back to Page A 5 minutes later, i don't want the app to make another request if the content has not changed.

I heard about http cache-control headers but my concern is. If the cache control go check firestore to learn if the content is still the same, will this operation be counted as a request by firestore ?

John doe
  • 3,680
  • 7
  • 31
  • 65

1 Answers1

8

Firestore does not make requests in a way that HTTP Cache-Control headers would apply.

However, the protocol includes a notion of a resume token which allows entire queries to be resumed at a later time, possibly avoiding the retransmission of any unchanged documents that match the query.

If you enable persistence in your web app, Firestore will cache the documents and resume tokens in a local IndexedDB for you. If you come back to a page later, it will use the resume token transparently and avoid retransmission.

Note a pricing caveat:

If the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.

So, to specifically address your question. If:

  • you have persistence enabled,
  • the underlying data hasn't changed,
  • a user loads page A, and
  • loads page A again 5 minutes later

Then Firestore will request the data for the page but the server will essentially respond that nothing has changed: no documents will be transferred and there will be no charge to confirm that.

Gil Gilbert
  • 7,722
  • 3
  • 24
  • 25
  • Thank you. That's what i wanted to know. – John doe Oct 05 '17 at 08:28
  • Gil do we have to register no-op listeners for onSnapshot to cache data in memory like with Firebase RTDB? https://stackoverflow.com/questions/38423277/does-firebase-cache-the-data#answer-38423694 – cdock Oct 08 '17 at 05:40
  • ^Just to confirm I tested adding a persistent listener, and it dropped response times from ~300ms to ~5ms. Wondering if `enablePersistence()` should handle this for us...? – cdock Oct 08 '17 at 08:04
  • Adding a listener indicates interest in a value, so yes if you absolutely want to pin something in RAM but don't care to do anything with it you can create a no-op listener to accomplish that. With persistence enabled you should be able to create listeners on demand and get responses from the local cache even without such things. Ask a separate, specific question and I'll go into more detail. – Gil Gilbert Oct 09 '17 at 16:53
  • This comment: https://stackoverflow.com/questions/46649022/firestore-serve-cached-datas-first#comment80251067_46649022 clarified persistence behavior for me. I was wondering why I wasn't seeing cache-first data, but I was using `get()` instead of `onSnapshot()`. It's not that I didn't want to do anything w/ data just wanted subsequent requests to be fast. – cdock Oct 09 '17 at 18:58