This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.tsx
93 lines (82 loc) · 3.25 KB
/
index.tsx
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
84
85
86
87
88
89
90
91
92
93
/* eslint-disable global-require */
/* eslint-disable-next-line */
import React from "react";
import { hydrate, unmountComponentAtNode } from "react-dom";
import { browserHistory, match } from "react-router";
import { syncHistoryWithStore } from "react-router-redux";
import Fetchr from "fetchr";
import createStore from "../shared/redux/createStore";
import App from "./components/App";
const isDevToolVisible = __DEVELOPMENT__ && !__MOCK_BUILD__;
const store = configStore();
const history = syncHistoryWithStore(browserHistory, store, {
adjustUrlOnReplay: __DEVELOPMENT__,
});
const root = document.getElementById("root");
renderApp().then(() => {
if (isDevToolVisible) {
configHotLoader();
}
});
function configStore() {
const getJson = (id: string) =>
JSON.parse((document.getElementById(id) as any).getAttribute("data-json"));
const initialState = getJson("initial-state");
const clientConfig = getJson("client-config");
return createStore(initialState, {
csrfToken: clientConfig.csrfToken,
fetchr: new Fetchr(clientConfig.fetchr),
fetchrCache: clientConfig.fetchrCache,
history: browserHistory,
devTools: isDevToolVisible,
mockBuild: __MOCK_BUILD__ ? clientConfig.mockBuild : false,
});
}
function renderApp() {
const routes = getRoutes();
return new Promise(resolve => {
// 非同期のonEnter()がある画面がサーバサイドレンダリングされるとクライアント
// サイドの最初のレンダリングも非同期 (画面の一部が未完成) となってハッシュが
// 合わなくなり、サーバサイドレダリングの結果が破棄されてしまう。
// 回避策としてクライアントサイドでも最初の表示はmatch()を使用して非同期処理が
// 終わった後にレンダリングする。
// matchの引数にbrowserHistoryがベースのhistoryを渡している (renderPropsを
// 通じて <Router> に渡る) ため、最初の表示後はhistoryの変化により通常通り
// レンダリングされる。
match(
{ routes, history },
(error: Error, redirectLocation: any, renderProps: {}) => {
// redirect が必要で、 SSR モードがオフの時に renderProps が空になるため
// hydrate に失敗する事がある。
// SSR が有効の場合は redirect は server の response で行われるのでこの処理は不要。
if (__DISABLE_SSR__ && redirectLocation && !renderProps) {
window.location.replace(
`${redirectLocation.pathname}${redirectLocation.search}`,
);
return resolve();
}
if (root) {
hydrate(<App store={store} {...renderProps} />, root);
}
return resolve();
},
);
});
}
function getRoutes() {
const routes = require("../shared/routes").default;
return routes(store);
}
function configHotLoader() {
(module as any).hot.accept("../shared/routes", () => {
if (root) {
unmountComponentAtNode(root);
}
renderApp();
});
// module.hot is extension by hot-loader
(module as any).hot.accept("../shared/redux/modules/reducer", () => {
const nextReducer = require("../shared/redux/modules/reducer").default;
store.replaceReducer(nextReducer);
});
}