Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct key behavior #3483

Merged
merged 3 commits into from
Jan 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/gatsby-react-router-scroll/src/StateStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class SessionStorage {
const stateKey = this.getStateKey(location, key)

try {
const value = sessionStorage.getItem(stateKey)
const value = window.sessionStorage.getItem(stateKey)
return JSON.parse(value)
} catch (e) {
console.warn(
Expand All @@ -30,7 +30,7 @@ export default class SessionStorage {
const storedValue = JSON.stringify(value)

try {
sessionStorage.setItem(stateKey, storedValue)
window.sessionStorage.setItem(stateKey, storedValue)
} catch (e) {
if (window && window[GATSBY_ROUTER_SCROLL_STATE]) {
window[GATSBY_ROUTER_SCROLL_STATE][stateKey] = JSON.parse(storedValue)
Expand All @@ -46,8 +46,9 @@ export default class SessionStorage {
}

getStateKey(location, key) {
const locationKey = location.key
const stateKeyBase = `${STATE_KEY_PREFIX}${locationKey}`
const locationPath = location.pathname
const strippedLocationPath = locationPath.endsWith(`/`) ? locationPath.slice(0, -1) : locationPath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endsWith isn't on IE https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith so just do slice(-1) or whatever it is exactly

const stateKeyBase = `${STATE_KEY_PREFIX}${strippedLocationPath}`
return key === null || typeof key === `undefined`
? stateKeyBase
: `${stateKeyBase}|${key}`
Expand Down