|
@@ -0,0 +1,201 @@
|
|
1
|
+'use strict';
|
|
2
|
+
|
|
3
|
+Object.defineProperty(exports, "__esModule", {
|
|
4
|
+ value: true
|
|
5
|
+});
|
|
6
|
+
|
|
7
|
+let renderFragment = (() => {
|
|
8
|
+ var _ref = _asyncToGenerator(function* (createApp, routes, url, initialState, timeout, verbose) {
|
|
9
|
+ const history = (0, _history.createMemoryHistory)();
|
|
10
|
+ history.push(url);
|
|
11
|
+ const context = {};
|
|
12
|
+ const app = createApp({
|
|
13
|
+ history,
|
|
14
|
+ initialState
|
|
15
|
+ });
|
|
16
|
+ if (!existSSRModel(app)) {
|
|
17
|
+ app.model(_ssrModel2.default);
|
|
18
|
+ }
|
|
19
|
+ app.router(function (options) {
|
|
20
|
+ return _react2.default.createElement(
|
|
21
|
+ _reactRouter.StaticRouter,
|
|
22
|
+ { location: url, context: options.context },
|
|
23
|
+ _react2.default.createElement(
|
|
24
|
+ 'div',
|
|
25
|
+ null,
|
|
26
|
+ routes
|
|
27
|
+ )
|
|
28
|
+ );
|
|
29
|
+ });
|
|
30
|
+ const asyncActions = getAsyncActions(app);
|
|
31
|
+ const branch = (0, _utils.findRouteByUrl)(routes, url);
|
|
32
|
+ if (branch.length === 0) {
|
|
33
|
+ return {};
|
|
34
|
+ }
|
|
35
|
+ const sync = findSync(branch);
|
|
36
|
+ if (!sync && asyncActions && asyncActions.length > 0) {
|
|
37
|
+ const id = (0, _uid2.default)(10);
|
|
38
|
+ app.use((0, _dvaServerSync2.default)(id, function (action) {
|
|
39
|
+ if (asyncActions.indexOf(action.type) > -1) {
|
|
40
|
+ return true;
|
|
41
|
+ }
|
|
42
|
+ return false;
|
|
43
|
+ }, _block2.default));
|
|
44
|
+ const appDOM = app.start()({
|
|
45
|
+ context
|
|
46
|
+ });
|
|
47
|
+ if (verbose) {
|
|
48
|
+ console.time(`${url}: async wait time`);
|
|
49
|
+ }
|
|
50
|
+ const result = yield new Promise(function (resolve, reject) {
|
|
51
|
+ const timer = setTimeout(function () {
|
|
52
|
+ reject(new Error('render timeout'));
|
|
53
|
+ }, timeout);
|
|
54
|
+ _block2.default.wait(id, function () {
|
|
55
|
+ if (verbose) {
|
|
56
|
+ console.timeEnd(`${url}: async wait time`);
|
|
57
|
+ }
|
|
58
|
+ clearTimeout(timer);
|
|
59
|
+ const curState = appDOM.props.store.getState();
|
|
60
|
+ const html = (0, _server.renderToStaticMarkup)(appDOM);
|
|
61
|
+ resolve({ html, state: curState, context });
|
|
62
|
+ });
|
|
63
|
+ });
|
|
64
|
+ return result;
|
|
65
|
+ }
|
|
66
|
+ const appDOM = app.start()({
|
|
67
|
+ context
|
|
68
|
+ });
|
|
69
|
+ const html = (0, _server.renderToStaticMarkup)(appDOM);
|
|
70
|
+ const curState = appDOM.props.store.getState();
|
|
71
|
+ return { html, state: curState, context };
|
|
72
|
+ });
|
|
73
|
+
|
|
74
|
+ return function renderFragment(_x, _x2, _x3, _x4, _x5, _x6) {
|
|
75
|
+ return _ref.apply(this, arguments);
|
|
76
|
+ };
|
|
77
|
+})();
|
|
78
|
+
|
|
79
|
+var _react = require('react');
|
|
80
|
+
|
|
81
|
+var _react2 = _interopRequireDefault(_react);
|
|
82
|
+
|
|
83
|
+var _uid = require('uid');
|
|
84
|
+
|
|
85
|
+var _uid2 = _interopRequireDefault(_uid);
|
|
86
|
+
|
|
87
|
+var _reactRouter = require('react-router');
|
|
88
|
+
|
|
89
|
+var _history = require('history');
|
|
90
|
+
|
|
91
|
+var _lodash = require('lodash.merge');
|
|
92
|
+
|
|
93
|
+var _lodash2 = _interopRequireDefault(_lodash);
|
|
94
|
+
|
|
95
|
+var _server = require('react-dom/server');
|
|
96
|
+
|
|
97
|
+var _ssrModel = require('./ssrModel');
|
|
98
|
+
|
|
99
|
+var _ssrModel2 = _interopRequireDefault(_ssrModel);
|
|
100
|
+
|
|
101
|
+var _utils = require('./utils');
|
|
102
|
+
|
|
103
|
+var _dvaServerSync = require('./dvaServerSync');
|
|
104
|
+
|
|
105
|
+var _dvaServerSync2 = _interopRequireDefault(_dvaServerSync);
|
|
106
|
+
|
|
107
|
+var _block = require('./block');
|
|
108
|
+
|
|
109
|
+var _block2 = _interopRequireDefault(_block);
|
|
110
|
+
|
|
111
|
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
112
|
+
|
|
113
|
+function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
|
114
|
+
|
|
115
|
+function existSSRModel(app) {
|
|
116
|
+ try {
|
|
117
|
+ let model = null;
|
|
118
|
+ app._models.forEach(m => {
|
|
119
|
+ if (m.namespace === 'ssr') {
|
|
120
|
+ model = m;
|
|
121
|
+ }
|
|
122
|
+ });
|
|
123
|
+ return !!model;
|
|
124
|
+ } catch (e) {
|
|
125
|
+ return false;
|
|
126
|
+ }
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+function getAsyncActions(app) {
|
|
130
|
+ try {
|
|
131
|
+ let actions = [];
|
|
132
|
+ app._models.forEach(model => {
|
|
133
|
+ if (model.effects) {
|
|
134
|
+ actions = actions.concat(Object.keys(model.effects));
|
|
135
|
+ }
|
|
136
|
+ });
|
|
137
|
+ return actions;
|
|
138
|
+ } catch (e) {
|
|
139
|
+ return [];
|
|
140
|
+ }
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+function findSync(branch) {
|
|
144
|
+ let sync = false;
|
|
145
|
+ branch.forEach(b => {
|
|
146
|
+ sync = !!b.props.sync;
|
|
147
|
+ });
|
|
148
|
+ return sync;
|
|
149
|
+}
|
|
150
|
+
|
|
151
|
+exports.default = (() => {
|
|
152
|
+ var _ref2 = _asyncToGenerator(function* ({
|
|
153
|
+ url, env, routes, renderFullPage, createApp, initialState, onRenderSuccess, timeout = 6000, verbose = true
|
|
154
|
+ }) {
|
|
155
|
+ try {
|
|
156
|
+ if (verbose) {
|
|
157
|
+ console.log(`[${url}]`);
|
|
158
|
+ console.time(`${url}: render time`);
|
|
159
|
+ }
|
|
160
|
+ const state = (0, _lodash2.default)({}, initialState || {}, {
|
|
161
|
+ ssr: {
|
|
162
|
+ env
|
|
163
|
+ }
|
|
164
|
+ });
|
|
165
|
+ const fragment = yield renderFragment(createApp, routes, url, state, timeout, verbose);
|
|
166
|
+ if (verbose) {
|
|
167
|
+ console.timeEnd(`${url}: render time`);
|
|
168
|
+ }
|
|
169
|
+ const context = fragment.context;
|
|
170
|
+ if (!context) {
|
|
171
|
+ return { code: 404, url, env };
|
|
172
|
+ } else if (context.url) {
|
|
173
|
+ return {
|
|
174
|
+ code: 302, url, env, redirect: context.url
|
|
175
|
+ };
|
|
176
|
+ }
|
|
177
|
+ const html = yield renderFullPage(fragment);
|
|
178
|
+ if (onRenderSuccess) {
|
|
179
|
+ yield onRenderSuccess({
|
|
180
|
+ html, url, env, state: fragment.state
|
|
181
|
+ });
|
|
182
|
+ }
|
|
183
|
+ return {
|
|
184
|
+ code: 200, url, env, html
|
|
185
|
+ };
|
|
186
|
+ } catch (e) {
|
|
187
|
+ console.error(e);
|
|
188
|
+ return {
|
|
189
|
+ code: 500, url, env, error: e
|
|
190
|
+ };
|
|
191
|
+ }
|
|
192
|
+ });
|
|
193
|
+
|
|
194
|
+ function render(_x7) {
|
|
195
|
+ return _ref2.apply(this, arguments);
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ return render;
|
|
199
|
+})();
|
|
200
|
+
|
|
201
|
+module.exports = exports['default'];
|