React nested route fails to load on refresh

后端 未结 3 1008
清酒与你
清酒与你 2021-02-03 10:44

I have a React app with navigation powered by react-router that I run in development with webpack-dev-server and the history fallback option enabled. H

3条回答
  •  情深已故
    2021-02-03 11:11

    For those who have tried the above and stil have issues, I'm using html-webpack-plugin on my webpack configuration, so it injects the bundle inside the HTML with no need to add any script import on index.html

    So I removed the following line from my index.html file:

    
    

    And added the following configuration on webpack plugins section:

    // some other imports 
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    
    module.exports = {
    ...
    webpack default config
    ...
    
    plugins: [
        new HtmlWebpackPlugin({
          template: './public/index.html',
          hash: true,
          inject: true,
        }),
        ...
        other plugins,
      ],
    
    }
    

    Just remember to also use the output configuration accordingly to your project, such as mine:

    output: {
        path: path.resolve(__dirname, 'public'),
        publicPath: '/',
        filename: 'bundle.js',
      },
    

提交回复
热议问题