@@ -37,6 +37,7 @@ function locationToString(location) {
37
37
}
38
38
39
39
function syncReduxAndRouter ( history , store , selectRouterState = SELECT_STATE ) {
40
+ let lastRoute ;
40
41
const getRouterState = ( ) => selectRouterState ( store . getState ( ) ) ;
41
42
42
43
if ( ! getRouterState ( ) ) {
@@ -47,22 +48,26 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
47
48
}
48
49
49
50
const unsubscribeHistory = history . listen ( location => {
51
+ const newLocation = locationToString ( location ) ;
50
52
// Avoid dispatching an action if the store is already up-to-date,
51
53
// even if `history` wouldn't do anything if the location is the same
52
- if ( getRouterState ( ) . path !== locationToString ( location ) ) {
53
- store . dispatch ( updatePath ( locationToString ( location ) ) ) ;
54
+ if ( getRouterState ( ) . path !== newLocation ) {
55
+ lastRoute = newLocation ;
56
+ store . dispatch ( updatePath ( newLocation ) ) ;
54
57
}
55
58
} ) ;
56
59
57
60
const unsubscribeStore = store . subscribe ( ( ) => {
58
61
const routing = getRouterState ( ) ;
59
62
60
- // Don't update the router if nothing has changed. The
61
- // `noRouterUpdate` flag can be set to avoid updating altogether,
63
+ // Don't update the router if the routing state hasn't changed or the new routing path
64
+ // is already the current location.
65
+ // The `noRouterUpdate` flag can be set to avoid updating altogether,
62
66
// which is useful for things like loading snapshots or very special
63
67
// edge cases.
64
- if ( routing . path !== locationToString ( window . location ) &&
68
+ if ( lastRoute !== routing . path && routing . path !== locationToString ( window . location ) &&
65
69
! routing . noRouterUpdate ) {
70
+ lastRoute = routing . path ;
66
71
history . pushState ( null , routing . path ) ;
67
72
}
68
73
} ) ;
@@ -77,5 +82,5 @@ module.exports = {
77
82
UPDATE_PATH ,
78
83
updatePath,
79
84
syncReduxAndRouter,
80
- routeReducer : update ,
85
+ routeReducer : update
81
86
} ;
0 commit comments