2017-05-10 22:23:11 +00:00
|
|
|
import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
|
|
|
|
import reducers from './reducers'
|
|
|
|
|
|
|
|
let reduxStore = null
|
|
|
|
|
|
|
|
// Get the Redux DevTools extension and fallback to a no-op function
|
|
|
|
let devtools = f => f
|
|
|
|
if (process.browser && window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
|
|
devtools = window.__REDUX_DEVTOOLS_EXTENSION__()
|
|
|
|
}
|
|
|
|
|
|
|
|
function create (apollo, initialState = {}) {
|
|
|
|
return createStore(
|
|
|
|
combineReducers({ // Setup reducers
|
2017-12-27 18:57:57 +00:00
|
|
|
...reducers
|
2017-05-10 22:23:11 +00:00
|
|
|
}),
|
|
|
|
initialState, // Hydrate the store with server-side data
|
|
|
|
compose(
|
2017-12-27 18:57:57 +00:00
|
|
|
// Add additional middleware here
|
2017-05-10 22:23:11 +00:00
|
|
|
devtools
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
export default function initRedux (initialState) {
|
2017-05-10 22:23:11 +00:00
|
|
|
// Make sure to create a new store for every server-side request so that data
|
|
|
|
// isn't shared between connections (which would be bad)
|
|
|
|
if (!process.browser) {
|
2017-12-27 18:57:57 +00:00
|
|
|
return create(initialState)
|
2017-05-10 22:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reuse store on the client-side
|
|
|
|
if (!reduxStore) {
|
2017-12-27 18:57:57 +00:00
|
|
|
reduxStore = create(initialState)
|
2017-05-10 22:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return reduxStore
|
|
|
|
}
|