este icon indicating copy to clipboard operation
este copied to clipboard

Fetch URL problem in production env (Heroku)

Open amowu opened this issue 11 years ago • 9 comments

When use server render to open http[s]://xxx.com/todos, the ensureAbsoluteUrl return localhost path, then isomorphicFetch parse error.

4fc7d20a-7d16-11e5-981f-94b419734875

amowu avatar Oct 29 '15 04:10 amowu

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) 

ilionic avatar Oct 29 '15 23:10 ilionic

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.

Kurtas avatar Nov 10 '15 14:11 Kurtas

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 :-)

steida avatar Nov 10 '15 20:11 steida

Config in environment variable?

louy2 avatar Nov 11 '15 09:11 louy2

@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.

XeeD avatar Nov 11 '15 14:11 XeeD

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;
  }
});

rkoberg avatar Nov 11 '15 14:11 rkoberg

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 avatar Jan 15 '16 19:01 dougnukem

@dougnukem Thank you for info. I will redesign fetch when react-native 0.18 will be relased. Your comment was helpful.

steida avatar Jan 15 '16 21:01 steida

Awesome!

steida avatar Jan 15 '16 22:01 steida