React-router v4 – cannot GET *url*

I’m assuming you’re using Webpack. If so, adding a few things to your webpack config should solve the issue. Specifically, output.publicPath="https://stackoverflow.com/" 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: "https://stackoverflow.com/"
  },
  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)

Leave a Comment