I have a react website that works fine locally and uses react browser router. When I build it and place the build folder on my hosted webhost (dreamhost) The routing to other pages does not work. The app.js
<Router>
<Header />
<Routes>
<Route exact path="/" element={<Home />} />
<Route path="/contact" element={<ContactPage />} />
</Routes>
<Footer />
</Router>
I followed this guide: https://www.andreasreiterer.at/fix-browserrouter-on-apache/
Which suggested adding an .htaccess file with
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
So after adding the htaccess file with those lines, my hosted react website now correctly routes to pages (whereas before it was a 404). The only problem is now when I go to those pages I just get console errors that say:
2.6c645178.chunk.js:1 Uncaught SyntaxError: Unexpected token '<' (at 2.6c645178.chunk.js:1:1)
main.2e7dd33a.chunk.js:1 Uncaught SyntaxError: Unexpected token '<' (at main.2e7dd33a.chunk.js:1:1)
manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.
Based on things I found online, it sounds like it is treating the pages as html and not recognizing it as javascript. Does anyone have any advice for how I might solve this? Suggestions I saw mentioned adding the lang= to the script tag. But in my react app I don't have any script tag.
react-router-dom, or even reallyreact. The server hosting the app and serving up page requests needs to be sufficiently configured to redirect all page requests to to the rootindex.htmlfile so your React app can load and mount, and handle routing to the correct app page internally. I'm unfamiliar with "dreamhost" so it's not clear if using apache setup/configuration is correct. Take a look at the CRA deployment docs and you'll find many different server environments need different setups/configurations.RewriteRule ^ index.html [QSA,L]T73560- Where is thatT73560coming from? Is that just a typo in your question? "it sounds like it is treating the pages as html and not recognizing it as javascript" - What is the response? Any error response from the server will be atext/htmlresponse.