2
votes

I've built a dynamic site using typescript in cloud functions. I also have cloud functions triggers, which whenever data changes in Firestore, the relevant pages recompile themselves to a static page hosted in a firebase cloud storage bucket.

Bucket look something like this:

gs://[bucketname].appspot.com
/app/index.html
/app/page1.html
/app/page2.html
/uploads/images.webp
...

Currently, the static site is served through an express app within a cloud function named app. The problem here lies within the cloud function states. They aren't always hot, which makes the initial load time very slow.

firebase.json

"hosting": {
  "public": "public",
  "rewrites": [{ "source": "**", "function": "app" }],
}

What I would like to achieve, is to have Firebase hosting point to the files from the bucket instead of having an express app within a cloud function. Something between the lines of:

firebase.json

"hosting": {
  "public": "public",
  "rewrites": [
    { "source": "**/:url", "destination": "[BUCKETURL]/app/:url.html" },
    { "source": "**", "destination": "[BUCKETURL]/app/index.html" }]
}

My URL structure is very simple, it's domain.com/page-name and I also want to keep it that way without .html at the end. Any advice would be greatly appreciated. Thank you

2

2 Answers

1
votes

Allowing rewrites from Firebase Hosting to Cloud Storage as you're describing sounds cool, but is currently not possible. I'd recommend filing a feature request.

In the meantime your options are to:

  • Use the Cloud Functions integration into Firebase Hosting as the redirector, as you already seem to do. This indeed may incur occasional cold-start delays.

  • Use another type of Cloud Function (or other server-side process) to generate the static content when needed and push that into the Cloud Storage bucket. In this case, you'd not be using Firebase Hosting anymore, but always serve the static content directly from the Cloud Storage bucket.

    If you're interested in this, check out: Upload single file to firebase hosting via CLI or other without deleting existing ones?

1
votes

A similar approach, it is possible with GCP cloud storage and an HTTP load balancer, but the only inconvenient is that the .html cannot be hidden.

For this apporach, the main steps are:

  • Create a bucket where the objects are publicly accesible
  • Set up a load balancer and SSL certificate.
  • Connect your load balancer to your bucket.
  • Point your domain to your load balancer using an A record.

To create a static website in a bucket please follow this guide

Static sites hosted in a bucket are not dependent on any backend, and your generated on-the-fly files could be configured not to be cached and always serve the latest version available .