【问题标题】:webpack-dev-server, historyApiFallback not working (webpack-4, react-router-4)webpack-dev-server,historyApiFallback 不起作用(webpack-4,react-router-4)
【发布时间】:2019-04-22 14:06:03
【问题描述】:

我正在用 webpack4 测试 react-router4,但我无法获取 webpack-dev-server 的设置:

{historyApiFallback: true}

工作。这个使用在 webpack3 中工作得很好,所以我不确定是什么问题......这是我的 webpack.config.js:

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = () => {
  return {
    mode: 'development',
    devtool: 'source-map',
    devServer: {
      port: 8888,
      historyApiFallback: true,
      stats: 'minimal'
    },
    resolve: {
      extensions: ['*', '.mjs', '.js', '.jsx']
    },
    module: {
      rules: [
        {
          test: /\.m?jsx?$/,
          exclude: /(node_modules)/,
          use: {
            loader: 'babel-loader'
          }
        }
      ]
    },
    plugins: [
      new HtmlWebpackPlugin({
        title:'React Lab',
        template: 'src/index.html'
      })
    ]
  }
}

这是我使用 react-router4 的简单反应应用程序:

import React from 'react';
import ReactDOM  from 'react-dom';
import {
  BrowserRouter as Router,
  Route,
  Link,
  Switch
} from 'react-router-dom';

const mountNode = document.getElementById('app');

const App = () => (
  <Router>
    <div>

      <ul>
        <li><Link to="/">Link to: /</Link></li>
        <li><Link to="/page1">Link to: /page1</Link></li>
        <li><Link to="/page2">Link to: /page2</Link></li>
        <li><Link to="/does/not/exist">Link to: /does/not/exist</Link></li>
      </ul>
      <button onClick={()=>{location.reload();}}>reload page</button>

      <Switch>
        <Route exact path="/"      component={()=>(<h2>home</h2>)} />
        <Route exact path="/page1" component={()=>(<h2>page1</h2>)} />
        <Route exact path="/page2" component={()=>(<h2>page2</h2>)} />
        <Route                     component={()=>(<h2>no match</h2>)} />
      </Switch>

      <Route path="/" component={(props) =><div>{`props.location.pathname: ${props.location.pathname}`}</div>} />

    </div>
  </Router>
);

ReactDOM.render( <App/>, mountNode

导航到后:

<Link to="/does/not/exist" /> 

然后点击

<button>reload page</button>

webpack 开发服务器无法重定向到 main.js

这里是github上的完整代码: https://github.com/ApolloTang/webpack-dev-server-history-api-fall-back-not-working.

任何帮助或评论将不胜感激。

【问题讨论】:

    标签: webpack react-router webpack-dev-server


    【解决方案1】:

    原来我在 webpack.config.js 中缺少 output.publicPath:

    output: {
      //  must specified output.publicPath otherwise
      //  devServer.historyApiFallback will not work
      publicPath: '/'
    },
    

    如上指定 output.publicPath,historyApiFallback 有效。

    我不记得我在哪里读到过 output.publicPath 在 webpack4 的配置中是可选的,但它确实需要与 webpack-dev-server 一起使用。

    https://webpack.js.org/configuration/output/#output-publicpath 的文档说:

    webpack-dev-server 也从 publicPath 获取提示,使用它来 确定从哪里提供输出文件。

    但我不明白这与 hisitoryApiFallback 有什么关系。

    【讨论】:

      【解决方案2】:

      您的output.publicPath 似乎必须与devServer.historyApiFallback.index 匹配。

      我想知道为什么这不是自动暗示的,就像 DevServer 重用 output.publicPath 一样,除非另有说明。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-20
        • 2018-09-29
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 2020-01-01
        • 2017-09-13
        • 2015-05-09
        相关资源
        最近更新 更多