From 46df49486e3e26d0dcc64498a29713fc4172bef8 Mon Sep 17 00:00:00 2001 From: Gustaf Dalemar Date: Thu, 2 Jun 2016 20:34:54 +0200 Subject: [PATCH] Init currentLocation to initial location from the store This solves the problem when the server has initialised the state and the user already has things in their history. Without this change a superfluous action will be pushed with the same URL that the user is already looking at resulting in that we would need to use the back button twice. Resolves #313 --- src/sync.js | 4 +++- test/_createSyncTest.js | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/sync.js b/src/sync.js index 361aa61..ee68f79 100644 --- a/src/sync.js +++ b/src/sync.js @@ -31,7 +31,6 @@ export default function syncHistoryWithStore(history, store, { } let initialLocation - let currentLocation let isTimeTraveling let unsubscribeFromStore let unsubscribeFromHistory @@ -43,6 +42,9 @@ export default function syncHistoryWithStore(history, store, { (useInitialIfEmpty ? initialLocation : undefined) } + // Init currentLocation with potential location in store + let currentLocation = getLocationInStore() + // If the store is replayed, update the URL in the browser to match. if (adjustUrlOnReplay) { const handleStoreChange = () => { diff --git a/test/_createSyncTest.js b/test/_createSyncTest.js index 2aec90c..34aa7b3 100644 --- a/test/_createSyncTest.js +++ b/test/_createSyncTest.js @@ -163,6 +163,33 @@ export default function createTests(createHistory, name, reset = defaultReset) { }) }) + describe('Server', () => { + it('handles inital load correctly', () => { + // Server + const { store: serverStore } = createSyncedHistoryAndStore(createHistory('/')) + expect(serverStore).toContainLocation({ + pathname: '/', + action: 'POP' + }) + + // Client + let clientStore = createStore(combineReducers({ + routing: routerReducer + }), serverStore.getState()) + let clientHistory = useRouterHistory(createHistory)() + + const historyListen = expect.createSpy() + const historyUnsubscribe = clientHistory.listen(historyListen) + + syncHistoryWithStore(clientHistory, clientStore) + + // We expect that we get a single call to history + expect(historyListen.calls.length).toBe(1) + + historyUnsubscribe() + }) + }) + describe('Redux DevTools', () => { let originalHistory, history, store, devToolsStore