question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support webpack-dev-server historyApiFallback: true

See original GitHub issue

Hi I don’t know exactly is this issue with webpack but i think so I have simple config for router

var ReactappApp = require('./ReactappApp');
var HelloWorld = require('./HelloWorld');
var React = require('react');
var Router = require('react-router');
var Route = Router.Route;


var content = document.getElementById('content');

var Routes = (
  <Route path="/" handler={ReactappApp}>
    <Route name="hello" path="hello" handler={HelloWorld}/>
  </Route>
);
console.log(Routes);
Router.run(Routes,Router.HistoryLocation, function (Handler) {
  React.render(<Handler/>, content);
});

So i try to load HelloWorld component at /hello and get Cannot GET /hello So server doesn’t see this route

I use grunt and connect also. Here is grunt task for webpack:

'webpack-dev-server': {
      options: {
        hot: true,
        port: 8000,
        webpack: webpackDevConfig,
        publicPath: '/assets/',
        contentBase: './<%= pkg.src %>/'
    }

and here is webpack config https://gist.github.com/fellz/b2cc0de81c7b1f5bf442#file-webpack-config

with historyApiFallback: true

'webpack-dev-server': {
      options: {
        hot: true,
        port: 8000,
        webpack: webpackDevConfig,
        publicPath: '/assets/',
        contentBase: './<%= pkg.src %>/',
        historyApiFallback: true
    }

it’s loading ReactappApp instead of HelloWorld

p.s. react-router-component works as expected and perfectly well

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:7
  • Comments:42 (9 by maintainers)

github_iconTop GitHub Comments

154reactions
BenFlanagancommented, Nov 28, 2015

I’ve been struggling with this for longer than I’d like to admit, but I’ve just figured it out.

Besides enabling historyApiFallback, if you’re having trouble only with nested routes, check your html file’s reference to your script.

In my case, it was the difference between <script src="/bundle.js"></script> and <script src="bundle.js"></script> causing problems on nested routes. Basic stuff, and super obvious now that I’ve found it, but it was easy to miss.

Thanks to @adrianmacneil for the sample app, it was a big help.

edit - “/bundle.js” being the correct src, so the reference isn’t taken as relative to the current path.

84reactions
amacneilcommented, Sep 28, 2015

Some of the examples above are very complicated. In most cases you can get this working simply by adding the following lines to your webpack.config.js file:

devServer: {
  historyApiFallback: true
}

I made an example app to help anyone else looking for a solution to this (like I was).

Read more comments on GitHub >

github_iconTop Results From Across the Web

DevServer - webpack
webpack -dev-server can be used to quickly develop an application. See the development guide ... Enable devServer.historyApiFallback by setting it to true :....
Read more >
historyApiFallback doesn't work in Webpack dev server
historyApiFallback.index indicate that when url path not match a true file,webpack-dev-server use the file config in ...
Read more >
Support webpack-dev-server historyApiFallback: true #676
A hackish but simpler way to do that is to inject a middleware (note this relies on undocumented property and may stop working...
Read more >
historyApiFallback troubles with webpack-dev-server and ...
In your webpack config file, add the devServer root level property, set to an object with historyApiFallback: true like so:.
Read more >
History API Routing for SPAs using webpack-dev-server
We need to enable the so-called History API Fallback which basically means the server should fallback to serve index.html in case the requested ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found