From 361485b445dd88eaa9b8a38b22080f4f720dae1d Mon Sep 17 00:00:00 2001 From: Emmanuel Fallancy Date: Thu, 26 Oct 2017 17:23:28 +1100 Subject: [PATCH] Add warning as prevention on drop off of UI from being displayed (#2614) * Add console warning as prevention on drop off of UI from being displayed * Remove console warning from getStateKey as it is not necessary for it is not trying to access the sessionStorage * Fix linting * Preserve scroll position in window global when sessionStorage is not available --- .../src/StateStorage.js | 31 +++++++++++++++++-- yarn.lock | 10 +++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-react-router-scroll/src/StateStorage.js b/packages/gatsby-react-router-scroll/src/StateStorage.js index a5b1929314779..caf8151d932ec 100644 --- a/packages/gatsby-react-router-scroll/src/StateStorage.js +++ b/packages/gatsby-react-router-scroll/src/StateStorage.js @@ -1,16 +1,41 @@ const STATE_KEY_PREFIX = `@@scroll|` +const GATSBY_ROUTER_SCROLL_STATE = `___GATSBY_REACT_ROUTER_SCROLL` export default class SessionStorage { read(location, key) { const stateKey = this.getStateKey(location, key) - const value = sessionStorage.getItem(stateKey) - return JSON.parse(value) + + try { + const value = sessionStorage.getItem(stateKey) + return JSON.parse(value) + } catch (e) { + console.warn(`[gatsby-react-router-scroll] Unable to access sessionStorage; sessionStorage is not available.`) + + if (window && window[GATSBY_ROUTER_SCROLL_STATE] && window[GATSBY_ROUTER_SCROLL_STATE][stateKey]) { + return window[GATSBY_ROUTER_SCROLL_STATE][stateKey] + } + + return ({}) + } } save(location, key, value) { const stateKey = this.getStateKey(location, key) const storedValue = JSON.stringify(value) - sessionStorage.setItem(stateKey, storedValue) + + try { + sessionStorage.setItem(stateKey, storedValue) + } catch (e) { + if (window && window[GATSBY_ROUTER_SCROLL_STATE]) { + window[GATSBY_ROUTER_SCROLL_STATE][stateKey] = JSON.parse(storedValue) + } else { + window[GATSBY_ROUTER_SCROLL_STATE] = {} + window[GATSBY_ROUTER_SCROLL_STATE][stateKey] = JSON.parse(storedValue) + } + + console.warn(`[gatsby-react-router-scroll] Unable to save state in sessionStorage; sessionStorage is not available.`) + } + } getStateKey(location, key) { diff --git a/yarn.lock b/yarn.lock index aa717126721f1..7a2cb037cf18b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5965,7 +5965,11 @@ is-binary-path@^1.0.0: dependencies: binary-extensions "^1.0.0" -is-buffer@^1.0.2, is-buffer@^1.1.4, is-buffer@^1.1.5, is-buffer@~1.1.1, is-buffer@~1.1.2, is-buffer@~1.1.5: +is-buffer@^1.0.2, is-buffer@^1.1.4, is-buffer@~1.1.1, is-buffer@~1.1.2, is-buffer@~1.1.5: + version "1.1.6" + resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" + +is-buffer@^1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc" @@ -7930,6 +7934,10 @@ mime-db@~1.25.0: version "1.25.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.25.0.tgz#c18dbd7c73a5dbf6f44a024dc0d165a1e7b1c392" +mime-db@~1.30.0: + version "1.30.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.30.0.tgz#74c643da2dd9d6a45399963465b26d5ca7d71f01" + mime-types@2.1.13: version "2.1.13" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88"