From 99e4870d99ca76ead12ee0ba8219b443fc8e1f94 Mon Sep 17 00:00:00 2001 From: Filip Barl Date: Mon, 9 Apr 2018 09:42:43 +0200 Subject: [PATCH] Don't update route if it hasn't changed. --- client/app/scripts/utils/router-utils.js | 10 +++++++--- client/package.json | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/client/app/scripts/utils/router-utils.js b/client/app/scripts/utils/router-utils.js index 16f29f8f7..b5950f07f 100644 --- a/client/app/scripts/utils/router-utils.js +++ b/client/app/scripts/utils/router-utils.js @@ -1,4 +1,5 @@ import page from 'page'; +import stableStringify from 'json-stable-stringify'; import { fromJS, is as isDeepEqual } from 'immutable'; import { each, omit, omitBy, isEmpty } from 'lodash'; @@ -107,9 +108,12 @@ export function getUrlState(state) { export function updateRoute(getState) { const state = getUrlState(getState()); - const stateUrl = encodeURL(JSON.stringify(state)); - const dispatch = false; const prevState = parseHashState(); + const dispatch = false; + + const stateUrl = encodeURL(stableStringify(state)); + const prevStateUrl = encodeURL(stableStringify(prevState)); + if (stateUrl === prevStateUrl) return; // back up state in storage as well storageSet(STORAGE_STATE_KEY, stateUrl); @@ -152,7 +156,7 @@ export function getRouter(dispatch, initialState) { } else { const mergedState = Object.assign(initialState, parsedState); // push storage state to URL - window.location.hash = `!/state/${JSON.stringify(mergedState)}`; + window.location.hash = `!/state/${stableStringify(mergedState)}`; dispatch(route(mergedState)); } } else { diff --git a/client/package.json b/client/package.json index f79428d5c..5cbcc728a 100644 --- a/client/package.json +++ b/client/package.json @@ -24,6 +24,7 @@ "filter-invalid-dom-props": "2.0.0", "font-awesome": "4.7.0", "immutable": "3.8.2", + "json-stable-stringify": "1.0.1", "lcp": "1.1.0", "lodash": "4.17.4", "materialize-css": "0.98.1",