Drew Hutton
Drew Hutton

Reputation: 203

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.

Upvotes: 12

Views: 3319

Answers (4)

Hlawuleka MAS
Hlawuleka MAS

Reputation: 560

In my case, the cause for this was misspelling function in the firebase.json file, i,e:

 "rewrites": [
      {
        "source": "**",
        "function": "ngssr" // I had spelled this as "functions" (extra s)
      }
    ]

Upvotes: 0

ali harris
ali harris

Reputation: 21

If all the above answers don't help you, the fix that worked for me was:

  • Look in the public directory in your project that firebase hosting uses.
  • Delete the index.html file that firebase created when going through the firebase init steps.

After deleting the generated index.html file, it was able to run my cloud run containers with rewrites.

Upvotes: 2

Safa Alai
Safa Alai

Reputation: 1277

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.

Upvotes: 3

Arwin
Arwin

Reputation: 181

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.

Upvotes: 5

Related Questions