Matt Kaufman
Matt Kaufman

Reputation: 808

Pointing a subdomain to Firebase functions

I've got a website hosted on firebase www.example.com. I'd like to serve some functions in the same project from api.example.com.
Rewrites seem to only support directories and not subdomains though. Has anyone been able to accomplish this? Do I have to just use two different firebase projects?

Upvotes: 4

Views: 1937

Answers (2)

Daniel Eisenhardt
Daniel Eisenhardt

Reputation: 593

You can create multiple sites on the firebase console: Go to your project > hosting.

  1. Create a new site on a subdomain of the domain you own i.e. api.example.com; (Confirm ownership for the domain etc.)

  2. In your firebase.json update your hosting property to look something like this:

"hosting": [
  {
    "target": "mywebsite",
    "public": "website",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  {
    "target": "mywebsite-api",
    "public": "apisite",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/hello-world",
        "function": "helloWorld",
        "region": "firebase-region3" // This is optional if you do not use regions for your functions
      }
    ]
  }
],
  1. Put index.html and 404.html files in the directory i.e. apisite to avoid errors. If you want to publish documentation of your API publicly, this could be a good place to put it.

  2. Deploy with:

firebase deploy --only hosting

now your function should be available on your subdomain i.e. https://api.example.com/hello-world

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317487

Rewrites won't handle domain names directly. As you say, you can use a second project, and route calls to your API via your custom domain using Firebase Hosting integration with Cloud Functions.

Upvotes: 3

Related Questions