webpack historyApiFallback配置深度路由

时间:2016-09-06 15:25:47

标签: webpack-dev-server html5-history nested-routes

webpack-dev-server可以设置为将您发送回index.html并查找单个路由的脚本,例如http://localhost:4301/sdr,但是当您放入更深的路径(或带有/的单个路由)时在最后)http://localhost:4301/sdr/dog它变得混乱。

  devServer: {
    contentBase: './dist',
    historyApiFallback: true
  },

http://localhost:4301/sdr/dog服务器响应

x GET http://localhost:4301/sdr/bundle.js 

将/ sdr添加到其搜索bundle.js

的路径中

我该如何解决这个问题。 ...然后我会在NGINX上尝试使用react-router然后使用navigo然后使用react-router-redux ....

1 个答案:

答案 0 :(得分:2)

我也遇到过这个问题。我发现解决方案是将publicPath: '/'添加到输出下的webpack配置中。

const base = {
  entry: [
    PATHS.app,
  ],
  output: {
    path: PATHS.build,
    publicPath: '/',
    filename: 'index_bundle.js',
  },
  module: {
    loaders: [
      {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
      {test: /\.css$/, loader: 'style!css?sourceMap&modules&localIdentName=[name]__[local]___[hash:base64:5]'},
      {test: /\.json$/, loader: 'json'},
    ],
  },
  resolve: {
    root: path.resolve('./app'),
  },
}

const developmentConfig = {
  devtool: 'cheap-module-inline-source-map',
  devServer: {
    contentBase: PATHS.build,
    hot: true,
    inline: true,
    progress: true,
    proxy: {
      '/api': 'http://127.0.0.1:5000',
    },
    historyApiFallback: true,
  },
  plugins: [HTMLWebpackPluginConfig, new webpack.HotModuleReplacementPlugin()],
}

export default Object.assign({}, base, developmentConfig)

以下是此属性的更详细文档:http://webpack.github.io/docs/configuration.html#output-publicpath

以下是有关此问题的更详细讨论的论坛: https://github.com/webpack/webpack/issues/443