4

Our application is currently focus around one single domain, let's call it: mydomain.com

Currently, we have separate docker containers via cloud run serving microservices from nested path, such as:

mydomain.com/auth -> auth microservice cloud run container mydomain.com/files -> storage bucket mydomain.com/public -> public microservice cloud run container.

The issue I'm having is trying to run a static SPA published on firebase from the root of the domain.

mydomain.com -> firebase hosting.

However, there doesn't seem to be any option for this in the spec and load-balancing is listed as N/A in the documentation, which makes sense as they're static files served from a CDN.

Is there a way to achieve this via firebase?

SebastianG
  • 8,563
  • 8
  • 47
  • 111
  • Did you find something ? – Dako Junior Aug 15 '21 at 09:58
  • 1
    Yes, I have found that simply hosting the SPA from a traditional google storage bucket works just as well and easy as the firebase hosting option. Firebase does give you the CDN and domain mapping out of the box, but if you are using a load balancer you don't need that anyway. – SebastianG Aug 15 '21 at 21:51
  • How did you archive SPA rewrite to `index.html` with Google's LB? Did you rewrite in Firebase instead? – gavenkoa Oct 19 '22 at 19:28
  • 1
    @gavenkoa I didn't use firebase in the end as it didn't make sense in that context, a simple Google Storage bucket does the exact same thing (because it is the exact same thing under the hood). with that you have this single one-line command to modify your 404 and index.html to whatever you want: `gsutil web set -m index.html -e 404.html gs://my-static-assets` as documented here: https://cloud.google.com/storage/docs/hosting-static-website#storage-create-bucket-gcloud – SebastianG Oct 19 '22 at 23:00

1 Answers1

2

Google LB rewrite engine is limited to only stripping fixed prefixes. For SPA app we need stripping variable suffixes instead. Fortunately bucket HTTP hosting engine allows rewrites of unknown URLs to error page:

gsutil web set -m index.html -e 404.html gs://web-stage/

If we replace 404.html with index.html we obtain classical SPA URL mapping:

gsutil web set -m index.html -e index.html gs://web-stage/

with only one downside: non-root virtual URLs are returned with HTTP code 404. It is not an error and should be ignored.

Thanks to @SebastianG! I saw 404 trick earlier but hesitated to implement it: Deploy SPA application on Google Cloud Storage using Load Balancer and CDN

Overall steps are:

gcloud alpha storage buckets create --location=europe-west1 gs://web-stage
gsutil iam ch allUsers:objectViewer gs://web-stage/
gsutil web set -m index.html -e index.html gs://web-stage/
npm install
npm run build
gsutil -m rsync -r build/ gs://web-stage/

plus you register your bucket as a default route in LB so unknown URLs trigger 404 handler in bucket's HTTP web server.

gavenkoa
  • 45,285
  • 19
  • 251
  • 303