Skip to content

Commit

Permalink
Add warning as prevention on drop off of UI from being displayed (gat…
Browse files Browse the repository at this point in the history
…sbyjs#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
  • Loading branch information
efallancy authored and KyleAMathews committed Oct 26, 2017
1 parent 11c3aee commit 361485b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
31 changes: 28 additions & 3 deletions packages/gatsby-react-router-scroll/src/StateStorage.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"

[email protected]:
version "2.1.13"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88"
Expand Down

0 comments on commit 361485b

Please sign in to comment.