From 766d7d5fe44b9b0dacb012f5c0aafd0d1a66c9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1r=20=C3=96rlygsson?= Date: Tue, 20 Nov 2018 14:53:28 +0000 Subject: [PATCH] Run initializeStore() only once per route on server (#5644) On the server `props.initialMobxState` received by the `constructor` is a fully functioning mobx store that was instantiated by `getInitialProps()` only a few ticks earlier. Thus, creating a new instance is unneccessary. In the browser, however, `props.initialMobxState` is a hydrated plain object and thus the store needs to be initialized. --- examples/with-mobx/pages/_app.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/with-mobx/pages/_app.js b/examples/with-mobx/pages/_app.js index d8c59b13..32921ffd 100644 --- a/examples/with-mobx/pages/_app.js +++ b/examples/with-mobx/pages/_app.js @@ -21,7 +21,10 @@ class MyMobxApp extends App { constructor(props) { super(props) - this.mobxStore = initializeStore(props.initialMobxState) + const isServer = typeof window === 'undefined' + this.mobxStore = isServer ? + props.initialMobxState: + initializeStore(props.initialMobxState) } render() {