How to allow for webpack-dev-server to allow entry points from react-router

后端 未结 9 1945
無奈伤痛
無奈伤痛 2020-12-04 05:15

I\'m creating an app that uses webpack-dev-server in development alongside react-router.

It seems that webpack-dev-server is built around the assumption that you wil

相关标签:
9条回答
  • 2020-12-04 06:03

    This worked for me: just simply add the webpack middlewares first and the app.get('*'... index.html resolver later,

    so express will first check if the request matches one of the routes provided by webpack (like: /dist/bundle.js or /__webpack_hmr_) and if not, then it will move to the index.html with the * resolver.

    ie:

    app.use(require('webpack-dev-middleware')(compiler, {
      publicPath: webpackConfig.output.publicPath,
    }))
    app.use(require('webpack-hot-middleware')(compiler))
    app.get('*', function(req, res) {
      sendSomeHtml(res)
    })
    
    0 讨论(0)
  • 2020-12-04 06:05

    For anyone else that may still be looking for this answer. I put together a simple proxy bypass which achieves this without much hassle and the config goes into the webpack.config.js

    I am sure there are much more elegant ways to test for local content using regex, but this works for my needs.

    devServer: {
      proxy: { 
        '/**': {  //catch all requests
          target: '/index.html',  //default target
          secure: false,
          bypass: function(req, res, opt){
            //your custom code to check for any exceptions
            //console.log('bypass check', {req: req, res:res, opt: opt});
            if(req.path.indexOf('/img/') !== -1 || req.path.indexOf('/public/') !== -1){
              return '/'
            }
    
            if (req.headers.accept.indexOf('html') !== -1) {
              return '/index.html';
            }
          }
        }
      }
    } 
    
    0 讨论(0)
  • 2020-12-04 06:05

    historyApiFallback can also be an object instead of a Boolean, containing the routes.

    historyApiFallback: navData && {
      rewrites: [
          { from: /route-1-regex/, to: 'route-1-example.html' }
      ]
    }
    
    0 讨论(0)
提交回复
热议问题