(Updated below with a bit more info)
We have a one-page reaction-based web application that lives in Firebase Hosting. We used hash-based routing to this point (e.g. mysite.com/#/path/to/content ). We introduced the React Router, which allows us to have more beautiful URLs (for example, mysite.com/path/to/content ), but now the application does not load when we go directly to the deep route. Details below:
Using React Router required us to use URL rewriting in Firebase Hosting. The directions are pretty simple - it seems all you have to do is the following:
"rewrites": [ { "source": "**", "destination": "/index.html" } ]
... inside the firebase.json file. In fact, our full firebase.json file looks like this:
{ "firebase": "", "public": "dist", "rules": "rules.json", "ignore": [ "firebase.json", "**/.*", "**/*.map", "**/node_modules/**" ], "rewrites": [ { "source": "**", "destination": "/index.html" } ] }
There is nothing complicated (we do firebase deploy -f in our build script if you are wondering why the firebase argument firebase empty). The rules.json file is even simpler:
{ "rules":{ ".write":"true", ".read":"true" } }
When I download the application by going to the base URL and starting the navigation, everything is fine. If I go directly to the route at the same level (e.g. mysite.com/path ), this also works.
BUT
If I go directly to a deep-level route, or even to a top-level route with a trailing slash (for example, mysite.com/path/ or mysite.com/path/to/content ), the application does not load.
In particular, what happens when the index.html page is loaded, and then the browser loads our bundle.js file created by webpack, which is referenced in index.html , but the fact that Firebase Hosting returns for this JS file is the contents of the file index.html. So, predictably, what we see in the browser console is an error:
Uncaught SyntaxError: Unexpected token <
... on line 1 of the "package file". If I look at the contents of the package file in Chrome dev tools, line 1 of the package file will literally be like this:
<!doctype html>
... then it follows the rest of the HTML from the index.html file.
There seems to be a rule of rewriting the Firebase URL: "Oh, you are trying to link to a JS file - I see that I have to return the contents of index.html for everything, so here you go." But it cannot always be, or the site will never work under any circumstances. So why does it work if I start from the root site, but it breaks if I embed a deep URL in the application? I have no idea.
If this helps, here is how my index.html file refers to the package file:
<script src="bundle.ce843ef7a2ae68e9e319.js"></script></body>
So, this seems like a problem with Firebase hosting, but I could also see that maybe I didnβt understand React Router at some level, and so I somehow messed up with the client code in such a way that it forces the server to return the wrong one thing.
Here I hope this is some kind of stupid configuration that we are missing. Any help is appreciated!
Thanks!
UPDATE: I deleted our application so that it only returned βhello worldβ, which bypasses all React-based materials, including the React Router, and the problem persists, so I no longer think it has something to do with React-Router although I believe that there is still a slight chance that this might have something to do with React.