React 应用程序在刷新时崩溃 [webpack,REACT]

react app crashes on refresh [webpack , REACT ]

所以我有这个反应 webpack 项目我 运行 使用 webpack-dev-server 一切正常,但是当 url 有两层深并且我刷新页面时应用程序崩溃.
例如,如果我在 http://localhost:8080/product/someProductIdhttp://localhost:8080/checkout/information 刷新时出现此错误 Failed to load resource: the server responded with a status of 404 (Not Found)
但如果它 http://localhost:8080/shop 它不会在刷新时崩溃
这是我的 webpack 配置:

const path = require('path')
const HTMLPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin');

module.exports ={
    entry :"./src/index.js",
    devServer:{historyApiFallback: true},
    output:{
        path:path.resolve(__dirname,"./dist"),
        filename:'index.js',
    },
    plugins:[
        new HTMLPlugin({template:'./src/index.html'}),
        new CopyPlugin({
            patterns: [
              { from: './src/images', to: 'images' },
            ],
          }),
    ],
    module:{
        rules:[
            { 
                test : /\.js$/,
                exclude:/node_modules/,
                use:{
                    loader:"babel-loader"
                },
            },
            {
                test: /\.(png|gif|jpg)$/,
                include: [
                  path.join(__dirname, './src/images')
                ],
                loader: 'file-loader',
             },
            {
                test:/.(png|jpg|woff|woff2|eot|ttf|svg|gif)$/, 
                use: [
                  {
                    loader: 'url-loader',
                    options: {
                      limit: 100000000,
                    }
                  }
               ]
            },
            {
                test: /\.js$/,
                enforce: 'pre',
                use: ['source-map-loader'],
            },
            {
                test: /\.css$/i,
                loader: 'style-loader!css-loader' 
            },
        ]
    }
}

package.json 的脚本:

 "scripts": {
    "nodemon": "nodemon ./backEnd/app.js",
    "start": "webpack-dev-server --mode development --open --hot -inline --progress --content-base ",
    "build": "webpack --mode production",
    "watch": "webpack-dev-server --progress"
  },

告诉 Webpack Dev Server 将所有服务器请求重定向到 /index.html。为此,您需要设置 webpack 配置中的两个属性,publicPathhistoryApiFallback.

publicPath: '/',
historyApiFallback: true,

publicPath 允许您为应用程序中的所有资产指定基本路径。 historyAPIFallback 会将 404 重定向到 /index.html.

这是一个例子:

module.exports = {
  entry: './app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
  },
  devServer: {
    historyApiFallback: true,
  }
// rest of your config ...
};

查看更多详情this