-
Notifications
You must be signed in to change notification settings - Fork 0
/
homepage.js
50 lines (44 loc) · 1.27 KB
/
homepage.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
47
48
49
50
'use strict';
require('babel-register')({
babelrc: false,
presets: ['es2015', 'react'],
plugins: ['css-modules-transform'],
only: 'src'
});
const l = require('./level');
const bundles = require('./config').bundles;
// client deps
const React = require('react');
const createStore = require('redux').createStore;
const Provider = require('react-redux').Provider;
const renderToString = require('react-dom/server').renderToString;
const App = require('../src/app').default;
const todoApp = require('../src/reducers').todoApp;
function homepage () {
const initialState = l.getState().catch(l.NotFoundError, () => undefined);
return initialState.then(templateHome);
}
module.exports = homepage;
// templating
function templateHome (initialState) {
console.log(initialState);
const store = createStore(todoApp, initialState);
const html = renderToString(
React.createElement(Provider, { store },
React.createElement(App)
)
);
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="container">${html}</div>
<script>window.STATE_FROM_SERVER = ${JSON.stringify(initialState)}</script>
<script src="/build/${bundles.vendor}"></script>
<script src="/build/${bundles.js}"></script>
</body>
</html>`;
}