-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.js
More file actions
138 lines (122 loc) · 4.08 KB
/
server.js
File metadata and controls
138 lines (122 loc) · 4.08 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import Helmet from 'react-helmet';
import Html from './components/html';
let match;
let RouterContext;
let Provider;
let configureStore;
let routes;
let NotFoundComponent;
if (!process.env.DISABLE_UNIVERSAL_RENDERING) {
match = require('react-router').match;
RouterContext = require('react-router').RouterContext;
Provider = require('react-redux').Provider;
configureStore = require('./store/configureStore').default;
routes = require('./routes').default;
NotFoundComponent = require('./routes').NotFoundComponent;
}
const DOCTYPE = '<!doctype html>';
function fetchComponentData(renderProps, store) {
const requests = renderProps.components
// filter undefined values
.filter(component => component)
.map(component => {
// Handle `connect`ed components.
if (component.WrappedComponent) {
component = component.WrappedComponent;
}
if (component.fetchData) {
const { query, params, history } = renderProps;
return component.fetchData({
dispatch: store.dispatch,
query,
params,
history
})
// Make sure promise always successfully resolves
.catch(() => {});
}
});
return Promise.all(requests);
}
function isNotFound(renderProps) {
return !renderProps || renderProps.components
.some(component => component === NotFoundComponent);
}
function getJsFromStats(stats) {
return stats.assetsByChunkName.client.find(asset => /\.js$/.test(asset));
}
function getCssFromStats(stats) {
return stats.assetsByChunkName.client.find(asset => /\.css$/.test(asset));
}
function renderApp(renderProps, store) {
const html = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);
const state = store.getState();
return [html, state];
}
function render(stats, html = '', state) {
const js = getJsFromStats(stats);
const css = getCssFromStats(stats);
const head = Helmet.rewind();
return renderToStaticMarkup(
<Html
js={js && `/${js}`}
css={css && `/${css}`}
html={html}
head={head}
initialState={state} />
);
}
/**
* Express middleware to render HTML using react-router
* @param {object} stats Webpack stats output
* @return {function} middleware function
*/
export default (stats) => {
/**
* @param {object} req Express request object
* @param {object} res Express response object
* @return {undefined} undefined
*/
return (req, res, next) => {
if (process.env.DISABLE_UNIVERSAL_RENDERING) {
let html;
try {
html = render(stats);
} catch (ex) {
return next(ex);
}
return res.status(200)
.send(`${DOCTYPE}${html}`);
}
match({
routes,
location: req.url
}, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, `${redirectLocation.pathname}${redirectLocation.search}`);
} else {
const store = configureStore();
fetchComponentData(renderProps, store)
.then(() => {
let html;
try {
const [markup, state] = renderApp(renderProps, store);
html = render(stats, markup, state);
} catch (ex) {
return next(ex);
}
res.status(isNotFound(renderProps) ? 404 : 200)
.send(`${DOCTYPE}${html}`);
});
}
});
};
};