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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 | 2x
2x
2x
2x
2x
2x
2x
2x
2x
8x
8x
8x
8x
8x
7x
7x
6x
3x
2x
8x
8x
8x
8x
8x
8x
1x
7x
7x
7x
7x
7x
7x
1x
7x
7x
7x
7x
6x
6x
2x
2x
2x
2x
2x
2x | var path = require("path");
var Provider = require("react-redux").Provider;
var WrapperProvider = require("./wrapper").WrapperProvider;
var renderToString = require("react-dom/server").renderToString;
var React = require("react");
var ReactRouter = require("react-router");
var match = ReactRouter.match;
var RouterContext = ReactRouter.RouterContext;
function sendError(config, options) {
config.res
.status(config.code)
.send(options.errorTemplate(config));
}
function waitForTemplate(options) {
return (new Promise(function(resolve) {
var interval = setInterval(function() {
Eif (options.fs.existsSync(options.templatePath)) {
clearInterval(interval);
resolve(options.fs.readFileSync(options.templatePath).toString());
}
}, 200);
}));
}
function renderFullPage(config, options) {
var parsedTemplate = options.template({
component: config.component,
html: config.html,
initialProps: config.initialProps,
store: config.store,
req: config.req,
res: config.res,
template: config.template
});
if (typeof parsedTemplate !== 'string') throw new Error('Return type of options.template() has to be a string');
return parsedTemplate.replace(
'<head>', // this should be the first script on a page so that others can pick it up
'<head>' +
'<script type="text/javascript">window["' + options.initialStateKey + '"] = ' + JSON.stringify(config.store.getState()) + ';</script>' +
'<script type="text/javascript">window["' + options.initialPropsKey + '"] = ' + JSON.stringify(config.initialProps) + ';</script>'
);
}
function errorTemplate(config) {
if (config.html) return config.html;
return (
"<h1>" + config.code + " Server Error</h1>" +
"<h2>" + (config.error.message || config.error) + "</h2>" +
"<pre>" + (config.error.stack || config.error) + "</pre>"
);
}
function middleware(options, routes, history, templatePromise, req, res, next) {
var location = history.createLocation(req.url);
var store = options.createStore({req: req, res: res});
Iif (
options.fs.existsSync(path.join(options.outputPath, location.pathname)) &&
!~options.ignoreUrls.indexOf(location.pathname)
) {
if (options.debug) console.log('Static', location.pathname);
return next();
}
match({routes: routes, location: location}, function(error, redirectLocation, renderProps) {
Eif (options.debug) console.log('Rendering', location.pathname + location.search);
if (redirectLocation) {
return res.redirect(301, redirectLocation.pathname + redirectLocation.search);
}
Iif (error) {
//TODO Find how to test
return sendError({
code: 502,
error: error,
req: req,
res: res
}, options);
}
Iif (renderProps === null) {
//TODO Find how to test
return templatePromise.then(function(template) {
sendError({
code: 404,
html: renderFullPage({
template: template,
req: req,
res: res,
component: null,
initialProps: null,
html: '',
store: store
}, options),
req: req,
res: res
}, options);
});
}
new Promise(function(resolve) {
var Cmp = renderProps.components[renderProps.components.length - 1]; // it used to be .WrappedComponent but it's useless, users can find it themselves if needed
// console.log('Found Component', Cmp.getInitialProps);
function getInitialProps() {
return new Promise(function(resolve) {
resolve((Cmp && Cmp.getInitialProps) ? Cmp.getInitialProps({
location: renderProps.location,
params: renderProps.params,
query: renderProps.query,
req: req,
res: res,
store: store
}) : null);
}).catch(function(e) {
// console.log('Recovering after getInitialProps error', e);
return {initialError: e.message || e.toString()};
});
}
resolve(Promise.all([
Cmp,
getInitialProps(),
templatePromise
]));
}).then(function(result) {
var Cmp = result[0];
var initialProps = result[1];
// console.log('Setting context initial props', initialProps);
// console.log('Store state before rendering', store.getState());
var html = renderToString(
React.createElement(
WrapperProvider,
{initialProps: initialProps},
React.createElement(
Provider,
{store: store},
React.createElement(RouterContext, renderProps)
)
)
);
res.status(Cmp && !Cmp.notFound ? 200 : 404);
return renderFullPage({
template: result[2],
component: Cmp,
html: html,
initialProps: initialProps,
req: req,
res: res,
store: store
}, options);
}).then(res.send.bind(res)).catch(function(e) {
return sendError({
code: 500,
error: e,
req: req,
res: res
}, options);
});
});
}
exports.sendError = sendError;
exports.middleware = middleware;
exports.errorTemplate = errorTemplate;
exports.renderFullPage = renderFullPage;
exports.waitForTemplate = waitForTemplate; |