-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.js
83 lines (71 loc) · 2.55 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
/*global document, window */
import React from 'react';
import ReactDOM from 'react-dom';
import debug from 'debug';
import app from './app';
import { IntlProvider } from 'react-intl';
import { loadLocale } from './configs/locales';
import es6Promise from 'es6-promise';
es6Promise.polyfill();
const debugClient = debug('slidewiki-platform');
const dehydratedState = window.App; // Sent from the server
window.React = ReactDOM; // For chrome dev tool support
// expose debug object to browser, so that it can be enabled/disabled from browser:
// https://github.com/visionmedia/debug#browser-support
window.fluxibleDebug = debug;
//removing hash, for redirects
function removeHash() {
let scrollV, scrollH, loc = window.location;
if ('replaceState' in history)
history.replaceState('', document.title, loc.pathname + loc.search);
else {
// Prevent scrolling by storing the page's current scroll offset
scrollV = document.body.scrollTop;
scrollH = document.body.scrollLeft;
loc.hash = '';
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scrollV;
document.body.scrollLeft = scrollH;
}
}
function renderApp(locale, messages) {
debugClient('rehydrating app');
// pass in the dehydrated server state from server.js
app.rehydrate(dehydratedState, (err, context) => {
if (err) {
throw err;
}
window.context = context;
removeHash();
const mountNode = document.getElementById('app');
const Root = app.getComponent();
ReactDOM.hydrate(
<IntlProvider locale={locale} messages={messages}>
<Root context={context.getComponentContext()} />
</IntlProvider>,
mountNode,
() => {
debug('Root component has been mounted');
}
);
});
}
// Load the Intl polyfill and required locale data
const locale = document.documentElement.getAttribute('lang');
loadLocale(locale).then((messages) => {
renderApp(locale, messages);
if (module.hot) {
module.hot.accept('./app', () => {
const RootContainer = require('./app');
const Root = RootContainer.getComponent();
ReactDOM.render(
<IntlProvider locale={locale} messages={messages}>
<Root context={window.context.getComponentContext()} />
</IntlProvider>,
document.getElementById('app')
);
});
}
}).catch((err) => {
console.error(err);
});