11

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.

Community
  • 1
  • 1
Drew Hutton
  • 183
  • 1
  • 9
  • 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 Apr 20 '19 at 15:52
  • 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 Apr 21 '19 at 03:09
  • 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 Apr 24 '19 at 23:05

4 Answers4

5

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.

Arwin
  • 181
  • 8
  • To think that upgrading the firebase tools didn't cross my mind when troubleshooting. The full answer was a combination of this and rewriting paths that look like capture both /api/** and /api/. – Drew Hutton May 06 '19 at 03:11
  • @DrewHutton can you please elaborate "rewriting paths that look like capture both /api/** and /api/." Did you have to add two rewrite statements .. any chance you can just show what your rewrite statements ended up looking like. (I'm having the same problem) – Safa Alai Sep 14 '19 at 23:52
  • 3
    I got it working by: 1) setting source to "**" 2) removing index.html from the public directory – user5269602 Dec 19 '19 at 19:19
3

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.

Safa Alai
  • 1,223
  • 1
  • 15
  • 27
  • 1
    This was the thing that resolved the issue for me. Hosting an angular app in Firebase, but with a microservice in cloudRun. If the rewrites source is /api, the cloudRun must have an /api route to get. – ASomN Sep 01 '20 at 20:54
  • @ASomN can you please show your firebase.json with rewrites? I have the same case angular app with /api in cloud run. When I open my url domain e.g. domain.com/api it loads cloud run, when I open angular app e.g. domain.com/agnet angular app load. However when I open domain.com/api again it doesn't work load cloud run but angular app. – aponski Dec 08 '21 at 09:20
  • "rewrites": [ { "source": "/api/**", "run": { "serviceId": "api", "region": "europe-west1" } }, { "source": "**", "destination": "/index.html" } ] – ASomN Dec 31 '21 at 10:10
1

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.

ali harris
  • 11
  • 1
0

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)
      }
    ]
Hlawuleka MAS
  • 550
  • 5
  • 11