-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_server.js
57 lines (47 loc) · 1.83 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
57
import React from 'react';
import docTemplate from './HTML.js';
import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import { Provider } from 'react-redux';
import Helmet from 'react-helmet';
import routes from './routes.js';
import getStore from './store.js';
import { minify } from 'html-minifier';
import { resolve } from './hocs/ss-resolve';
import {selectHTTPResponseCode} from './store.js';
import * as env from './env.js';
export default (req, res, next) => {
match({ routes, location: req.url }, (err, redirect, props) => {
if (err) {
return next(err);
} else if (redirect) {
res.redirect(redirect.pathname + redirect.search)
} else if (props) {
const store = getStore();
resolve(props, store)
.then(() => {
const initialState = store.getState();
const httpStatus = selectHTTPResponseCode(initialState);
const content = renderToString(
<Provider store={store}>
<RouterContext {...props} />
</Provider>
);
res.status(httpStatus).send(
minify(
docTemplate({
...(Helmet.rewind()),
content,
initialState,
env,
base_path: env.APP_WEB_BASE_PATH
}),
{ collapseWhitespace: true, removeAttributeQuotes: true }
)
);
}).catch(next);
} else {
res.status(404).send('Not Found')
}
})
}