1

I have a project with multiple firebase hosting projects set up (for staging and production), and when I add rewrites per the Firebase docs here the rewrite for function /bigben does not work at https://customdomain.com/bigben (both deployed to production and in the emulators)

firebase.json

{
  "hosting": [
    {
      "target": "production",
      "public": "build",
      "rewrites": [
        {
          "source": "/bigben",
          "function": "bigben"
        },
        {
          "source": "**",
          "destination": "/index.html"
        },
        {
          "source": "!/@(js|css|svg|jpg|png|gif|ico|json|html)/**",
          "destination": "/index.html"
        }
      ]
      // ...
    },
    {
      "target": "staging",
      "public": "build",
      "rewrites": [
        {
          "source": "/bigben",
          "function": "bigben"
        },
        {
          "source": "**",
          "destination": "/index.html"
        },
        {
          "source": "!/@(js|css|svg|jpg|png|gif|ico|json|html)/**",
          "destination": "/index.html"
        }
      ]
      // ...
    }
  ]
}

How can I set /bigben to use this custom domain (that is already configured in Firebase for hosting/the frontend create-react-app? I've seen removing index.html from the build folder, but isn't that needed for CRA?

1 Answer 1

2

I know the docs say your source should look like that, a simple path, but it never seems to work for me either. Change your source for bigben from:

"source": "/bigben",

to this:

"source": "/bigben/**",

After that it should do the rewrite correctly.

Edit:

Also your rewrites are out of order and so all of your source files are being rewritten to index.html. Here are my recommended changes:

{
  "hosting": [
    {
      "target": "production",
      "public": "build",
      "rewrites": [
        {
          "source": "/bigben/**",
          "function": "bigben"
        },
        {
          "source": "!**/*.*",
          "destination": "/index.html"
        }
      ]
      // ...
    },
    {
      "target": "staging",
      "public": "build",
      "rewrites": [
        {
          "source": "/bigben/**",
          "function": "bigben"
        },
        {
          "source": "!**/*.*",
          "destination": "/index.html"
        }
      ]
      // ...
    }
  ]
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you! Still getting the You need to enable JavaScript to run this app. (index.html of public: build) after doing this, both after redeploying on production and restarting emulators
@user2521295 Updated my answer
Thank you! Will try this-- by out of order-- I thought the docs said to add the function rewrites before { source: "**", destination: index.html }? What order should they be in?
The problem is your third rewrite after your wildcard. Your second rewrite statement says to rewrite anything that's not /bigben to index.html. It never hits your third rewrite statement, so the visitor can't download your JS/CSS assets (I believe, could be wrong).
Ahh got it, thank you! Will review the CSS/JS assets rewrite-- it hasn't been a problem so far (as in people have been able to access the site with all correct styling/scripts) but have not tried downloading directly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.