|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+import React from 'react';
|
|
|
2
|
+import uid from 'uid';
|
|
|
3
|
+import { StaticRouter } from 'react-router';
|
|
|
4
|
+import { createMemoryHistory } from 'history';
|
|
|
5
|
+import merge from 'lodash.merge';
|
|
|
6
|
+import { renderToStaticMarkup } from 'react-dom/server';
|
|
|
7
|
+import ssrModel from './ssrModel';
|
|
|
8
|
+import { findRouteByUrl } from './utils';
|
|
|
9
|
+import dvaServerSync from './dvaServerSync';
|
|
|
10
|
+import block from './block';
|
|
|
11
|
+
|
|
|
12
|
+function existSSRModel(app) {
|
|
|
13
|
+ try {
|
|
|
14
|
+ let model = null;
|
|
|
15
|
+ app._models.forEach((m) => {
|
|
|
16
|
+ if (m.namespace === 'ssr') {
|
|
|
17
|
+ model = m;
|
|
|
18
|
+ }
|
|
|
19
|
+ });
|
|
|
20
|
+ return !!model;
|
|
|
21
|
+ } catch (e) {
|
|
|
22
|
+ return false;
|
|
|
23
|
+ }
|
|
|
24
|
+}
|
|
|
25
|
+
|
|
|
26
|
+function getAsyncActions(app) {
|
|
|
27
|
+ try {
|
|
|
28
|
+ let actions = [];
|
|
|
29
|
+ app._models.forEach((model) => {
|
|
|
30
|
+ if (model.effects) {
|
|
|
31
|
+ actions = actions.concat(Object.keys(model.effects));
|
|
|
32
|
+ }
|
|
|
33
|
+ });
|
|
|
34
|
+ return actions;
|
|
|
35
|
+ } catch (e) {
|
|
|
36
|
+ return [];
|
|
|
37
|
+ }
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+function findSync(branch) {
|
|
|
41
|
+ let sync = false;
|
|
|
42
|
+ branch.forEach((b) => {
|
|
|
43
|
+ sync = !!b.props.sync;
|
|
|
44
|
+ });
|
|
|
45
|
+ return sync;
|
|
|
46
|
+}
|
|
|
47
|
+
|
|
|
48
|
+async function renderFragment(createApp, routes, url, initialState, timeout, verbose) {
|
|
|
49
|
+ const history = createMemoryHistory();
|
|
|
50
|
+ history.push(url);
|
|
|
51
|
+ const context = {};
|
|
|
52
|
+ const app = createApp({
|
|
|
53
|
+ history,
|
|
|
54
|
+ initialState,
|
|
|
55
|
+ });
|
|
|
56
|
+ if (!existSSRModel(app)) {
|
|
|
57
|
+ app.model(ssrModel);
|
|
|
58
|
+ }
|
|
|
59
|
+ app.router(options => (<StaticRouter location={url} context={options.context}>
|
|
|
60
|
+ <div>
|
|
|
61
|
+ {routes}
|
|
|
62
|
+ </div>
|
|
|
63
|
+ </StaticRouter>));
|
|
|
64
|
+ const asyncActions = getAsyncActions(app);
|
|
|
65
|
+ const branch = findRouteByUrl(routes, url);
|
|
|
66
|
+ if (branch.length === 0) {
|
|
|
67
|
+ return {};
|
|
|
68
|
+ }
|
|
|
69
|
+ const sync = findSync(branch);
|
|
|
70
|
+ if (!sync && asyncActions && asyncActions.length > 0) {
|
|
|
71
|
+ const id = uid(10);
|
|
|
72
|
+ app.use(dvaServerSync(id, (action) => {
|
|
|
73
|
+ if (asyncActions.indexOf(action.type) > -1) {
|
|
|
74
|
+ return true;
|
|
|
75
|
+ }
|
|
|
76
|
+ return false;
|
|
|
77
|
+ }, block));
|
|
|
78
|
+ const appDOM = app.start()({
|
|
|
79
|
+ context,
|
|
|
80
|
+ });
|
|
|
81
|
+ if (verbose) {
|
|
|
82
|
+ console.time(`${url}: async wait time`);
|
|
|
83
|
+ }
|
|
|
84
|
+ const result = await new Promise((resolve, reject) => {
|
|
|
85
|
+ const timer = setTimeout(() => {
|
|
|
86
|
+ reject(new Error('render timeout'));
|
|
|
87
|
+ }, timeout)
|
|
|
88
|
+ block.wait(id, () => {
|
|
|
89
|
+ if (verbose) {
|
|
|
90
|
+ console.timeEnd(`${url}: async wait time`);
|
|
|
91
|
+ }
|
|
|
92
|
+ clearTimeout(timer);
|
|
|
93
|
+ const curState = appDOM.props.store.getState();
|
|
|
94
|
+ const html = renderToStaticMarkup(appDOM);
|
|
|
95
|
+ resolve({ html, state: curState, context });
|
|
|
96
|
+ });
|
|
|
97
|
+ });
|
|
|
98
|
+ return result;
|
|
|
99
|
+ }
|
|
|
100
|
+ const appDOM = app.start()({
|
|
|
101
|
+ context,
|
|
|
102
|
+ });
|
|
|
103
|
+ const html = renderToStaticMarkup(appDOM);
|
|
|
104
|
+ const curState = appDOM.props.store.getState();
|
|
|
105
|
+ return { html, state: curState, context };
|
|
|
106
|
+}
|
|
|
107
|
+
|
|
|
108
|
+export default async function render({
|
|
|
109
|
+ url, env, routes, renderFullPage, createApp, initialState, onRenderSuccess, timeout = 6000, verbose = true
|
|
|
110
|
+}) {
|
|
|
111
|
+ try {
|
|
|
112
|
+ if (verbose) {
|
|
|
113
|
+ console.log(`[${url}]`)
|
|
|
114
|
+ console.time(`${url}: render time`);
|
|
|
115
|
+ }
|
|
|
116
|
+ const state = merge({}, initialState || {}, {
|
|
|
117
|
+ ssr: {
|
|
|
118
|
+ env,
|
|
|
119
|
+ }
|
|
|
120
|
+ });
|
|
|
121
|
+ const fragment = await renderFragment(createApp, routes, url, state, timeout, verbose);
|
|
|
122
|
+ if (verbose) {
|
|
|
123
|
+ console.timeEnd(`${url}: render time`);
|
|
|
124
|
+ }
|
|
|
125
|
+ const context = fragment.context;
|
|
|
126
|
+ if (!context) {
|
|
|
127
|
+ return { code: 404, url, env };
|
|
|
128
|
+ } else if (context.url) {
|
|
|
129
|
+ return {
|
|
|
130
|
+ code: 302, url, env, redirect: context.url,
|
|
|
131
|
+ };
|
|
|
132
|
+ }
|
|
|
133
|
+ const html = await renderFullPage(fragment);
|
|
|
134
|
+ if (onRenderSuccess) {
|
|
|
135
|
+ await onRenderSuccess({
|
|
|
136
|
+ html, url, env, state: fragment.state,
|
|
|
137
|
+ });
|
|
|
138
|
+ }
|
|
|
139
|
+ return {
|
|
|
140
|
+ code: 200, url, env, html,
|
|
|
141
|
+ };
|
|
|
142
|
+ } catch (e) {
|
|
|
143
|
+ console.error(e);
|
|
|
144
|
+ return {
|
|
|
145
|
+ code: 500, url, env, error: e,
|
|
|
146
|
+ };
|
|
|
147
|
+ }
|
|
|
148
|
+}
|