forked from EddiG/ssr-cra-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (51 loc) · 1.59 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import 'isomorphic-fetch';
import path from 'path';
import express from 'express';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StaticRouter as Router } from 'react-router-dom';
import Helmet from 'react-helmet';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import {
ApolloProvider,
getDataFromTree,
} from 'react-apollo';
import Html from './Html';
import App from './src/App';
import assets from './assets';
const app = express();
app.use(express.static(path.resolve(__dirname, '../build'), { index: false }));
app.use(async (req, res) => {
const client = new ApolloClient({
ssrMode: true,
link: new HttpLink({
uri: 'https://m5j9784k8j.sse.codesandbox.io',
}),
cache: new InMemoryCache(),
});
const app = (
<ApolloProvider client={client}>
<Router location={req.url} context={{}}>
<App />
</Router>
</ApolloProvider>
);
// Executes all graphql queries for the current state of application
await getDataFromTree(app);
// Extracts apollo client cache
const state = client.extract();
const content = ReactDOMServer.renderToStaticMarkup(app);
const helmet = Helmet.renderStatic();
const html = ReactDOMServer.renderToStaticMarkup(
<Html content={content} helmet={helmet} assets={assets} state={state} />,
);
res.status(200);
res.send(`<!doctype html>${html}`);
res.end();
});
const port = process.env.PORT || 8888;
app.listen(port, () => {
console.log(`Server listening on ${port} port`);
});