Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save firestore requests by using Http cache-control?

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 ?

like image 737
John doe Avatar asked Jan 29 '23 11:01

John doe


1 Answers

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.

like image 137
Gil Gilbert Avatar answered Feb 13 '23 06:02

Gil Gilbert