I have a route configuration that looks like this (showing only the parts that I think may be relevant):
var React = require('react');
var Router = require('react-router');
var { Route, DefaultRoute, NotFoundRoute } = Router;
var routes = (
<Route handler={AppHandler}>
<DefaultRoute handler={HomeHandler}/>
<Route name='home' path='/home' handler={HomeHandler}/>
<Route name='settings' path='/settings/?:tab?' handler={SettingsHandler}/>
<NotFoundRoute handler={NotFoundHandler}/>
</Route>
);
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, document.getElementById('application'));
});
Now, on my Settings react file, I have the following:
var Settings = React.createClass({
mixins: [Router.State],
render: function() { ... }
});
If I access the host.local/settings
and log this.getParams()
everything works fine and the file renders showing me Object {tab: undefined}
in the console. But as soon as I try host.local/settings/user
- where I expected the console to return something like Object {tab: 'user'}
-, the whole thing crashes somewhere and just starts throwing Uncaught SyntaxError: Unexpected token <
in the console.
React is still young and therefore the errors are rather vague in many situations.
I've followed the specifications provided on the Path Matching Guide and it seems pretty standard, so I can only assume it's a problem with react-router itself or am I missing something?
Update
tl;dr: It's not a problem with react-router.
Long version:
Apparently, it turns out the issue is not on ReactJS neither React-Router themselves. It involves mostly a bug on gulp-webserver (which I'm using for developing using proxies to connect separate projects running in parallel) and triggers an error when URLs are typed directly on the browser instead of being accessed through a link.
I did a test and it works fine when navigating, but crashes when accessed directly as described on this github issue, which basically renders my functional deeplink tests unviable, but should work when in production.
Update 2
tl;dr: It's not a problem with gulp-webserver either.
In my case, it was related to relative
and absolute
paths and not any issue with gulp-webserver as mentioned before. To be more specific, I had to make sure your script and CSS references were absolute so that it doesn't try to find them in the path you're typing in the URL.
e.g.:
You have two URLs: /settings
and /settings/account
. In that case, if you have your script inclusion in the fallback file being something like <script src="scripts/main.js"></script>
, the server will return a 404 for it, since there's no file in /settings/scripts/main.js
.
Dumb of me, but in case anyone else falls for that, I hope this helps.
question from:
https://stackoverflow.com/questions/28253162/react-router-dynamic-segments-crash-when-accessed