This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
/
index.js
66 lines (57 loc) · 2.17 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
import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import {
Router,
browserHistory as history,
match
} from 'react-router';
import { Provider } from 'react-redux';
import * as reducers from 'reducers';
import injectStoreAndGetRoutes from 'routes';
import {
createStore,
combineReducers,
applyMiddleware
} from 'redux';
import axios from 'axios';
import {
syncHistory,
routeReducer
} from 'react-router-redux'
import immutifyState from 'lib/immutifyState';
import injectAxiosAndGetMiddleware from 'lib/promiseMiddleware';
import universalRouter from 'lib/universalRouter';
axios.interceptors.request.use(function (config) {
if (config.url[0] === '/') {
config.url = '/api/1' + config.url;
}
return config;
});
const { pathname, search } = window.location;
const initialState = immutifyState(window.__INITIAL_STATE__);
const reducer = combineReducers({...reducers, routing: routeReducer});
const promiseMiddleware = injectAxiosAndGetMiddleware(axios);
const reduxRouterMiddleware = syncHistory(history);
const store = applyMiddleware(reduxRouterMiddleware, promiseMiddleware)(createStore)(reducer, initialState);
const routes = injectStoreAndGetRoutes(store);
// Required for replaying actions from devtools to work
reduxRouterMiddleware.listenForReplays(store);
const renderApp = (location, preload) => {
return universalRouter({routes, location, store, history, deferred: true, preload})
.then(({component, redirectLocation}) => {
if (redirectLocation) {
history.replace(redirectLocation)
} else {
render(component, document.getElementById('react-view'));
}
})
.catch(console.error.bind(console));
};
history.listenBefore((location, callback) => {
renderApp(location, true)
.then(callback);
});
// can't use `false` for `preload` as this would break symmetry and `fetchComponentData`...`catch` wouldn't be called.
// need to find a workaround.
renderApp(pathname + search, true);