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

Commit 5127e46

Browse files
dlmrtimdorr
authored andcommitted
Makes sure the state in the store matches the state in history (#445)
This solves some problems where they would be out of sync on the first render when using server side rendering. The location would be the same for both of them but the key would be different resulting in some strange behaviour. The application will now always dispatch a LOCATION_CHANGE on initial render. This was already done when not using server side rendering, meaning that the behaviour is similar between using server side rendering and not.
1 parent a304bda commit 5127e46

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/sync.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default function syncHistoryWithStore(history, store, {
3434
let isTimeTraveling
3535
let unsubscribeFromStore
3636
let unsubscribeFromHistory
37+
let currentLocation
3738

3839
// What does the store say about current location?
3940
const getLocationInStore = (useInitialIfEmpty) => {
@@ -42,14 +43,14 @@ export default function syncHistoryWithStore(history, store, {
4243
(useInitialIfEmpty ? initialLocation : undefined)
4344
}
4445

45-
// Init currentLocation with potential location in store
46-
let currentLocation = getLocationInStore()
46+
// Init initialLocation with potential location in store
47+
initialLocation = getLocationInStore()
4748

4849
// If the store is replayed, update the URL in the browser to match.
4950
if (adjustUrlOnReplay) {
5051
const handleStoreChange = () => {
5152
const locationInStore = getLocationInStore(true)
52-
if (currentLocation === locationInStore) {
53+
if (currentLocation === locationInStore || initialLocation === locationInStore) {
5354
return
5455
}
5556

test/_createSyncTest.js

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ export default function createTests(createHistory, name, reset = defaultReset) {
186186
// We expect that we get a single call to history
187187
expect(historyListen.calls.length).toBe(1)
188188

189+
clientStore.dispatch({
190+
type: 'non-router'
191+
})
192+
193+
// We expect that we still get only a single call to history after a non-router action is dispatched
194+
expect(historyListen.calls.length).toBe(1)
195+
189196
historyUnsubscribe()
190197
})
191198
})

0 commit comments

Comments
 (0)