From 88caa08b909792eec05c8d23796a94857dc28436 Mon Sep 17 00:00:00 2001 From: Josh Worden Date: Thu, 12 Jul 2018 15:18:44 -0500 Subject: [PATCH] Load ROOT_URI from request for SSR --- README.md | 4 ++-- docker-compose.yml | 2 +- next.config.js | 2 +- src/apollo/client.js | 34 ++++++++++++++++++---------------- src/apollo/init.js | 2 +- src/server/index.js | 8 +++++++- src/server/routes/index.js | 6 +++--- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index a616577..dee50bf 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,9 @@ Production environment variables are *not* under version control, per [Part 3 of 2. Set (or change) values for the following variables: ```ini APP_PORT=3005 -GRAPH_PROXY=http://docker.for.mac.host.internal:8100 +ROOT_URI=http://docker.for.mac.host.internal:8100 ``` - **Note:** If you are not running on OSX, or you have customized the [cygnusb2b/fortnight-graph](https://github.com/cygnusb2b/fortnight-graph) port, you will need to customize the `EGRAPH_PROXY` URL to point to the IP/Hostname and port of your graph instance: + **Note:** If you are not running on OSX, or you have customized the [cygnusb2b/fortnight-graph](https://github.com/cygnusb2b/fortnight-graph) port, you will need to customize the `ROOT_URI` URL to point to the IP/Hostname and port of your graph instance: - AWS: `curl http://169.254.169.254/latest/meta-data/local-ipv4` - *nix: `ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+'` diff --git a/docker-compose.yml b/docker-compose.yml index ac7f07e..cdb7748 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,7 +15,7 @@ services: environment: NODE_ENV: development PORT: ${APP_PORT-3005} - GRAPH_PROXY: ${GRAPH_PROXY-http://docker.for.mac.host.internal:8100} + ROOT_URI: ${ROOT_URI-http://docker.for.mac.host.internal:8100} volumes: node_modules: {} next: diff --git a/next.config.js b/next.config.js index 29c284d..05c7cb6 100644 --- a/next.config.js +++ b/next.config.js @@ -4,7 +4,7 @@ module.exports = { distDir: '../.next/build', webpack: (config, { dev }) => { - config.plugins.push(new EnvironmentPlugin(['NODE_ENV', 'GRAPH_PROXY'])); + config.plugins.push(new EnvironmentPlugin(['NODE_ENV'])); if (dev) { config.module.rules.push({ diff --git a/src/apollo/client.js b/src/apollo/client.js index 1b16cad..f7510a4 100644 --- a/src/apollo/client.js +++ b/src/apollo/client.js @@ -5,24 +5,26 @@ import fetch from 'isomorphic-unfetch'; import withApollo from './WithApollo'; const headers = {}; -const { GRAPH_PROXY } = process.env; -const config = { - link: ApolloLink.from([ - onError(({ graphQLErrors, networkError }) => { - if (graphQLErrors) { +const config = (req) => { + const host = (req) ? req.ROOT_URI : ''; + return { + link: ApolloLink.from([ + onError(({ graphQLErrors, networkError }) => { + if (graphQLErrors) { + // eslint-disable-next-line no-console + graphQLErrors.map(({ message, locations, path }) => console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)); + } // eslint-disable-next-line no-console - graphQLErrors.map(({ message, locations, path }) => console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)); - } - // eslint-disable-next-line no-console - if (networkError) console.error(`[Network error]: ${networkError}`); - }), - new HttpLink({ - uri: `${GRAPH_PROXY}/graph`, - headers, - fetch, - }), - ]), + if (networkError) console.error(`[Network error]: ${networkError}`); + }), + new HttpLink({ + uri: `${host}/graph`, + headers, + fetch, + }), + ]), + }; }; export default withApollo(config); diff --git a/src/apollo/init.js b/src/apollo/init.js index 1228b1d..c7c4d7a 100644 --- a/src/apollo/init.js +++ b/src/apollo/init.js @@ -40,7 +40,7 @@ export default function initApollo(config, initialState, req) { // On the server, ensure a new client is created for every request. // @see https://www.apollographql.com/docs/react/features/server-side-rendering.html#server-initialization if (!process.browser) { - return create(apolloConfig, initialState); + return create(apolloConfig, initialState, req); } // Reuse the same client instance. diff --git a/src/server/index.js b/src/server/index.js index 92e5c79..4090e94 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -2,9 +2,15 @@ const express = require('express'); const helmet = require('helmet'); const routes = require('./routes'); -const { PORT } = process.env; +const { PORT, ROOT_URI } = process.env; + +const applyProxy = (req, res, next) => { + req.ROOT_URI = ROOT_URI; + next(); +}; const server = express(); +server.use(applyProxy) server.use(helmet()); module.exports = (client) => { diff --git a/src/server/routes/index.js b/src/server/routes/index.js index 321089f..15e0540 100644 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -2,13 +2,13 @@ const proxy = require('http-proxy-middleware'); const index = require('./root'); const story = require('./story'); -const { GRAPH_PROXY } = process.env; +const { ROOT_URI } = process.env; module.exports = (server, client) => { server.use('/', index(client)); server.use('/story', story(client)); - server.use('/graphql', proxy({ - target: `${GRAPH_PROXY}/graph`, + server.use('/graph', proxy({ + target: `${ROOT_URI}/graph`, changeOrigin: true, })); };