Skip to content

Instantly share code, notes, and snippets.

@takanorip
Last active December 10, 2018 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takanorip/4db791390672accb5eb0770a22635334 to your computer and use it in GitHub Desktop.
Save takanorip/4db791390672accb5eb0770a22635334 to your computer and use it in GitHub Desktop.
// react-router v4 を想定したサンプルですが
// その他のルーターでも動作します
import { ApolloProvider, getDataFromTree } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import Express from 'express';
import { StaticRouter } from 'react-router';
import { InMemoryCache } from "apollo-cache-inmemory";
import Layout from './routes/Layout';
const app = new Express();
app.use((req, res) => {
const client = new ApolloClient({
ssrMode: true,
link: createHttpLink({
uri: 'http://localhost:3010',
credentials: 'same-origin',
headers: {
cookie: req.header('Cookie'),
},
}),
cache: new InMemoryCache(),
});
const context = {};
const App = (
<ApolloProvider client={client}>
<StaticRouter location={req.url} context={context}>
<Layout />
</StaticRouter>
</ApolloProvider>
);
// 後述のレンダリング用コードを書く
});
app.listen(basePort, () => console.log( // eslint-disable-line no-console
`app Server is now running on http://localhost:${basePort}`
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment