Search code examples
firebasecachinggoogle-cloud-functionsgoogle-cloud-firestorefirebase-hosting

Can Firebase Hosting Serve Cached Data from Cloud Functions?


Let's say I have a database of 100,000 pieces of content inside of firestore. Each piece of content is unlikely to change more than once per month. My single page app, using firebase hosting, uses a function to retrieve the content from firestore, render it to HTML, and return it to the browser.

It's a waste of my firestore quotas and starts to add up to a lot of money if I'm routinely going through this process for content that is not that dynamic.

How can that piece of content be saved as a static .com/path/path/contentpage.html file to be served whenever that exact path and query are requested, rather than going through the firestore / functions process every time?

My goal is to improve speed, and reduce unnecessary firestore requests, knowing each read costs money.

Thanks!


Solution

  • When you use Firebase Hosting on top of Cloud Functions for Firebase, Hosting can act as an edge-cached layer on top of the responses from your HTTPS functions. You can read about that integration in the documentation. In particular, read the section managing cache behavior:

    The main tool you'll use to manage cache is the Cache-Control header. By setting it, you can communicate both to the browser and the CDN how long your content should be cached. In your function, you set Cache-Control like so:

    res.set('Cache-Control', 'public, max-age=300, s-maxage=600');