Search code examples
firebase-hostinggoogle-cloud-load-balancer

Google cloud load balancer create backend service for Firebase Hosting to serve microservices & frontend SPA from the same domain via load balancer?


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?


Solution

  • 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.