Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Commit 7b7cc26

Browse files
dlmrtimdorr
authored andcommitted
Init currentLocation to initial location from the store (#403)
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
1 parent 5f2b0b6 commit 7b7cc26

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/sync.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export default function syncHistoryWithStore(history, store, {
3131
}
3232

3333
let initialLocation
34-
let currentLocation
3534
let isTimeTraveling
3635
let unsubscribeFromStore
3736
let unsubscribeFromHistory
@@ -43,6 +42,9 @@ export default function syncHistoryWithStore(history, store, {
4342
(useInitialIfEmpty ? initialLocation : undefined)
4443
}
4544

45+
// Init currentLocation with potential location in store
46+
let currentLocation = getLocationInStore()
47+
4648
// If the store is replayed, update the URL in the browser to match.
4749
if (adjustUrlOnReplay) {
4850
const handleStoreChange = () => {

test/_createSyncTest.js

+27
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,33 @@ export default function createTests(createHistory, name, reset = defaultReset) {
163163
})
164164
})
165165

166+
describe('Server', () => {
167+
it('handles inital load correctly', () => {
168+
// Server
169+
const { store: serverStore } = createSyncedHistoryAndStore(createHistory('/'))
170+
expect(serverStore).toContainLocation({
171+
pathname: '/',
172+
action: 'POP'
173+
})
174+
175+
// Client
176+
let clientStore = createStore(combineReducers({
177+
routing: routerReducer
178+
}), serverStore.getState())
179+
let clientHistory = useRouterHistory(createHistory)()
180+
181+
const historyListen = expect.createSpy()
182+
const historyUnsubscribe = clientHistory.listen(historyListen)
183+
184+
syncHistoryWithStore(clientHistory, clientStore)
185+
186+
// We expect that we get a single call to history
187+
expect(historyListen.calls.length).toBe(1)
188+
189+
historyUnsubscribe()
190+
})
191+
})
192+
166193
describe('Redux DevTools', () => {
167194
let originalHistory, history, store, devToolsStore
168195

0 commit comments

Comments
 (0)