I have non-SPA server-side application with React application that is limited to current page, /some/static/page. The application has <base href="/"> in <head> on all pages and relies on it, this cannot be changed.
Here is basic example with React 16, React Router 4 and <HashRouter>:
export class App extends React.Component {
render() {
return (
<HashRouter>
<div>
<Route exact path="/" component={Root} />
</div>
</HashRouter>
);
}
}
All routes can be disabled for testing purposes, but this doesn't change the behaviour.
Here is create-react-app project that shows the problem. The steps to replicate it are:
npm inpm start- navigate to
http://localhost:3000/some/static/page
HashRouter is clearly affected by <base>. It redirects from /some/static/page to /#/ on initialization, while I expect it to be /some/static/page#/ or /some/static/page/#/ (works as intended only in IE 11).
There's a quick splash of Root component before it redirects to /#/.
It redirects to /foo/#/ in case of <base href="/foo">, and it redirects to /some/static/page/#/ when <base> tag is removed.
The problem affects Chrome and Firefox (recent versions) but not Internet Explorer (IE 11).
Why is <HashRouter> affected by <base>? It's used here exactly because it isn't supposed to affect location path, only hash.
How can this be fixed?