React-router: When I refresh the browser Cannot GET thiscurrent router

Created on 3 May 2017  路  5Comments  路  Source: ReactTraining/react-router

router config

const routes = [
  { 
    path: '/',
    exact: true,
    component: Main
  },
  { 
    path: '/plan',
    component: PranList
  },
  { 
    path: '/study',
    component: StudyList,
    routes: [
      {
        path: '/study/',
        component: BCMain
      },
      {
        path: '/study/bus',
        component: Bus
      },
      {
        path: '/study/cart',
        component: Cart
      }
    ]
  },
  {
    component: NoMatch
  }
]

from '/' to '/plan' is normal
image

When I refresh the browser Cannot GET thiscurrent router

image

Most helpful comment

This isn't an issue with React Router, this occurs with any client-side routing for single page applications.

The reason this occurs is because whatever is serving your application doesn't know what to do with /plan, it only exists on the client, so it gives you a 404.

The configuration depends on what you're serving your application with, but, the gist of it is you'll need to send all paths that aren't static files to load index.html.

Apache -> mod_rewrite
Nginx -> rewrite
Webpack Dev Server -> historyApiFallback
Express/Koa/Hapi/etc. -> a wildcard route to your index.html

All 5 comments

This isn't an issue with React Router, this occurs with any client-side routing for single page applications.

The reason this occurs is because whatever is serving your application doesn't know what to do with /plan, it only exists on the client, so it gives you a 404.

The configuration depends on what you're serving your application with, but, the gist of it is you'll need to send all paths that aren't static files to load index.html.

Apache -> mod_rewrite
Nginx -> rewrite
Webpack Dev Server -> historyApiFallback
Express/Koa/Hapi/etc. -> a wildcard route to your index.html

As @mhazy said, you need a server in order to support "pretty" URIs. This is also covered in React Router's FAQs https://github.com/ReactTraining/react-router/blob/v4.1.1/FAQ.md#why-doesnt-my-application-render-after-refreshing

In my case, I was using hash routing, so the routes were broken. Fixed it with:

import { createHashHistory } from 'history'

// create history
const history = createHashHistory({
  basname: '',
  hashType: 'slash'
}

// pass to middleware
const store = createStore(
  combineReducers(merge(reducers, { router: routerReducer })),
  applyMiddleware(thunkMiddleware, routerMiddleware(history))
)

for any one using webpack dev server :

you guys just need to add this to your config file

devServer: {
    contentBase: './src',
    stats: "errors-only",
    port: 9000,
    open:true,
    historyApiFallback: true
  }

historyApiFallback will handle that ....

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Radivarig picture Radivarig  路  3Comments

ryansobol picture ryansobol  路  3Comments

maier-stefan picture maier-stefan  路  3Comments

misterwilliam picture misterwilliam  路  3Comments

alexyaseen picture alexyaseen  路  3Comments