سعيد
سعيد

Reputation: 1764

react app crashes on refresh [webpack , REACT ]

so I have this react webpack project I'm running using webpack-dev-server everything works fine , but when the url is two layers deep and I refresh the page the app crashes .
for example if I was at http://localhost:8080/product/someProductId or http://localhost:8080/checkout/information I get this error when I refresh Failed to load resource: the server responded with a status of 404 (Not Found)
but if its http://localhost:8080/shop it wont crash on refresh
here is my webpack config :

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's scripts :

 "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"
  },

Upvotes: 2

Views: 3304

Answers (1)

Tell Webpack Dev Server to redirect all server requests to /index.html. There are just two properties in your webpack config you need to set to do this, publicPath and historyApiFallback.

publicPath: '/',
historyApiFallback: true,

publicPath allows you to specify the base path for all the assets within your application. historyAPIFallback will redirect 404s to /index.html.

Here’s an example :

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

more details check this

Upvotes: 3

Related Questions