My understanding of React Router is that once the single page is bootstrapped on the client side, all subsequent calls to routes don't go to the server. They are intercepted by React Router. But why does my app always make a HTTP request to the server? Despite having the React Router code? When I see Networks on the browser dev tools, I always see a network request being made for each "react routed" route.
I tested the app by first starting the server, then delivering the index file across and then stopping the server after the client side rendering was complete. I then tried to access another route such as "/contactus" from the client side app, but the request seems to go to the server (which is now not running), and the hence the client side doesn't get any file and the browser goes blank. My understanding was that React Router intercepts all API calls once the client side bootstrapping of the app is done.
I then modified my server side code by doing a app.get("/*", function (req, res) { res.sendFile(path.join(__dirname, "../dist", "landing_page.html")); });
Now the app works. But this defeats the whole purpose of React Router as all requests are going to the server. So what is React Router then? Is it only for routing and not for interception? Is there any variant of React Router that intercepts the requests? Or do I use something like a history API? This is my code.
import {
BrowserRouter as Router,
Switch,
Route,
Link,
} from "react-router-dom";
class App extends React.Component {
render() {
return (
<Router>
<Switch>
<Route exact path="/">
<div className="banner">
<Banner />
</div>
</Route>
</Switch>
<Switch>
<Route exact path="/cloudautomation">
<div style={{ position: "absolute", top: "15%", zIndex: "-1" }}>
<CloudAutomation />
</div>
</Route>
</Switch>
<Switch>
<Route exact path="/marketingautomation">
<div style={{ position: "absolute", top: "15%", zIndex: "-1" }}>
<MarketingAutomation />
</div>
</Route>
</Switch>
<Switch>
<Route exact path="/contactus">
<div style={{ width: "100%" }}>
<ContactUs />
</div>
</Route>
</Switch>
</div>
</Router>
);
}
}
ReactDOM.render(<App />, document.getElementById("main"));