historyApiFallback doesn't work in Webpack dev server

后端 未结 4 835
小鲜肉
小鲜肉 2020-12-05 07:09

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.

相关标签:
4条回答
  • 2020-12-05 07:40

    I had this problem and was only able to fix it using index: '/' with webpack 4.20.2

            historyApiFallback: {
                index: '/'
            }
    
    0 讨论(0)
  • 2020-12-05 07:47

    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.

    0 讨论(0)
  • 2020-12-05 07:54

    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
        },
    ...
    }
    
    0 讨论(0)
  • 2020-12-05 07:56
    output: {
        ...
        publicPath: "/"
      },
    

    Adding public path solved this for me

    0 讨论(0)
提交回复
热议问题