webpack historyApiFallback configuration for deep routes

大兔子大兔子 提交于 2019-12-24 00:46:40

问题


webpack-dev-server can be set up to send you back to index.html and find your scripts for a single route like http://localhost:4301/sdr but when you put in a deeper route (or a single route with a / at the end) http://localhost:4301/sdr/dog it gets confused.

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

with http://localhost:4301/sdr/dog the server responds

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

adding /sdr to the the path in its search for bundle.js

How can I fix this. ... then I will try it on NGINX then with react-router then with navigo then with react-router-redux....


回答1:


I also had this issue. I found the solution was to add publicPath: '/' to my webpack config under output.

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)

Here is more detailed documentation of this property: http://webpack.github.io/docs/configuration.html#output-publicpath

Here is the forum where there is a more detailed discussion of this issue: https://github.com/webpack/webpack/issues/443



来源:https://stackoverflow.com/questions/39352703/webpack-historyapifallback-configuration-for-deep-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!