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

后端 未结 10 2067
心在旅途
心在旅途 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:33

    You can enable historyApiFallback to serve the index.html instead of an 404 error when no other resource has been found at this location.

    let devServer = new WebpackDevServer(compiler, {
        historyApiFallback: true,
    });
    

    If you want to serve different files for different URIs, you can add basic rewriting rules to this option. The index.html will still be served for other paths.

    let devServer = new WebpackDevServer(compiler, {
        historyApiFallback: {
            rewrites: [
                { from: /^\/page1/, to: '/page1.html' },
                { from: /^\/page2/, to: '/page2.html' },
                { from: /^\/page3/, to: '/page3.html' },
            ]
        },
    });
    

提交回复
热议问题