Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Hosting rewrite doesn't redirect to Google Cloud Run

I am setting up a redirect(rewrite) with my firebase hosting so that I can call an api that is running from google cloud run here.

I have tried changing the rewrite string from "/api/**" (should catch all things to page.com/api/** and send that to the function). deleted the index.html and swapped to "**" to capture ALL paths including index. Nothing has worked so far.

My hosting firebase.json is setup like so, is there something wrong with this?

{
  "hosting": {
    "public": "dist/public",
    "ignore": ["firebase.json", "**.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "run": {
          "serviceId": "next-js-base-api",
          "region": "us-central1"
        }
      }
    ]
  }
}

I also tried with normal redirects to another page, this does not work, what determines when the firebase.json settings begin to propagate and work?

Update

I tried running the hosting emulator and with a modified rewrite "source": "/api/**" which had the following results. Navigating to /api returns non crash (doesn't redirect) with output in browser of cannot GET /api navigating to api/wkapi (a sub directory that is caught by the api endpoint) returns an unexpected error in the browser and

Error: Unable to find a matching rewriter for {"source":"/api/**","run":{"serviceId":"next-js-base-api","region":"us-central1"}}

in the console.

like image 807
Drew Hutton Avatar asked Apr 20 '19 13:04

Drew Hutton


People also ask

Does Firebase Hosting work with Google Cloud?

Today we're excited to announce Firebase Hosting integration for Google Cloud’s new Cloud Run service. Cloud Run is a fully managed compute platform that enables developers to run stateless containers that are invocable via HTTP requests in a language and framework of their choosing.

How to rewrite a Firebase Cloud function link?

To rewrite your Cloud Function link go to the /firebase.json file. In the rewrites array add this object before the existing one: Run firebase deploy --only hosting to send changes to the server.

What is iCloud run in Firebase?

Cloud Run is a fully managed compute platform that enables developers to run stateless containers that are invocable via HTTP requests in a language and framework of their choosing. Firebase Hosting integration lets you use this architecture as a backend for a web app or microservice in your Firebase project.

How do I enable cloud run API in Firebase?

Enable the Cloud Run API in the Google APIs console: Open the Cloud Run API pagein the Google APIs console. When prompted, select your Firebase project. Click Enableon the Cloud Run API page. Install and initializethe Cloud SDK.


2 Answers

Make sure to update to the latest version of your Firebase CLI by running:

npm install -g firebase-tools@latest

This will enable you to rewrite to cloud run instances as you are trying to do.

like image 116
Arwin Avatar answered Sep 19 '22 15:09

Arwin


Actually, I ran this just now and, looking at the logs of the deployed cloud-run helloworld container, found that custom-domain/helloworld is actually mapping onto container-domain/helloworld instead of simply mapping to container-domain/. To fix this, I had to add an additional app.get rule to my original Node.js program:

app.get('/helloworld', (req, res) => {

And then calling custom-domain/helloworld worked.

like image 27
Safa Alai Avatar answered Sep 19 '22 15:09

Safa Alai