I use Webpack dev server and browserHistory in React Router to manipulate with urls by HTML5 History API. historyapifallback-option does not work in my webpack config file.
I had this problem and was only able to fix it using index: '/'
with webpack 4.20.2
historyApiFallback: {
index: '/'
}
I meet the same question today.
let config in webpack.config.js: output.publicPath
be equal to devServer.historyApiFallback.index and point out html file route。my webpack-dev-server version is 1.10.1 and work well. http://webpack.github.io/docs/webpack-dev-server.html#the-historyapifallback-option doesn't work, you must point out html file route.
for example
module.exports = {
entry: "./src/app/index.js",
output: {
path: path.resolve(__dirname, 'build'),
publicPath: 'build',
filename: 'bundle-main.js'
},
devServer: {
historyApiFallback:{
index:'build/index.html'
},
},
};
historyApiFallback.index indicate that when url path not match a true file,webpack-dev-server use the file config in historyApiFallback.index to show in browser rather than 404 page. then all things about your route change let your js using react-router do it.
Ref.: https://webpack.js.org/configuration/dev-server/#devserver-historyapifallback
This works with any react-router
You have to add historyApiFallback: true
module.exports = {
cache: true,
entry: "./index.js",
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'public')
},
context: SRC,
devServer: {
contentBase: path.resolve(__dirname, 'public/assets'),
stats: 'errors-only',
open: true,
port: 8080,
compress: true,
historyApiFallback: true
},
...
}
output: {
...
publicPath: "/"
},
Adding public path solved this for me