9
votes

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.

2
I have a Cloud Run redirect on ** that is working just fine. Please edit the question to state what is actually happening that's not expected (be specific, don't just say "it doesn't work"). Also try running the Firebase Hosting emulator with firebase serve --only hosting and look at the output. Rewrites to Cloud run should appear there.Doug Stevenson
Hi Doug, thanks for the reply, sorry about not being too specific. 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. I'm unsure.Drew Hutton
Hi Drew, I have made the same example of helloworld, and testing with firebase serve --only hosting I see that the trigger is launched: [hosting] Cloud Run rewrite {"source": "**", "run": {"serviceId ":" helloworld "," region ":" us-central1 "}} triggered. However in the browser only Can not GET / helloworld appears instead of --Hello World! -Darry Morales

2 Answers

4
votes

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.

1
votes

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.