Tomas Bulva
Tomas Bulva

Reputation: 1220

Use custom domain for firebase function http calls

Is there a way to use a custom domain for firebase cloud functions http hooks.

The default url for cloud functions looks something like this:

https://us-central1-my-awesome-app.cloudfunctions.net/ios-oauth/

And

I would like to make it look like this:

https://myawesomeapp.com/ios-oauth/

I looked around if there was some other people looking for the same solution and sure enough I found this:

https://stackoverflow.com/questions/43482224/firebase-cloud-functions-custom-domain

Upvotes: 44

Views: 24938

Answers (4)

Chen Peleg
Chen Peleg

Reputation: 2034

2024 Update

If anyone is still looking here the rewrites API was changed (as documented here):

"rewrites": [
  {
    "source": "/api/function1",
    "function": {
      "functionId": "function1"
    }
  },
  {
     "source": "!/@(api)/**",
     "destination": "/index.html"
  }
]

I still haven't found a way to reroute All functions to the /api/ as I would like, so at the moment I'm routing them one by one.

As of now (2024) there are 5 regions that support hosting functions:

  • us-west1
  • us-central1
  • us-east1
  • europe-west1
  • asia-east1

Upvotes: 7

Tomas Bulva
Tomas Bulva

Reputation: 1220

I have contacted firebase support to get some answers about this. And I was forwarded to this part in the documentation.

https://firebase.google.com/docs/hosting/functions#create_an_http_function_to_your_hosting_site

You can use your own domain with the firebase-cloud-functions. The way to do it is by using the firebase-hosting.

  1. Connect custom domain to firebase hosting

  2. Add custom function routing to firebase.json

     {
       "hosting": {
         "public": "public",
    
         // Add the following rewrites section *within* "hosting"
         "rewrites": [{
           "source": "/bigben", "function": "bigben"
         }]
    
       }
     }
    
  3. Deploy to firebase

Upvotes: 64

Craig Myles
Craig Myles

Reputation: 5454

The accepted answer is correct, and I created this repository last year to demonstrate the functionality: https://github.com/cjmyles/firebase-react-express

In order to retain the HTML pushState functionality as per https://firebase.google.com/docs/hosting/full-config#rewrites you may want to extend the rules to allow all other requests through to your index.html page, which solves the issue @Boris was facing in his answer.

"rewrites": [
    {
        "source": "/api/**",
        "function": "app"
    },
    {
        "source": "!/@(api)/**",
        "destination": "/index.html"
    }
]

This shouldn't really be needed as the rewrite rules are meant to match the first occurence of a request (so the order matters), but this worked for me by allowing all non-api related requests through.

Please note: At the time of writing this the Firebase documentation states: Firebase Hosting supports Cloud Functions in us-central1 only.

Upvotes: 27

Boris
Boris

Reputation: 586

If anyone else runs into this, Thomas Bulva's answer is correct but for me, I also had to remove the below snippet from the firebase.json file. .

It was redirecting any request to the index.html page. My https://us---<>.cloudfunctions.net URL was working fine; when I did /helloWorld it would take me to "Hello from Firebase!" But if I tried the same from my custom domain, it would fail. Removing this fixed it.

{
   "source": "**",
   "destination": "/index.html"
},

Upvotes: 5

Related Questions