Cross-posted to https://github.com/andreypopp/react-async/issues/34
When using react-async is is common to have code which looks like:
var UserPage = React.createClass({
mixins: [ReactAsync.Mixin],
statics: {
getUserInfo: function(username, cb) {
superagent.get(
'localhost:3000/api/users/' + username,
function(err, res) {
cb(err, res ? res.body : null);
});
}
},
getInitialStateAsync: function(cb) {
this.type.getUserInfo(this.props.username, cb);
},
...
The problem with this is that it only runs correctly in a browser running on the server.
Using the obvious solution of making the URLs relative (e.g. '/api/users/' + username
has a subtle issue.
It seems to work, when moving between pages, but does not work on a page reload or initial load. (You do not actually move between pages in a ReactJS app, it's just the URL that changes.)
The cause of this issue is that the server needs to call the AJAX API, during server-side rendering, but the server has no knowledge of the page origin as seen by browsers (http://www.example.com:3000
).
Is there a way of telling this to the server-side renderer?
(I have already thought of a nasty work-around, where you use full URLs for both client and server, but this must be configured explicitly for each development, test and production server that runs the code.)