webpack historyApiFallback configuration for deep routes
Asked Answered
F

1

7

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....

Ferroconcrete answered 6/9, 2016 at 15:25 Comment(1)
Can you show us your code in index.html?Torse
E
8

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

Entrechat answered 24/1, 2017 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.