Skip to content

Commit

Permalink
with-apollo: Don't store Redux store and Apollo client in global name…
Browse files Browse the repository at this point in the history
…space (#909)

* Add yarn lockfile

* Avoid storing Redux store and Apollo client in global namespace + don't create Apollo client when already existing in browser
  • Loading branch information
sedubois authored and timneutkens committed Jan 28, 2017
1 parent bf467e6 commit a3bec76
Show file tree
Hide file tree
Showing 3 changed files with 3,179 additions and 9 deletions.
17 changes: 11 additions & 6 deletions examples/with-apollo/lib/initClient.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}
8 changes: 5 additions & 3 deletions examples/with-apollo/lib/initStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit a3bec76

Please sign in to comment.