-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathisomorphic_render.js
46 lines (40 loc) · 1.83 KB
/
isomorphic_render.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
// client-server module
import React from 'react/addons';
import render from './flux/render.js';
import {serialize, deserialize} from 'utils/serialize.js';
// i place both realization of render in one file
// in oreder to show that there are alomost no difference
// between server and client rendering
export default function isomorphicRender({isClient, serverPath}) {
if (isClient) {
if (__DEV__) {
window.React = React; // for devtools
}
const initialState = window.K_SERIALIZED_DATA && deserialize(window.K_SERIALIZED_DATA) || undefined;
// client: serverPath = undefined;
return render({React, initialState /*, serverPath*/})
.then(
({component /* client: no need in filled initialState */}) =>
React.render(component, document.getElementById('react_main')), // ServDiff
(err) => {
console.error('app.js error', err); // eslint-disable-line no-console
throw err;
}
);
}
// -------------------------------------------------------------------------
// ------------------------===<<< SERVER RENDERING >>>===---------------
// -------------------------------------------------------------------------
return render({React, serverPath /* server: no initialState on the server, server must prepare it for client*/})
.then(
({component, initialState}) => ({
html: React.renderToString(component), // server: render to string not Dom
initialState: serialize(initialState) // server: initialState at this moment is filled with data, so with html we also return initialState
// find this code `multiActionMiddleware({wait: isServerCall})` to see
}),
(err) => {
console.error('app.js error', err); // eslint-disable-line no-console
throw err;
}
);
}