1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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
This commit is contained in:
Sébastien Dubois 2017-01-28 18:22:32 +02:00 committed by Tim Neutkens
parent bf467e64af
commit a3bec7666b
3 changed files with 3182 additions and 12 deletions

View file

@ -1,7 +1,9 @@
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) => {
}
})
})
if (!process.browser) {
return client
}
if (!window.APOLLO_CLIENT) {
window.APOLLO_CLIENT = client
}
return window.APOLLO_CLIENT
}
export const initClient = (headers) => {
if (!process.browser) {
return createClient(headers)
}
if (!apolloClient) {
apolloClient = createClient(headers)
}
return apolloClient
}

View file

@ -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
}

File diff suppressed because it is too large Load diff