This repository was archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathserver.js
More file actions
152 lines (124 loc) · 6.04 KB
/
server.js
File metadata and controls
152 lines (124 loc) · 6.04 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*-------------------------------------------------------------------------------------------------------------------*\
| Copyright (C) 2015 PayPal |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| with the License. |
| |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed |
| on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for |
| the specific language governing permissions and limitations under the License. |
\*-------------------------------------------------------------------------------------------------------------------*/
'use strict';
var path = require('path');
var util = require('./util');
var assert = require('assert');
var Config = require('./config');
var format = require('util').format;
var Performance = require('./performance');
var omit = require('lodash-node/compat/object/omit');
var merge = require('lodash-node/compat/object/merge');
// safely require the peer-dependencies
var React = util.safeRequire('react');
var Router = util.safeRequire('react-router');
// a template of the `script` tag that gets
// injected into the server rendered pages.
var TEMPLATE = ['<script id="%s" type="application/javascript">var ',
Config.client.variableName,
' = %s;</script>'
].join('');
var TEMPLATE_WITH_NONCE = ['<script id="%s" nonce="%s" type="application/javascript">var ',
Config.client.variableName,
' = %s;</script>'
].join('');
exports.create = function create(createOptions) {
createOptions = createOptions || {};
if (createOptions.performanceCollector) {
assert.equal(typeof createOptions.performanceCollector,
'function',
'`performanceCollector` - should be a function');
}
// the render implementation
return function render(thing, options, callback) {
var routes;
var perfInstance;
if (createOptions.performanceCollector) {
perfInstance = Performance(thing);
}
function done(err, html) {
if (options.settings.env === 'development') {
// remove all the files under the express's view folder from require cache.
// Helps in making changes to react views without restarting the server.
util.clearRequireCache(createOptions.reactRoutes);
util.clearRequireCacheInDir(options.settings.views, options.settings['view engine']);
}
if (createOptions.performanceCollector) {
createOptions.performanceCollector(perfInstance());
}
callback(err, html);
}
if (createOptions.reactRoutes) {
routes = require(createOptions.reactRoutes);
}
// initialize the markup string
var html = Config.docType;
// create the data object that will be fed into the React render method.
// Data is a mash of the express' `render options` and `res.locals`
// and meta info about `react-engine`
var data = merge({
__meta: {
// get just the relative path for view file name
view: null,
markupId: Config.client.markupId
}
}, omit(options, ['settings', 'enrouten', '_locals']));
if (this.useRouter && !routes) {
return done(new Error('asking to use react router for rendering, but no routes are provided'));
}
var componentInstance;
try {
if (this.useRouter) {
// runs the react router that gives the Component to render
Router.run(routes, thing, function onRouterRun(Component) {
componentInstance = React.createElement(Component, data);
});
}
else {
// path utility to make path string compatible in different OS
// ------------------------------------------------------------
// use `path.normalize()` to normalzie absolute view file path and absolute base directory path
// to prevent path strings like `/folder1/folder2/../../folder3/exampleFile`
// then, derive relative view file path
// and replace backslash with slash to be compatible on Windows
data.__meta.view = path.normalize(thing)
.replace(path.normalize(options.settings.views), '').substring(1)
.replace('\\', '/');
var view = require(thing);
// create the Component using react's createFactory
var component = React.createFactory(view);
componentInstance = component(data);
}
// render the componentInstance
html += React.renderToString(componentInstance);
// state (script) injection
//add nonce to the script tag for unsafe-inline;
var script;
if (options.nonce) {
script = format(TEMPLATE_WITH_NONCE, Config.client.markupId,
options.nonce, JSON.stringify(data));
} else {
script = format(TEMPLATE, Config.client.markupId, JSON.stringify(data));
}
html = html.replace('</body>', script + '</body>');
return done(null, html);
}
catch (err) {
// on error, pass to the next
// middleware in the chain!
return done(err);
}
};
};