Jeff Huijsmans
Jeff Huijsmans

Reputation: 1418

Firebase Functions: route every URL through function and serve from "hosting"

I'm trying to get every url in a subdomain to be routed through a firebase function. This is my configuration:

The functions file:

const functions = require('firebase-functions');

exports.handleWebRequest = functions
  .region('europe-west1')
  .https
  .onRequest((req, res) => {
    res.send('Currently down.');
  });

My firebase.json:

{
  "hosting": {
    "public": "dist",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "handleWebRequest"
      }
    ]
  },
  "functions": {
    "source": "firebase-functions",
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  }
}

When I deploy the hosting and functions, and go to the URL in my browser, I expect to see "Currently down" for every route I open. However, every route except the root route shows the "Currently down" message, and the root route shows the index.html that I deployed.

So, in a nutshell:

/ shows my index.html (which I don't want)
/whatever shows "Currently down." (which is what I want)

The reason I'm trying to route everything through the function, is because I want to shield the website with HTTP Basic Auth, which is "not natively supported" by Firebase.

Upvotes: 0

Views: 393

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317372

Your function is deployed to the "europe-west1" region, but Firebase Hosting currently only supports the default "us-central1". This is stated in the documentation:

Important: Firebase Hosting supports Cloud Functions in us-central1 only.

Upvotes: 3

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

This is most probably because you still have the index.html file under the public folder.

As explained in the doc:

Note: The static files in the public directory take precedence over the rewrites, so any static files will be served alongside the Cloud Functions endpoints.

If you remove the index.html file it should work the way you expect.

Upvotes: 3

Related Questions