Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React App returns error after refresh

I'm creating a React/Redux app with React Router v4. It has a simple architecture

--RootPage    path /
 --HomePage   path /h
   -Gallery   path /h/portfolio
   -Links     path /h/links
   -About     path /h/about

Now, every time I refresh, if I'm on second level /h/portfolio, or /h/links, or /h/about, this error net::ERR_ABORTED.

enter image description here

The refresh works fine if I'm on root level or /h/ level. And I've added historyApiFallback: true to devServer so it's not that issue. It's bit unproductive to have to always go to / root everytime I refresh. What could be wrong here?

Here are my routes

const mainRouter=(
    <Wrapper>
<Provider store={store} history={history}>
  <BrowserRouter>
      <div>
      <Route exact path="/" component={Home}/>
      <Route path="/h" component={HomePage}/>
       </div>
   </BrowserRouter>
    </Provider>
    </Wrapper>
  )

And in HomePage

       <main>
          <Route path={`${this.props.match.path}/portfolio`}
          render={ ()=><Gallery {...this.props}/> } />
          <Route path={`${this.props.match.path}/about`} render={ ()=><About 
          {...this.props}/> }/>
          <Route path={`${this.props.match.path}/links`} render={ ()=><Links 
          {...this.props}/> }/>

      </main>
like image 557
Ndx Avatar asked Sep 21 '17 13:09

Ndx


People also ask

Is React harder than PHP?

React vs PHP: Developers Additionally, React is easier to develop than traditional PHP frameworks, which makes it a better choice for larger projects.

Is laravel harder than React?

Is laravel development simpler than react? Development with Laravel is much more straightforward than React. The immense community of Laravel backs you if you have development issues.

What does NaN mean in React?

The global NaN property is a value representing Not-A-Number. Property attributes of NaN.


1 Answers

After reproducing the same problem on express server instead of devserver, I got the same error and doing some research, I found this simple fix to this problem

All I had to do was change the <script src="js/app.bundle.js"></script> to <script src="/js/app.bundle.js"></script>

Edit: If you use Webpack-html-plugin which I do, it still gives same error, I had to resolve it by adding output.path.publicPath:"/" in webpack config file

like image 72
Ndx Avatar answered Dec 07 '22 04:12

Ndx