@@ -12,7 +12,6 @@ import {
1212} from 'next' ;
1313
1414export const HYDRATE = '__NEXT_REDUX_WRAPPER_HYDRATE__' ;
15- export const STOREKEY = '__NEXT_REDUX_WRAPPER_STORE__' ;
1615
1716const getIsServer = ( ) => typeof window === 'undefined' ;
1817
@@ -22,19 +21,16 @@ const getDeserializedState = <S extends Store>(initialState: any, {deserializeSt
2221const getSerializedState = < S extends Store > ( state : any , { serializeState} : Config < S > = { } ) =>
2322 serializeState ? serializeState ( state ) : state ;
2423
25- const getStoreKey = < S extends Store > ( { storeKey} : Config < S > = { } ) => storeKey || STOREKEY ;
26-
2724export declare type MakeStore < S extends Store > = ( context : Context ) => S ;
2825
2926export interface InitStoreOptions < S extends Store > {
3027 makeStore : MakeStore < S > ;
3128 context : Context ;
32- config : Config < S > ;
3329}
3430
35- const initStore = < S extends Store > ( { makeStore, context, config} : InitStoreOptions < S > ) : S => {
36- const storeKey = getStoreKey ( config ) ;
31+ let store : any ;
3732
33+ const initStore = < S extends Store > ( { makeStore, context} : InitStoreOptions < S > ) : S => {
3834 const createStore = ( ) => makeStore ( context ) ;
3935
4036 if ( getIsServer ( ) ) {
@@ -48,15 +44,16 @@ const initStore = <S extends Store>({makeStore, context, config}: InitStoreOptio
4844 if ( ! req . __nextReduxWrapperStore ) req . __nextReduxWrapperStore = createStore ( ) ;
4945 return req . __nextReduxWrapperStore ;
5046 }
47+
5148 return createStore ( ) ;
5249 }
5350
5451 // Memoize store if client
55- if ( ! ( storeKey in window ) ) {
56- ( window as any ) [ storeKey ] = createStore ( ) ;
52+ if ( ! store ) {
53+ store = createStore ( ) ;
5754 }
5855
59- return ( window as any ) [ storeKey ] ;
56+ return store ;
6057} ;
6158
6259export const createWrapper = < S extends Store > ( makeStore : MakeStore < S > , config : Config < S > = { } ) => {
@@ -67,7 +64,7 @@ export const createWrapper = <S extends Store>(makeStore: MakeStore<S>, config:
6764 callback : Callback < S , any > ;
6865 context : any ;
6966 } ) : Promise < WrapperProps > => {
70- const store = initStore ( { context, makeStore, config } ) ;
67+ const store = initStore ( { context, makeStore} ) ;
7168
7269 if ( config . debug ) console . log ( `1. getProps created store with state` , store . getState ( ) ) ;
7370
@@ -141,7 +138,7 @@ export const createWrapper = <S extends Store>(makeStore: MakeStore<S>, config:
141138 const initialStateFromGSPorGSSR = props ?. pageProps ?. initialState ;
142139
143140 if ( ! this . store ) {
144- this . store = initStore ( { makeStore, config , context} ) ;
141+ this . store = initStore ( { makeStore, context} ) ;
145142
146143 if ( config . debug )
147144 console . log ( '4. WrappedApp created new store with' , displayName , {
@@ -232,7 +229,6 @@ export type Context = NextPageContext | AppContext | GetStaticPropsContext | Get
232229export interface Config < S extends Store > {
233230 serializeState ?: ( state : ReturnType < S [ 'getState' ] > ) => any ;
234231 deserializeState ?: ( state : any ) => ReturnType < S [ 'getState' ] > ;
235- storeKey ?: string ;
236232 debug ?: boolean ;
237233}
238234
0 commit comments