Skip to content

Commit 4c97e10

Browse files
committed
Fix hot reloading issues by removing the store from window
1 parent 193e2a3 commit 4c97e10

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ The `createWrapper` function accepts `makeStore` as its first argument. The `mak
214214
215215
`createWrapper` also optionally accepts a config object as a second parameter:
216216
217-
- `storeKey` (optional, string) : the key used on `window` to persist the store on the client
218217
- `debug` (optional, boolean) : enable debug logging
219218
- `serializeState` and `deserializeState`: custom functions for serializing and deserializing the redux state, see
220219
[Custom serialization and deserialization](#custom-serialization-and-deserialization).

packages/wrapper/src/index.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from 'next';
1313

1414
export const HYDRATE = '__NEXT_REDUX_WRAPPER_HYDRATE__';
15-
export const STOREKEY = '__NEXT_REDUX_WRAPPER_STORE__';
1615

1716
const getIsServer = () => typeof window === 'undefined';
1817

@@ -22,19 +21,16 @@ const getDeserializedState = <S extends Store>(initialState: any, {deserializeSt
2221
const 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-
2724
export declare type MakeStore<S extends Store> = (context: Context) => S;
2825

2926
export 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

6259
export 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
232229
export 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

packages/wrapper/tests/client.spec.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ import {DummyComponent, wrapper, child, makeStore} from './testlib';
77
import {createWrapper} from '../src';
88
import {Store} from 'redux';
99

10-
const w: {testStoreKey?: Store} = window as any;
10+
let store: Store;
1111

1212
const defaultState = {reduxStatus: 'init'};
1313

1414
describe('client integration', () => {
15-
afterEach(() => {
16-
delete w.testStoreKey;
17-
});
18-
1915
describe('existing store is taken from window', () => {
2016
beforeEach(() => {
21-
w.testStoreKey = makeStore();
17+
store = makeStore();
2218
});
2319

2420
test('withRedux', async () => {
2521
const WrappedPage: any = wrapper.withRedux(DummyComponent);
26-
expect(child(<WrappedPage initialState={w.testStoreKey?.getState()} />)).toEqual(
22+
expect(child(<WrappedPage initialState={store.getState()} />)).toEqual(
2723
'{"props":{},"state":{"reduxStatus":"init"}}',
2824
);
2925
});
@@ -38,11 +34,11 @@ describe('client integration', () => {
3834
});
3935
});
4036

41-
test('store is available in window when created', async () => {
42-
const wrapper = createWrapper(makeStore, {storeKey: 'testStoreKey'});
37+
test('store is available when calling getInitialProps client-side', async () => {
38+
const wrapper = createWrapper(makeStore);
4339
const Page = () => null;
4440
Page.getInitialProps = wrapper.getInitialPageProps(store => () => null);
4541
await wrapper.withRedux(Page)?.getInitialProps({} as any);
46-
expect(w.testStoreKey?.getState()).toEqual(defaultState);
42+
expect(store.getState()).toEqual(defaultState);
4743
});
4844
});

packages/wrapper/tests/testlib.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const reducer = (state: State = {reduxStatus: 'init'}, action: AnyAction)
2424

2525
export const makeStore = () => createStore(reducer, undefined, applyMiddleware(promiseMiddleware));
2626

27-
export const wrapper = createWrapper(makeStore, {storeKey: 'testStoreKey'});
27+
export const wrapper = createWrapper(makeStore);
2828

2929
export const DummyComponent = (props: any) => {
3030
const state = useSelector((state: State) => state);

0 commit comments

Comments
 (0)