forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (74 loc) · 2.09 KB
/
index.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
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
import Rx from 'rx';
import debug from 'debug';
import { render } from 'redux-epic';
import createHistory from 'history/createBrowserHistory';
import useLangRoutes from './utils/use-lang-routes';
import sendPageAnalytics from './utils/send-page-analytics';
import { App, createApp, provideStore } from '../common/app';
import { getLangFromPath } from '../common/app/utils/lang';
// client specific epics
import epics from './epics';
import {
isColdStored,
getColdStorage,
saveToColdStorage
} from './cold-reload';
const isDev = Rx.config.longStackSupport = debug.enabled('fcc:*');
const log = debug('fcc:client');
const hotReloadTimeout = 2000;
const {
devToolsExtension,
location,
history: _history,
document,
ga,
__fcc__: {
data: ssrState = {},
csrf: {
token: csrfToken
} = {}
}
} = window;
const epicOptions = {
isDev,
window,
document,
location,
history: _history
};
const DOMContainer = document.getElementById('fcc');
const defaultState = isColdStored() ?
getColdStorage() :
ssrState;
const primaryLang = getLangFromPath(location.pathname);
defaultState.app.csrfToken = csrfToken;
const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } };
const history = useLangRoutes(createHistory, primaryLang)();
sendPageAnalytics(history, ga);
createApp({
history,
serviceOptions,
defaultState,
epics,
epicOptions,
enhancers: isDev && devToolsExtension && [ devToolsExtension() ]
})
.doOnNext(({ store }) => {
if (module.hot && typeof module.hot.accept === 'function') {
module.hot.accept(() => {
// note(berks): not sure this ever runs anymore after adding
// RHR?
log('saving state and refreshing.');
log('ignore react ssr warning.');
saveToColdStorage(store.getState());
setTimeout(() => location.reload(), hotReloadTimeout);
});
}
})
.do(() => log('rendering'))
.flatMap(({ store }) => render(provideStore(App, store), DOMContainer))
.subscribe(
() => debug('react rendered'),
err => { throw err; },
() => debug('react closed subscription')
);