Search code examples
firebasefirebase-hosting

How to proxy a Firebase Hosting API url to another backend api endpoint using rewrites


I decided to migrate my website to firebase hosting.
Let's say my website was https://example.com/ and this domain was both serving front htmls but also the API routes.

After the migration some parts of the front-end are broken and it makes sense because the API routes aren't working anymore. So what I'd like to do is to use firebase configuration to serve my website's api endpoints, I tried to use this firebase config:

{
    "hosting": {
        "public": "dist",
        "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
        "rewrites": [
            {
                "source": "/api/**",
                "destination": "https://example.com/api/**"
            },
            {
                "source": "**",
                "destination": "/index.html"
            }
        ]
    }
}

But it's not working after new deployment. Firebase keeps returning 404 Not Found for the api routes. The routes are working fine if I try one directly (e.g. https://example.com/api/logs/12 returns correctly).

Is it not possible to write other domains in the rewrites? What am I missing?


Solution

  • What you're trying to do isn't possible. Firebase Hosting can't act as a proxy between itself and some other site. I suggest reviewing the documentation for rewrites. The destination property:

    A local file that must exist

    This URL can be a relative or an absolute path.

    You can't give an arbitrary URL - it must be a file that you deployed to Firebase Hosting.

    If you want to put Firebase Hosting in front of some backend service, your only option is to rewrite a URL to a Cloud Function or Cloud Run container to have it executed and the contents sent to the caller. If you can't use either of these services, Firebase Hosting might not be suitable for your use case.