提交 a3bec766 编写于 作者: S Sébastien Dubois 提交者: Tim Neutkens

with-apollo: Don't store Redux store and Apollo client in global namespace (#909)

* Add yarn lockfile

* Avoid storing Redux store and Apollo client in global namespace + don't create Apollo client when already existing in browser
上级 bf467e64
import ApolloClient, { createNetworkInterface } from 'apollo-client'
export const initClient = (headers) => {
const client = new ApolloClient({
let apolloClient = null
function createClient (headers) {
return new ApolloClient({
ssrMode: !process.browser,
headers,
dataIdFromObject: result => result.id || null,
......@@ -12,11 +14,14 @@ export const initClient = (headers) => {
}
})
})
}
export const initClient = (headers) => {
if (!process.browser) {
return client
return createClient(headers)
}
if (!window.APOLLO_CLIENT) {
window.APOLLO_CLIENT = client
if (!apolloClient) {
apolloClient = createClient(headers)
}
return window.APOLLO_CLIENT
return apolloClient
}
......@@ -2,15 +2,17 @@ import { createStore } from 'redux'
import getReducer from './reducer'
import createMiddleware from './middleware'
let reduxStore = null
export const initStore = (client, initialState) => {
let store
if (!process.browser || !window.REDUX_STORE) {
if (!process.browser || !reduxStore) {
const middleware = createMiddleware(client.middleware())
store = createStore(getReducer(client), initialState, middleware)
if (!process.browser) {
return store
}
window.REDUX_STORE = store
reduxStore = store
}
return window.REDUX_STORE
return reduxStore
}
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册