score:285

Accepted answer

i'm assuming you're using webpack. if so, adding a few things to your webpack config should solve the issue. specifically, output.publicpath = '/' and devserver.historyapifallback = true. here's an example webpack config below which uses both of ^ and fixes the refresh issue for me. if you're curious "why", this will help.

var path = require('path');
var htmlwebpackplugin = require('html-webpack-plugin');

module.exports = {
  entry: './app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index_bundle.js',
    publicpath: '/'
  },
  module: {
    rules: [
      { test: /\.(js)$/, use: 'babel-loader' },
      { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]}
    ]
  },
  devserver: {
    historyapifallback: true,
  },
  plugins: [
    new htmlwebpackplugin({
      template: 'app/index.html'
    })
  ]
};

i wrote more about this here - fixing the "cannot get /url" error on refresh with react router (or how client side routers work)

score:0

adding to the accepted answer, if you are using the htmlwebpackplugin and still getting errors double check the filename property. if you are setting the filename specifically for production then you will need to add a condition to set the filename contingent on the webpack_dev_server variable. please see the example below:

new htmlwebpackplugin({
   template: './src/index.html',
   filename: process.env.webpack_dev_server ? 'index.html' : prod_html_path,
})

score:1

if you use webpack check your configuration in part of server configuration for the "contentbase" attribute. you can set by this example:

devserver: {
    ...
    contentbase: path.join(__dirname, '../')
    ...
}

score:2

i also had success with this by adding ... historyapifallback: true

devserver: {
    contentbase: path.join(__dirname, "public"),
    watchcontentbase: true,
    publicpath: "/dist/",
    historyapifallback: true
}

score:3

i was having the same issue, what fixed it for me was editing my package.json file, and under scripts: {

"build": "webpack -d && copy src\\index.html dist\\index.html /y && webpack-dev-server --content-base src --inline --port 3000"

at the end of my webpack build code i added --history-api-fallback this also seemed like the easiest solution to the cannot get /url error

note: the next time you build after adding --history-api-fallback you will notice a line in the output that looks like this (with whatever your index file is):

404s will fallback to /index.html

score:3

if you are using express (not webpack), this worked for me..

app.get('/*', function(req, res) {
  res.sendfile(path.join(__dirname, 'path/to/your/index.html'), function(err) {
    if (err) {
      res.status(500).send(err)
    }
  })
})

score:8

if your application is hosted on a static file server, you need to use a instead of a .

import { hashrouter } from 'react-router-dom'

reactdom.render((
  <hashrouter>
    <app />
  </hashrouter>
), holder)

https://github.com/reacttraining/react-router/blob/v4.1.1/faq.md#why-doesnt-my-application-render-after-refreshing

score:10

it worked for me just need just addding devserver { historyapifallback: true } is ok, not need use publicpath: '/'

usage like:

  devserver: {
    historyapifallback: true
  },

score:16

just to supplement tyler's answer for anyone still struggling with this:

adding the devserver.historyapifallback: true to my webpack config (without setting publicpath) fixed the 404/cannot-get errors i was seeing on refresh/back/forward, but only for a single level of nested route. in other words, "/" and "/topics" started working fine but anything beyond that (e.g. "/topics/whatever") still threw a 404 on refresh/etc.

just came across the accepted answer here: unexpected token < error in react router component and it provided the last missing piece for me. adding the leading / to the bundle script src in my index.html has solved the issue completely.


Related Query

More Query from same tag