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

Commit

Permalink
Makes sure the state in the store matches the state in history (#445)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dlmr authored and timdorr committed Sep 22, 2016
1 parent a304bda commit 5127e46
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function syncHistoryWithStore(history, store, {
let isTimeTraveling
let unsubscribeFromStore
let unsubscribeFromHistory
let currentLocation

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

// Init currentLocation with potential location in store
let currentLocation = getLocationInStore()
// Init initialLocation with potential location in store
initialLocation = getLocationInStore()

// If the store is replayed, update the URL in the browser to match.
if (adjustUrlOnReplay) {
const handleStoreChange = () => {
const locationInStore = getLocationInStore(true)
if (currentLocation === locationInStore) {
if (currentLocation === locationInStore || initialLocation === locationInStore) {
return
}

Expand Down
7 changes: 7 additions & 0 deletions test/_createSyncTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ export default function createTests(createHistory, name, reset = defaultReset) {
// We expect that we get a single call to history
expect(historyListen.calls.length).toBe(1)

clientStore.dispatch({
type: 'non-router'
})

// We expect that we still get only a single call to history after a non-router action is dispatched
expect(historyListen.calls.length).toBe(1)

historyUnsubscribe()
})
})
Expand Down

0 comments on commit 5127e46

Please sign in to comment.