fix(router): skip router write on duplicate entries #4487
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes duplicate entries in the router that happen when
router.write
is called even thoughuiState
hasn't changed.Explanation
We used to call the
write
method of the router at every state change. A state change happens at each UI interaction or whenever a widget is mounted/unmounted. The latter is problematic in component-based flavors of InstantSearch (Vue InstantSearch is the only one relying on InstantSearch 4 right now) because widgets are often added/removed.The duplicate entry detection check was happening in the history router itself, which meant that whenever users opt-out of this default router and implemented their own, they would get this issue.
This fix checks that
router.write
needs to be called in the router middleware itself. Since it's one level above the history router, or custom user implementation, it will get fixed no matter the router implementations. We check that therouteState
is different from the last received to callrouter.write
. We base the check onrouteState
, and notuiState
, because users might plug their own logic instateToRoute
that returns arouteState
that differs fromuiState
; the two do not necessarily change at the same time.Related