Fetch URL problem in production env (Heroku)
When use server render to open http[s]://xxx.com/todos, the ensureAbsoluteUrl return localhost path, then isomorphicFetch parse error.

Same issue, but instead it crashed the app completely. Log from Heroku:
Oct 29 16:02:15 esteredux app/web.1: Yay Error: request to http://localhost:8000/api/v1/todos/user failed, reason: connect ECONNREFUSED 127.0.0.1:8000
Oct 29 16:02:15 esteredux app/web.1: at ClientRequest.<anonymous> (/app/node_modules/isomorphic-fetch/node_modules/node-fetch/index.js:117:11)
Oct 29 16:02:15 esteredux app/web.1: at emitOne (events.js:77:13)
Oct 29 16:02:15 esteredux app/web.1: at ClientRequest.emit (events.js:169:7)
Oct 29 16:02:15 esteredux app/web.1: at Socket.socketErrorListener (_http_client.js:259:9)
Oct 29 16:02:15 esteredux app/web.1: at emitOne (events.js:77:13)
Oct 29 16:02:15 esteredux app/web.1: at Socket.emit (events.js:169:7)
Oct 29 16:02:15 esteredux app/web.1: at emitErrorNT (net.js:1253:8)
Oct 29 16:02:15 esteredux app/web.1: at doNTCallback2 (node.js:439:9)
Oct 29 16:02:15 esteredux app/web.1: at process._tickCallback (node.js:353:17)
The problem is that the Este has hardcoded http://localhost:8000/ to make absolute url for fetching. see https://github.com/este/este/blob/master/src/common/fetch.js#L4
There should be actual server:port which Express uses.
This unfortunately must be configured and can't be autodetected, because fetch is supposed to work on React Native, and native app has no notion about server url and port.
So, we need to figure out where to put config, and how to use it. PR welcome :-)
Config in environment variable?
@louy2 Yes, I suggest passing the config in process.env + some default. Usually you have several types of servers, each connected to different API (dev, staging, test, production) so it is best to have the possibility to configure this from the env.
In the root of este commit a local.conf.example.js with all variables that need to be set. Copy that to a .gitignore'd file local.conf.js. Set your environment variables there for local development. For production, there is usually someplace where you can set environment variables. For the fetch issue on the client, and not sure how this works for mobile, use the intitialState that is created in the server side render to set the api server base url and whatever other variables the client needs. Then in the gulpfile env task:
gulp.task('env', () => {
process.env.NODE_ENV = args.production ? 'production' : 'development';
if (fs.existsSync('./local.conf.js')) {
require('./local.conf');
process.env.TP_IS_LOCAL = true;
}
});
It seems like the absolute URL is only necessary for React-Native, is there some way to isolate that to just be available in the react-native fetch and provide the server via environment.
If you go ahead and remove that when running on Heroku, server-side rendering and browser works fine.
function ensureAbsoluteUrl(input) {
if (typeof input !== 'string') return input;
if (URI(input).is('absolute')) return input;
return URI(input).normalize().toString();
}
@dougnukem Thank you for info. I will redesign fetch when react-native 0.18 will be relased. Your comment was helpful.
Awesome!