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:
parent
bf467e64af
commit
a3bec7666b
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
3163
examples/with-apollo/yarn.lock
Normal file
3163
examples/with-apollo/yarn.lock
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue