From 66875a108e6a23d704a117b0ef686db644832589 Mon Sep 17 00:00:00 2001 From: Manuel Bieh Date: Fri, 18 May 2018 20:20:03 +0200 Subject: [PATCH] clean up the mess --- src/client/index.js | 32 ++++++++++++++++++++------------ src/server/index.js | 2 ++ src/shared/store/index.js | 28 ---------------------------- src/shared/utils/isClient.js | 7 ------- 4 files changed, 22 insertions(+), 47 deletions(-) delete mode 100644 src/shared/utils/isClient.js diff --git a/src/client/index.js b/src/client/index.js index f24c0e1..b99cef3 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -1,22 +1,23 @@ import React from 'react'; -// import createHistory from 'history/createBrowserHistory'; +import createHistory from 'history/createBrowserHistory'; import { hydrate } from 'react-dom'; import { Provider } from 'react-redux'; -import { ConnectedRouter as Router } from 'react-router-redux'; +import { ConnectedRouter as Router, routerMiddleware } from 'react-router-redux'; import App from '../shared/App'; import IntlProvider from '../shared/i18n/IntlProvider'; -// import { configureStore } from '../shared/store'; -import { store, history } from '../shared/store'; +import { configureStore } from '../shared/store'; -// const history = createHistory(); -// const store = configureStore({ -// initialState: window.__PRELOADED_STATE__, -// middleware: [routerMiddleware(history)], -// }); +const browserHistory = window.browserHistory || createHistory(); +const store = + window.store || + configureStore({ + initialState: window.__PRELOADED_STATE__, + middleware: [routerMiddleware(history)], + }); hydrate( - + @@ -25,6 +26,13 @@ hydrate( document.getElementById('app') ); -if (module.hot) { - module.hot.accept(); +if (process.env.NODE_ENV === 'development') { + if (module.hot) { + module.hot.accept(); + } + + if (!window.store || !window.browserHistory) { + window.browserHistory = browserHistory; + window.store = store; + } } diff --git a/src/server/index.js b/src/server/index.js index b5387df..5821953 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -13,6 +13,8 @@ require('dotenv').config(); const app = express(); +// Use nginx or Apache to serve static assets in production or remove the if() around the following +// lines to use the express.static middleware to serve assets for production (not recommended!) if (process.env.NODE_ENV === 'development') { app.use(paths.publicPath, express.static(paths.clientBuild)); app.use('/favicon.ico', (req, res) => { diff --git a/src/shared/store/index.js b/src/shared/store/index.js index b0b52d0..65bb98e 100644 --- a/src/shared/store/index.js +++ b/src/shared/store/index.js @@ -1,13 +1,8 @@ import thunk from 'redux-thunk'; import { createStore, applyMiddleware, compose } from 'redux'; import rootReducer from './rootReducer'; -import isClient from 'shared/utils/isClient'; export const configureStore = ({ initialState, middleware = [] } = {}) => { - if (isClient() && window.store) { - return window.store; - } - const devtools = typeof window !== 'undefined' && typeof window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ === 'function' && @@ -21,10 +16,6 @@ export const configureStore = ({ initialState, middleware = [] } = {}) => { composeEnhancers(applyMiddleware(...[thunk].concat(...middleware))) ); - if (isClient()) { - window.store = store; - } - if (process.env.NODE_ENV !== 'production') { if (module.hot) { module.hot.accept('./rootReducer', () => @@ -37,22 +28,3 @@ export const configureStore = ({ initialState, middleware = [] } = {}) => { }; export default configureStore; - -export let store; -export let history; - -if (isClient()) { - const createHistory = require('history/createBrowserHistory').default; - const { routerMiddleware } = require('react-router-redux'); - - if (window.browserHistory) { - history = window.browserHistory; - } else { - history = createHistory(); - } - - store = configureStore({ - initialState: window.__PRELOADED_STATE__, - middleware: [routerMiddleware(history)], - }); -} diff --git a/src/shared/utils/isClient.js b/src/shared/utils/isClient.js deleted file mode 100644 index 7437473..0000000 --- a/src/shared/utils/isClient.js +++ /dev/null @@ -1,7 +0,0 @@ -export default () => { - return ( - typeof window !== 'undefined' && - typeof window.document !== 'undefined' && - typeof window.document.createElement !== 'undefined' - ); -};