diff --git a/src/components/controllers/repl/index.js b/src/components/controllers/repl/index.js index 6f8df70e9..a2a754d86 100644 --- a/src/components/controllers/repl/index.js +++ b/src/components/controllers/repl/index.js @@ -3,6 +3,7 @@ import { debounce } from 'decko'; import codeExample from './code-example.txt'; import todoExample from './todo-example.txt'; import style from './style'; +import { localStorageGet, localStorageSet } from '../../../lib/localstorage'; const EXAMPLES = [ { @@ -18,7 +19,7 @@ const EXAMPLES = [ export default class Repl extends Component { state = { loading: 'Initializing...', - code: localStorage.getItem('preact-www-repl-code') || codeExample + code: localStorageGet('preact-www-repl-code') || codeExample }; constructor(props, context) { @@ -87,7 +88,7 @@ export default class Repl extends Component { componentDidUpdate = debounce(500, () => { let { code } = this.state; if (code===codeExample) code = ''; - localStorage.setItem('preact-www-repl-code', code || ''); + localStorageSet('preact-www-repl-code', code || ''); }) componentWillReceiveProps({ code }) { diff --git a/src/components/github-stars.js b/src/components/github-stars.js index 6f8ba0c05..731f42269 100644 --- a/src/components/github-stars.js +++ b/src/components/github-stars.js @@ -1,5 +1,6 @@ import { h, Component } from 'preact'; import { memoize } from 'decko'; +import { localStorageGet, localStorageSet } from '../lib/localstorage'; const githubStars = memoize( repo => fetch('//api.github.com/repos/'+repo) .then( r => r.json() ) @@ -11,12 +12,12 @@ if (typeof window!=='undefined') window.githubStars = githubStars; export default class GithubStars extends Component { state = { - stars: localStorage._stars || '' + stars: localStorageGet('_stars') || '' }; setStars = stars => { if (stars && stars!=this.state.stars) { - localStorage._stars = stars; + localStorageSet('_stars', stars); this.setState({ stars }); } }; diff --git a/src/lib/localstorage.js b/src/lib/localstorage.js new file mode 100644 index 000000000..e3a0c9602 --- /dev/null +++ b/src/lib/localstorage.js @@ -0,0 +1,13 @@ +export const localStorageGet = (key) => { + try { + return localStorage.getItem(key); + } catch (e) { + return null; + } +}; + +export const localStorageSet = (key, value) => { + try { + localStorage.setItem(key, value); + } catch (e) {} +}; diff --git a/src/store.js b/src/store.js index 80f647285..53ce8af59 100644 --- a/src/store.js +++ b/src/store.js @@ -1,6 +1,7 @@ import createStore from './lib/store'; import getDefaultLanguage from './lib/default-language'; import config from './config'; +import { localStorageSet, localStorageGet } from './lib/localstorage'; const SAVE = ['lang']; @@ -22,13 +23,13 @@ export default () => { function saveState(state) { let saved = {}; for (let i=SAVE.length; i--; ) saved[SAVE[i]] = state[SAVE[i]]; - localStorage.state = JSON.stringify(saved); + localStorageSet('state', JSON.stringify(saved)); } function getSavedState() { let state; try { - state = JSON.parse(localStorage.state); + state = JSON.parse(localStorageGet('state')); } catch (e) {} return state || {}; }