Accessing firebase hosting files from firebase function triggers

Viewed 1752

I have 2 repos one for firebase functions and another for static firebase hosting react site. They both use same firebase project.

myfirebaseproject
--- firebase functions

--- firebase hosting
------index.html

Can I read my firebase-hosting files from firebase-function?

let indexHTML = fs.readFileSync('pathToFirebaseHosting/index.html').toString();
2 Answers

One important thing to realize is that Firebase Hosting and Cloud Functions are completely different products, and there is not a simple local path for Hosting files available in a very temporary and transient Cloud Functions instance. Everything is not bundled up together in one place (that wouldn't scale at all).

Since Hosting files are all public, what Frank says is right - just make an HTTP request for what you need. I suggest using the request-promise module instead, as it gives you a promise-based interface that's easier to work with in Cloud Functions.

Please note that if you want to make an outgoing HTTP request like this, you will have to enable billing on your project. Otherwise, Cloud Functions will not allow the request to happen. Also note that the egress and ingress from both Cloud Functions and Hosting will have additional billing. So if you make this request frequently, expect some costs associated with it.

If you just want some static files available in Cloud Function without having to make an external request, simply copy those files in to your functions folder and deploy them all along with your code. You will be able to read them locally, you won't need to enable billing to try it, and you won't incur egress and ingress charges. It'll also be faster.

While there is a REST API for Firebase Hosting API, it is focused on deploying updates, and does not have a call to access the deployed files.

But since all deployed files are publicly accessible, you can read them from the public internet with a regular HTTP request:

request('https://yourproject.firebaseapp.com/index.html', (err, res, body) => {
  if (err) { return console.log(err); }
  console.log(body);
});
Related