-
-
Notifications
You must be signed in to change notification settings - Fork 266
Fix hot reloading issues by removing the store from window #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,6 @@ import { | |
| } from 'next'; | ||
|
|
||
| export const HYDRATE = '__NEXT_REDUX_WRAPPER_HYDRATE__'; | ||
| export const STOREKEY = '__NEXT_REDUX_WRAPPER_STORE__'; | ||
|
|
||
| const getIsServer = () => typeof window === 'undefined'; | ||
|
|
||
|
|
@@ -22,19 +21,16 @@ const getDeserializedState = <S extends Store>(initialState: any, {deserializeSt | |
| const getSerializedState = <S extends Store>(state: any, {serializeState}: Config<S> = {}) => | ||
| serializeState ? serializeState(state) : state; | ||
|
|
||
| const getStoreKey = <S extends Store>({storeKey}: Config<S> = {}) => storeKey || STOREKEY; | ||
|
|
||
| export declare type MakeStore<S extends Store> = (context: Context) => S; | ||
|
|
||
| export interface InitStoreOptions<S extends Store> { | ||
| makeStore: MakeStore<S>; | ||
| context: Context; | ||
| config: Config<S>; | ||
| } | ||
|
|
||
| const initStore = <S extends Store>({makeStore, context, config}: InitStoreOptions<S>): S => { | ||
| const storeKey = getStoreKey(config); | ||
| let store: any; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this may not even be necessary, but it's probably a good idea to keep a global reference around in case |
||
|
|
||
| const initStore = <S extends Store>({makeStore, context}: InitStoreOptions<S>): S => { | ||
| const createStore = () => makeStore(context); | ||
|
|
||
| if (getIsServer()) { | ||
|
|
@@ -48,15 +44,16 @@ const initStore = <S extends Store>({makeStore, context, config}: InitStoreOptio | |
| if (!req.__nextReduxWrapperStore) req.__nextReduxWrapperStore = createStore(); | ||
| return req.__nextReduxWrapperStore; | ||
| } | ||
|
|
||
| return createStore(); | ||
| } | ||
|
|
||
| // Memoize store if client | ||
| if (!(storeKey in window)) { | ||
| (window as any)[storeKey] = createStore(); | ||
| if (!store) { | ||
| store = createStore(); | ||
| } | ||
|
|
||
| return (window as any)[storeKey]; | ||
| return store; | ||
| }; | ||
|
|
||
| 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: | |
| callback: Callback<S, any>; | ||
| context: any; | ||
| }): Promise<WrapperProps> => { | ||
| const store = initStore({context, makeStore, config}); | ||
| const store = initStore({context, makeStore}); | ||
|
|
||
| if (config.debug) console.log(`1. getProps created store with state`, store.getState()); | ||
|
|
||
|
|
@@ -141,7 +138,7 @@ export const createWrapper = <S extends Store>(makeStore: MakeStore<S>, config: | |
| const initialStateFromGSPorGSSR = props?.pageProps?.initialState; | ||
|
|
||
| if (!this.store) { | ||
| this.store = initStore({makeStore, config, context}); | ||
| this.store = initStore({makeStore, context}); | ||
|
|
||
| if (config.debug) | ||
| console.log('4. WrappedApp created new store with', displayName, { | ||
|
|
@@ -232,7 +229,6 @@ export type Context = NextPageContext | AppContext | GetStaticPropsContext | Get | |
| export interface Config<S extends Store> { | ||
| serializeState?: (state: ReturnType<S['getState']>) => any; | ||
| deserializeState?: (state: any) => ReturnType<S['getState']>; | ||
| storeKey?: string; | ||
| debug?: boolean; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,27 +3,26 @@ | |
| **/ | ||
|
|
||
| import * as React from 'react'; | ||
| import {useDispatch} from 'react-redux'; | ||
| import {create, act} from 'react-test-renderer'; | ||
| import {DummyComponent, wrapper, child, makeStore} from './testlib'; | ||
| import {createWrapper} from '../src'; | ||
| import {Store} from 'redux'; | ||
|
|
||
| const w: {testStoreKey?: Store} = window as any; | ||
| let store: Store; | ||
|
|
||
| const defaultState = {reduxStatus: 'init'}; | ||
| const modifiedState = {...defaultState, modified: true}; | ||
|
|
||
| describe('client integration', () => { | ||
| afterEach(() => { | ||
| delete w.testStoreKey; | ||
| }); | ||
|
|
||
| describe('existing store is taken from window', () => { | ||
| beforeEach(() => { | ||
| w.testStoreKey = makeStore(); | ||
| store = makeStore(); | ||
| }); | ||
|
|
||
| test('withRedux', async () => { | ||
| const WrappedPage: any = wrapper.withRedux(DummyComponent); | ||
| expect(child(<WrappedPage initialState={w.testStoreKey?.getState()} />)).toEqual( | ||
| expect(child(<WrappedPage initialState={store.getState()} />)).toEqual( | ||
| '{"props":{},"state":{"reduxStatus":"init"}}', | ||
| ); | ||
| }); | ||
|
|
@@ -38,11 +37,32 @@ describe('client integration', () => { | |
| }); | ||
| }); | ||
|
|
||
| test('store is available in window when created', async () => { | ||
| const wrapper = createWrapper(makeStore, {storeKey: 'testStoreKey'}); | ||
| const Page = () => null; | ||
| Page.getInitialProps = wrapper.getInitialPageProps(store => () => null); | ||
| test('store is available when calling getInitialProps client-side and references the existing store on client', async () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking this is sufficient to test the store is the same on client-side |
||
| const wrapper = createWrapper(makeStore); | ||
| let renderer: any = null; | ||
|
|
||
| const Page: React.ComponentType<any> & {getInitialProps: any} = () => { | ||
| const dispatch = useDispatch(); | ||
|
|
||
| React.useEffect(() => { | ||
| // modifies the state, | ||
| dispatch({type: 'MODIFY_STATE'}); | ||
| }, []); | ||
|
|
||
| return null; | ||
| }; | ||
| Page.getInitialProps = wrapper.getInitialPageProps(store => () => | ||
| // when invoked below, verify that state modification is retained in getInitialProps | ||
| expect(store.getState()).toEqual(modifiedState), | ||
| ); | ||
|
|
||
| const Wrapped: any = wrapper.withRedux(Page); | ||
|
|
||
| act(() => { | ||
| renderer = create(<Wrapped />); | ||
| }); | ||
|
|
||
| // expected when invoked above | ||
| await wrapper.withRedux(Page)?.getInitialProps({} as any); | ||
| expect(w.testStoreKey?.getState()).toEqual(defaultState); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is no longer necessary