-
Notifications
You must be signed in to change notification settings - Fork 167
/
client.js
180 lines (157 loc) · 5.25 KB
/
client.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* THIS IS THE ENTRY POINT FOR THE CLIENT, JUST LIKE server.js IS THE ENTRY POINT FOR THE SERVER.
*/
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router';
import { renderRoutes } from 'react-router-config';
import { trigger } from 'redial';
import { createBrowserHistory } from 'history';
import Loadable from 'react-loadable';
import { AppContainer as HotEnabler } from 'react-hot-loader';
import { getStoredState } from 'redux-persist';
import localForage from 'localforage';
import { socket, createApp } from 'app';
import createStore from 'redux/create';
import apiClient from 'helpers/apiClient';
import routes from 'routes';
import isOnline from 'utils/isOnline';
import asyncMatchRoutes from 'utils/asyncMatchRoutes';
import { RouterTrigger, Provider } from 'components';
import NProgress from 'nprogress';
const persistConfig = {
key: 'root',
storage: localForage,
stateReconciler(inboundState, originalState) {
// Ignore state from cookies, only use preloadedState from window object
return originalState;
},
whitelist: ['auth', 'info', 'chat']
};
const dest = document.getElementById('content');
const app = createApp();
const client = apiClient();
const providers = {
app,
client
};
function initSocket() {
socket.on('news', data => {
console.log(data);
socket.emit('my other event', { my: 'data from client' });
});
socket.on('msg', data => {
console.log(data);
});
return socket;
}
initSocket();
(async () => {
const preloadedState = await getStoredState(persistConfig);
const online = window.__data ? true : await isOnline();
if (online) {
socket.open();
await app.authenticate().catch(() => null);
}
const history = createBrowserHistory();
const store = createStore({
history,
data: {
...preloadedState,
...window.__data,
online
},
helpers: providers,
persistConfig
});
const triggerHooks = async (_routes, pathname) => {
NProgress.start();
const { components, match, params } = await asyncMatchRoutes(_routes, pathname);
const triggerLocals = {
...providers,
store,
match,
params,
history,
location: history.location
};
await trigger('inject', components, triggerLocals);
// Don't fetch data for initial route, server has already done the work:
if (window.__PRELOADED__) {
// Delete initial data so that subsequent data fetches can occur:
delete window.__PRELOADED__;
} else {
// Fetch mandatory data dependencies for 2nd route change onwards:
await trigger('fetch', components, triggerLocals);
}
await trigger('defer', components, triggerLocals);
NProgress.done();
};
const hydrate = _routes => {
const element = (
<HotEnabler>
<Provider store={store} {...providers}>
<Router history={history}>
<RouterTrigger trigger={pathname => triggerHooks(_routes, pathname)}>{renderRoutes(_routes)}</RouterTrigger>
</Router>
</Provider>
</HotEnabler>
);
if (dest.hasChildNodes()) {
ReactDOM.hydrate(element, dest);
} else {
ReactDOM.render(element, dest);
}
};
await Loadable.preloadReady();
hydrate(routes);
// Hot reload
if (module.hot) {
module.hot.accept('./routes', () => {
const nextRoutes = require('./routes');
hydrate(nextRoutes.__esModule ? nextRoutes.default : nextRoutes);
});
}
// Server-side rendering check
if (process.env.NODE_ENV !== 'production') {
window.React = React; // enable debugger
}
// Service worker
if (!__DEVELOPMENT__ && 'serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.register('/dist/service-worker.js', { scope: '/' });
registration.onupdatefound = () => {
// The updatefound event implies that reg.installing is set; see
// https://w3c.github.io/ServiceWorker/#service-worker-registration-updatefound-event
const installingWorker = registration.installing;
installingWorker.onstatechange = () => {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and the fresh content will
// have been added to the cache.
// It's the perfect time to display a "New content is available; please refresh."
// message in the page's interface.
console.log('New or updated content is available.');
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
console.log('Content is now available offline!');
}
break;
case 'redundant':
console.error('The installing service worker became redundant.');
break;
default:
}
};
};
} catch (error) {
console.log('Error registering service worker: ', error);
}
await navigator.serviceWorker.ready;
console.log('Service Worker Ready');
}
})();