Browserify + WebStorm debug breaks routing in React-Router v4 BrowserRouter

107 views Asked by At

I am writing a single page app with React for educational purposes. My React-Router v4 BrowserRouter handles client side routing correctly on CodeSandbox but not locally. In this case, the local server is the webstorm built-in devserver. HashRouter works locally but BrowserRouter does not.

Functioning properly: https://codesandbox.io/s/j71nwp9469

2

There are 2 answers

0
user2355058 On BEST ANSWER

The solution was to understand how push state routing and the history API works. It is necessary to proxy requests through the index page when serving Single Page Applications that utilize the HTML5 History API.

The Webstorm dev server is not expected to include this feature, therefore the mention of Webstorm in this thread was a mistake.

There are multiple libraries of < 20 lines which do this for us, or it can easily be hand coded.

0
lena On

You are likely serving your app on the built-in webserver (localhost:63342), right? Internal web server returns 404 when using 'absolute' URLs (the ones starting with slash) as it serves files from localhost:port/project_name and not from localhost:port. That's why you have to make sure to change all URLs from absolute to the relative ones.

There is no way to set up the internal webserver to use project root as server document root. But you can configure it to use URLs like http://<host name>:<port> where the 'host name' is a name specified in hosts file, like 127.0.0.1 myhostName. See https://youtrack.jetbrains.com/issue/WEB-8988#comment=27-577559.