I have a webapp that is deployed where the root is in the path of /exist/apps/my-app/
. I have the following code implemented for the App.js:
import React from 'react';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';
function App() {
return (
<Router basename={process.env.PUBLIC_URL}>
<Navbar bg="dark" variant="dark" fixed="top">
<Navbar.Brand href="/"><img alt="" src="https://avatars3.githubusercontent.com/u/2393489?s=200&v=4" weign="40" height="40"/> My React Application</Navbar.Brand>
<Nav className="mr-auto">
<Link to="/" className="nav-link">Home</Link>
<Link to="/features" className="nav-link">Features</Link>
<Link to="/pricing" className="nav-link">Pricing</Link>
</Nav>
</Navbar>
<div className="full">
<Switch>
<Route path="/features">
<h1>Features</h1>
</Route>
<Route path="/pricing">
<h1>Pricing</h1>
</Route>
<Route path="/">
<h1>Home</h1>
</Route>
</Switch>
</div>
</Router>
);
}
export default App;
Clicking through the nav links navigates to the proper routing, but /exist/apps/my-app
disappears from the URL displayed. How do I keep the missing part of the URL in the Location bar?
process.env.PUBLIC_URL
contained a period only.
I found two solutions.
First was to hard code basename={'/exist/apps/my-app'}
.
The second and better approach was to eliminate basename
all together and change BrowserRouter
to HashRouter
.
I eventually found the second approach and has the added benefit of being able to bookmark URLs containing a Route.
Checkout https://github.com/lcahlander/my-app for the sample eXist-db web application.