41

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

Brettski
  • 19,351
  • 15
  • 74
  • 97
Tomas Bulva
  • 1,120
  • 1
  • 9
  • 17
  • Oct 1 2020, Firebase 7.22.0 was released with "Cloud Functions for Firebase Client SDK: Users can now set a custom domain for callable functions." – xaphod Oct 03 '20 at 04:30
  • If you are using Express.js with multiple routes, look at https://stackoverflow.com/questions/44959652/firebase-hosting-with-dynamic-cloud-functions-rewrites – secretshardul Jan 24 '21 at 07:36

3 Answers3

57

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

Antonio
  • 11,413
  • 6
  • 34
  • 48
Tomas Bulva
  • 1,120
  • 1
  • 9
  • 17
  • 4
    Would it be possible to create a dynamic route so you didn't have to define each function? Something like `/api/***` where `***` would be the function name? Hope I'm not breaking a rule here, just don't see a reason to create an entire question for this. – Mitch Evans Jun 15 '19 at 01:04
  • 3
    How is this the solution? this is only supported in us-central1. – SebastianG Jul 23 '20 at 12:45
  • 9
    [It is January 2021, this solution still works only for `us-central1`](https://firebase.google.com/docs/hosting/functions#create_an_http_function_to_your_hosting_site) – secretshardul Jan 24 '21 at 05:42
  • 1
    And it only works for GET requests. If you try a POST, the body is lost... – jeanmatthieud Apr 08 '21 at 08:21
26

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.

Craig Myles
  • 5,206
  • 3
  • 40
  • 37
  • Nice one! Thanks for sharing! – crixx Dec 14 '19 at 22:02
  • This is great. Might be exactly what I needed. Thank you very much! – Iggy Feb 12 '20 at 23:49
  • 1
    Do you have any idea how to configure that the `firebase.functions.httpsCallable('myfn')` call using FB JS SDK automatically uses the custom domain instead of the default `https://us-central1-myapp.cloudfunctions.net/`? – sceee Jun 18 '20 at 06:35
  • Does the function itself need to be in us-central1 or the hosting? I can't seem to tell based on the documentation. – SebastianG Jul 23 '20 at 12:41
  • 2
    @SebastianG No matter where your hosting is located, if the hosting rewrites to a function, then that function MUST be in us-central1. – DarkNeuron Nov 26 '20 at 13:42
4

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"
},
Boris
  • 566
  • 5
  • 21