forked from roderickhsiao/roderickhsiao.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
160 lines (140 loc) · 4.88 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* global setImmediate */
import React from 'react';
import ReactDOM from 'react-dom/server';
import Debug from 'debug';
import {createElementWithContext as createElement} from 'fluxible-addons-react';
import {get, map} from 'lodash';
import {navigateAction} from 'fluxible-router';
import app from './app';
import bodyParser from 'body-parser';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import csrf from 'csurf';
import express from 'express';
import favicon from 'serve-favicon';
import fetchAllStaticData from './actions/fetchAllStaticData';
import fs from 'fs';
import helmet from 'helmet';
import layout from './configs/layout';
import robots from 'robots.txt';
import routes from './configs/routes';
import serialize from 'serialize-javascript';
import sitemap from 'sitemap';
import spdy from 'spdy';
import HtmlComponent from './components/Html';
import UAParser from 'ua-parser-js';
import {HTTPS} from 'express-sslify';
const debug = Debug('roderickhsiao.me');
const server = express();
const inlineScript = fs.readFileSync('./utils/asyncLoadCSS.js', 'utf-8');
const inlineStyle = fs.readFileSync('./build/css/critial.css', 'utf-8');
const ONE_YEAR = 31556952000;
const IS_PROD = 'production' === process.env.NODE_ENV;
if (IS_PROD) {
server.use(HTTPS({
trustProtoHeader: true,
trustXForwardedHostHeader: true
}));
}
server.use(favicon(__dirname + '/build/images/favicon.ico'));
server.use(bodyParser.json());
server.use(compression({
threshold: '100b'
}));
server.use(cookieParser());
server.use(csrf({cookie: true}));
server.use(helmet());
server.set('state namespace', 'App');
server.use('/', express['static'](__dirname + '/build', {maxAge: ONE_YEAR}));
server.use('/', express['static'](__dirname + '/build/images', {maxAge: ONE_YEAR}));
server.use('/', express['static'](__dirname + '/build/js', {maxAge: ONE_YEAR}));
server.use('/', express['static'](__dirname + '/build/css', {maxAge: ONE_YEAR}));
server.use(robots(__dirname + '/robots.txt'));
// Get access to the fetchr plugin instance
let fetchrPlugin = app.getPlugin('FetchrPlugin');
fetchrPlugin.registerService(require('./services/fetchStaticData'));
// Set up the fetchr middleware
server.use(fetchrPlugin.getXhrPath(), fetchrPlugin.getMiddleware());
// sitemap
let routeConfig = map(routes, (route) => {
return { url: route.path };
});
let sm = sitemap.createSitemap({
hostname: 'https://www.roderickhsiao.me',
cacheTime: 600000,
urls: routeConfig
});
server.get('/sitemap.xml', (req, res) => {
sm.toXML((err, xml) => {
if (err) {
return res.status(500).end();
}
res.header('Content-Type', 'application/xml');
res.send(xml);
});
});
function renderPage(req, res, context) {
debug('Exposing context state');
let customContext = {
ua: new UAParser().setUA(req.headers['user-agent']).getResult()
};
const exposed = 'window.App=' + serialize(app.dehydrate(context)) + ';';
debug('Rendering Application component into html');
const html = ReactDOM.renderToStaticMarkup(
React.createElement(HtmlComponent, {
state: exposed,
markup: ReactDOM.renderToString(createElement(context, customContext)),
context: context.getComponentContext(),
inlineScript: inlineScript,
inlineStyle: inlineStyle
}
));
debug('Sending markup');
res.send(html);
}
server.use((req, res, next) => {
var context = app.createContext({
req: req, // The fetchr plugin depends on this
xhrContext: {
_csrf: req.csrfToken() // Make sure all XHR requests have the CSRF token
}
});
context.executeAction(navigateAction, { url: req.url, type: 'pageload' }, (err) => {
if (err) {
if (err.statusCode && err.statusCode === 404) {
next();
} else {
next(err);
}
return;
}
res.setHeader('Cache-Control', 'must_revalidate, public, max-age=3600');
let routeStore = context.getStore('RouteStore');
let {name} = routeStore.getCurrentRoute();
let prefetch = get(layout, [name, 'prefetch']);
if (!prefetch || !prefetch.length) {
renderPage(req, res, context);
} else {
setImmediate(()=> {
context.executeAction(fetchAllStaticData, {resources: prefetch}, () => {
renderPage(req, res, context);
});
});
}
});
});
var port = process.env.PORT || 3000;
var expressApp = server;
if (IS_PROD) {
spdy.createServer({
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
}, server);
}
expressApp.listen(port, (err) => {
if (err) {
console.error(error);
return process.exit(1);
}
console.log('Listening on port', port);
})