Skip to content

Instantly share code, notes, and snippets.

@daveols
Last active July 6, 2018 03:00
Show Gist options
  • Save daveols/666eec165f5b2acd6f049d8bd44dfd5a to your computer and use it in GitHub Desktop.
Save daveols/666eec165f5b2acd6f049d8bd44dfd5a to your computer and use it in GitHub Desktop.
React Loads - local storage cache provider
const Global = typeof window !== 'undefined' ? window : global
const serialize = data => JSON.stringify(data)
const deserialize = stringValue => {
if (!stringValue) return
let value = ''
try {
value = JSON.parse(stringValue)
} catch (e) {
value = stringValue
}
return value
}
const get = key => {
const data = Global.localStorage.getItem(`bacon-app.${key}`)
return deserialize(data)
}
const set = (key, data) => {
if (!data && data === undefined) {
Global.localStorage.removeItem(`bacon-app.${key}`)
} else {
Global.localStorage.setItem(`bacon-app.${key}`, serialize(data))
}
return data
}
export default {
get,
set,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment