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

后端 未结 9 1975
無奈伤痛
無奈伤痛 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: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';
            }
          }
        }
      }
    } 
    

提交回复
热议问题