Search code examples
firebasegoogle-cloud-functionsfirebase-hosting

Firebase Hosting rewrites to Cloud Functions


I want to rewrites all URL end with "api/(funcName)" to call cloud function (funcName).

In firebase.json I set rewrites rules as follows.

"rewrites": [
  {
    "source": "api/:funcName",
    "function": ":funcName"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
] 

but it's not working.

I got

Error: Forbidden

Your client does not have permission to get URL /:funcName/api/(funcName) from this server.

(funcName) is the real function name I don't want to show here.


Solution

  • Your rewrite should include the exact name of the function. The rewrite system doesn't support named wildcard routes like you use in Express. If you want to wildcard all URLs with a prefix, use the glob syntax supported by Firebase Hosting as described in the documentation.

      {
        "source": "api/**",
        "function": "funcName"
      },
    

    Where "funcName" is the name of your function as exported by your code.