'ReactJS -> Router of react-router-dom not working on production build, intead is use BrowserRouter but it ignores History
Ii am trying to understand why Router doesn't work when I make a production build
- the bug is that every
Redirector going through the route path ends on a blank page it makes the push and replace respectively but its not rendering the components - it only happens in production, on local its working fine
- I try to use
BrowserRouter as Routerbut it ignores thehistory.push()and when you try to log in it doesn't redirect to the default page, but when you reload manually the page it goes without no problem, but I need to go by redirect to not make a unnecessary reload because it will consume twice all the endpoints if I make awindow.location.reload(true). AuthService.isAuthenticated()it's a string if it logged in it should return something like this '1234567abcdefg' if not return null- when I log in using
BrowserRouter as Routerit's not calling the methodcheckLoginagain so it's not rendering the next page but if I useRouterinstead usingBrowserRouter as Routerit works fine on localHost - I'm using this dependencys
-
"react": "16.12.0", node version v12.18.2 npm version 6.14.5 "react-dom": "16.12.0", "react-redux": "7.2.0", "react-router-dom": "5.1.2" - my expected behavior is when it make success login it goes to '/default-page' (as it was but don't know where it's broken) right now it makes the success login but get stuck on '/login' when it should render 'default-page'
- It has nothing to do with it .htaccess
The only warning when I use BrowserRouter is this tiny-warning.esm.js:11 Warning: <BrowserRouter> ignores the history prop. To use a custom history, use `import { Router }` instead of `import { BrowserRouter as Router }`.
Routing.js
import { Redirect, Route, Router, Switch } from 'react-router-dom'
import history from '../../history'
const checkAuthentication = (Page, props) => {
if (!AuthService.isAuthenticated()) {
return <Redirect to='/login' />
}
return Notification.permission === NotificationPermissionsType.GRANTED ? (
<Page auth={AuthService} {...props} />
) : (
<AllowNotificationsDialog />
)
}
const checkLogin = props => {
return AuthService.isAuthenticated() ? (
<Redirect to='/' />
) : (
<LoginPage auth={AuthService} {...props} />
)
}
const handleExternalAuth = props => {
return AuthService.isAuthenticated() ? (
<Redirect to='/' />
) : (
<ExternalAuthPage {...props} />
)
}
const Routing = ({
snackBar,
startAppUp,
closeSnackBar,
intl: { formatMessage },
}) => {
useEffect(() => {
startAppUp() // comes from redux/root.actions
}, [])
return (
<>
<Router history={history}>
<Route
path='/'
component={({ location }) => {
// google analytics
ReactGA.pageview(location.pathname)
return null
}}
/>
<Switch>
<Route path='/login' component={props => checkLogin(props)} />
<Route path='/auth' component={props => handleExternalAuth(props)} />
<Route
path='/default-page'
component={props => checkAuthentication(DefaultPage, props)}
/>
<Route
path='/account'
component={props => checkAuthentication(AccountProfile, props)}
/>
<Redirect to='/default-page' />
</Switch>
</Router>
</>
)
}
Routing.defaultProps = {}
export default Routing
history.js
import { createBrowserHistory } from 'history
export default createBrowserHistory()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
