How to tell webpack dev server to serve index.html for any route

后端 未结 10 2085
心在旅途
心在旅途 2020-11-28 22:00

React router allows react apps to handle /arbitrary/route. In order this to work, I need my server to send the React app on any matched route.

But webpa

10条回答
  •  无人及你
    2020-11-28 22:27

    I know this question is for webpack-dev-server, but for anyone who uses webpack-serve 2.0. with webpack 4.16.5; webpack-serve allows add-ons.You'll need to create serve.config.js:

    const serve = require('webpack-serve');
    const argv = {};
    const config = require('./webpack.config.js');
    
    const history = require('connect-history-api-fallback');
    const convert = require('koa-connect');
    
    serve(argv, { config }).then((result) => {
      server.on('listening', ({ server, options }) => {
          options.add: (app, middleware, options) => {
    
              // HistoryApiFallback
              const historyOptions = {
                  // ... configure options
              };
    
              app.use(convert(history(historyOptions)));
          }
      });
    });
    

    Reference

    You will need to change the dev script from webpack-serve to node serve.config.js.

提交回复
热议问题