Redirect Firebase Root Hosting for Cloud Feature Not Working

I am using Firebase Hosting with a file firebase.json

that should redirect all traffic to a cloud function (prerender) that fills in the meta and og tags for SEO.

{
  "hosting": {
      "public": "dist/prod",
      "rewrites": [
       {
          "source": "**",
          "function": "prerender"
       }
      ]
   }
}

      

My function prerender

processes the request and renders the HTML file. This works great:

export const prerender = functions.https.onRequest((req, res) => {
   console.log('prerender function: "' + req.path + '"');
   ...
}

      

When reaching the endpoint at https://xxx.cloudfunctions.net/prerender

, I correctly receive a call in Dashboard Firebase under Functions -> Logs:

prerender function: "null"

      

However, when called, https://mypage.firebaseapp.com

I don't get any logs and it seems to display index.html

inside my folder dist/prod

.

Is there something I am missing when rewriting? I tried to rewrite /

to the same function but didn't have time. Any hints are greatly appreciated!

+3


source to share


1 answer


You should be able to route all urls to the function exactly as you are showing. I am assuming you still have an index.html file located in your dist / prod directory. In my test project, I just renamed the root index.html to something else, and requests to it /

were redirected to my function.

It turns out that if there is static web content that matches the client request url, that will be used instead of delegating the function. This is true for any incoming URL. The only way to make sure all requests are redirected to your function is to remove all content from your folder dist/prod

before deploying.



I believe the key piece of information is in the documentation for rewrites :

The overwrite rule applies only if the file or folder does not exist at the specified source, and returns the actual file content to the destination instead of an HTTP redirect.

+7


source







All Articles