/
serve.js
46 lines (40 loc) · 1.02 KB
/
serve.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
import through from 'through';
import { renderToNodeStream } from 'react-dom/server';
import { renderStylesToNodeStream } from 'emotion-server';
import { getDataFromTree } from 'react-apollo';
// eslint-disable-next-line
import template from 'server/template';
import injectStyles from 'styles/inject';
export default async (req, res) => {
const { app, client, stylesheets = [], assets = {} } = res.locals;
try {
injectStyles();
await getDataFromTree(app);
} catch (e) {
// eslint-disable-next-line
console.log(e);
}
const state = client.cache.extract();
const [header, footer] = template({
helmet: res.locals.helmetContext.helmet,
stylesheets,
state,
assets,
});
res.status(200);
res.write(header);
renderToNodeStream(app)
.pipe(renderStylesToNodeStream())
.pipe(
through(
function write(data) {
this.queue(data);
},
function end() {
this.queue(footer);
this.queue(null);
}
)
)
.pipe(res);
};