Firebase CLI: "Configure as a single-page app (rewrite all urls to /index.html)"

FirebaseFirebase HostingFirebase Tools

Firebase Problem Overview


I just used the Firebase CLI to init a static hosting project. What exactly happens when you enable the "configure as a single-page app" option? I'm looking for a description of exactly which files are modified, and what kind of effect this has on the Firebase backend.

Screenshot of firebase init command

Firebase Solutions


Solution 1 - Firebase

That option simply sets a flag in the firebase.json file to redirect all URLs to /index.html.

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

See the documentation of Firebase Hosting for more information, which also contains this fuller example:

> > "hosting": { > // ... > > // Add the "rewrites" attribute within "hosting" > "rewrites": [ { > // Serves index.html for requests to files or directories that do not exist > "source": "**", > "destination": "/index.html" > }, { > // Serves index.html for requests to both "/foo" and "/foo/**" > // Using "/foo/**" only matches paths like "/foo/xyz", but not "/foo" > "source": "/foo{,/**}", > "destination": "/index.html" > }, { > // Excludes specified pathways from rewrites > "source": "!/@(js|css)/**", > "destination": "/index.html" > } ] > } >

Solution 2 - Firebase

Full example:

{
  "hosting": {
    "public": ".",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

Solution 3 - Firebase

If you set it to yes, then all invalid URLs like www.example.com/some-invalid-url will be redirected to index.html of your site which is a good thing. You can also set it to your custom 404.html.

firebase.json

{

  "hosting": {
    "public": "pubic",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "cleanUrls": true
  }
}

Bonus: set the cleanUrls to true to remove .html extensions from your deployed website urls else all urls without .html will redirect to index.html.

Solution 4 - Firebase

Official Firebase explanation:

We had used that option last year (Q1 & Q2) but it seemed to do nothing, but nowadays when we apply it, definitely things work very different. The complete official explanation of what it does comes in here:

https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites

There's even some useful information about Headers usage in the next section of the same page.

Solution 5 - Firebase

As a note: if you would like to have Server-Side Rendering (SSR), type No and set up your rewrites as follow:

"rewrites": [
  {
    "function": "angularUniversalFunction",
    "source": "**"
  }
]

After all, whatever you will choose you can always change this in a firebase.json file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKayce BasquesView Question on Stackoverflow
Solution 1 - FirebaseFrank van PuffelenView Answer on Stackoverflow
Solution 2 - FirebaseAndrew StrommeView Answer on Stackoverflow
Solution 3 - FirebaseGorvGoylView Answer on Stackoverflow
Solution 4 - FirebaseDavidTaubmannView Answer on Stackoverflow
Solution 5 - FirebaseDaniel DanieleckiView Answer on Stackoverflow