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

Makes sure the state in the store matches the state in history when using SSR #445

Merged
merged 1 commit into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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