fix(client): Hot reload breaking on undefined defaultState

This commit is contained in:
Berkeley Martinez
2018-01-05 13:47:53 -08:00
parent 9d5d5c3be4
commit f8f55dd846

View File

@ -1,4 +1,5 @@
import './es6-shims'; import './es6-shims';
import _ from 'lodash';
import Rx from 'rx'; import Rx from 'rx';
import debug from 'debug'; import debug from 'debug';
import { render } from 'redux-epic'; import { render } from 'redux-epic';
@ -22,33 +23,41 @@ import {
const isDev = Rx.config.longStackSupport = debug.enabled('fcc:*'); const isDev = Rx.config.longStackSupport = debug.enabled('fcc:*');
const log = debug('fcc:client'); const log = debug('fcc:client');
const hotReloadTimeout = 2000; const hotReloadTimeout = 2000;
const { csrf: { token: csrfToken } = {} } = window.__fcc__; const {
devToolsExtension = _.noop,
location,
history: _history,
document,
ga,
__fcc__: {
flash = [],
data: ssrState = {},
csrf: {
token: csrfToken
} = {}
}
} = window;
const epicOptions = {
isDev,
window,
document,
location,
history: _history
};
const DOMContainer = document.getElementById('fcc'); const DOMContainer = document.getElementById('fcc');
const defaultState = isColdStored() ? const defaultState = isColdStored() ?
getColdStorage() : getColdStorage() :
window.__fcc__.data; ssrState;
const primaryLang = getLangFromPath(window.location.pathname); const primaryLang = getLangFromPath(location.pathname);
defaultState.app.csrfToken = csrfToken; defaultState.app.csrfToken = csrfToken;
defaultState.toasts = flashToToast(window.__fcc__.flash); defaultState.toasts = flashToToast(flash);
// make empty object so hot reload works
window.__fcc__ = {};
const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } }; const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } };
const history = useLangRoutes(createHistory, primaryLang)(); const history = useLangRoutes(createHistory, primaryLang)();
sendPageAnalytics(history, window.ga); sendPageAnalytics(history, ga);
const devTools = window.devToolsExtension ? window.devToolsExtension() : f => f;
const epicOptions = {
isDev,
window,
document: window.document,
location: window.location,
history: window.history
};
createApp({ createApp({
history, history,
@ -56,7 +65,7 @@ createApp({
defaultState, defaultState,
epics, epics,
epicOptions, epicOptions,
enhancers: [ devTools ] enhancers: [ devToolsExtension() ]
}) })
.doOnNext(({ store }) => { .doOnNext(({ store }) => {
if (module.hot && typeof module.hot.accept === 'function') { if (module.hot && typeof module.hot.accept === 'function') {
@ -65,7 +74,7 @@ createApp({
log('saving state and refreshing.'); log('saving state and refreshing.');
log('ignore react ssr warning.'); log('ignore react ssr warning.');
saveToColdStorage(store.getState()); saveToColdStorage(store.getState());
setTimeout(() => window.location.reload(), hotReloadTimeout); setTimeout(() => location.reload(), hotReloadTimeout);
}); });
} }
}) })