From 374dc2510d00e46fbeba309a48a0ff3e40392fbb Mon Sep 17 00:00:00 2001 From: ochicf Date: Tue, 28 Aug 2018 18:24:30 +0200 Subject: [PATCH] defer creation of apolloClient until it is first used --- .../vulcan-lib/lib/client/render_context.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/vulcan-lib/lib/client/render_context.js b/packages/vulcan-lib/lib/client/render_context.js index 3723a55d0..65edb6fab 100644 --- a/packages/vulcan-lib/lib/client/render_context.js +++ b/packages/vulcan-lib/lib/client/render_context.js @@ -14,15 +14,12 @@ export const initContext = () => { // init const history = browserHistory; const loginToken = global.localStorage['Meteor.loginToken']; - const apolloClient = createApolloClient(); - addReducer({ apollo: apolloClient.reducer() }); - addMiddleware(apolloClient.middleware()); + let apolloClient; // init context context = { history, loginToken, - apolloClient, addAction, // context.addAction same as addAction getActions, // context.getActions same as getActions addReducer, // context.addReducer same as addReducer @@ -31,6 +28,19 @@ export const initContext = () => { getMiddlewares, // context.getMiddlewares same as getMiddlewares }; + // defer creation of apolloClient until it is first used + Object.defineProperty(context, 'apolloClient', { + enumerable: true, + get: () => { + if (!apolloClient) { + apolloClient = createApolloClient(); + addReducer({ apollo: apolloClient.reducer() }); + addMiddleware(apolloClient.middleware()); + } + return apolloClient; + }, + }); + // init store context.store = configureStore(context.getReducers, {}, (store) => { let chain, newDispatch;