forked from pushtell/react-ab-test
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathstore.jsx
More file actions
46 lines (42 loc) · 1.14 KB
/
store.jsx
File metadata and controls
46 lines (42 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Cookies from 'js-cookie';
// https://github.com/Modernizr/Modernizr/blob/d4c7b6082709e32fb0589ba38aa96581d44ce395/feature-detects/storage/cookies.js#L16-L35
const cookiesEnabled = (() => {
try {
document.cookie = 'cookietest=1';
const ret = document.cookie.indexOf('cookietest=') !== -1;
document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
return ret;
} catch (e) {
return false;
}
})();
// https://github.com/Modernizr/Modernizr/blob/d4c7b6082709e32fb0589ba38aa96581d44ce395/feature-detects/storage/localstorage.js#L17-L46
const localStorageEnabled = (() => {
const mod = 'modernizr';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch (e) {
return false;
}
})();
const store = {
getItem(key) {
if (localStorageEnabled) {
return localStorage.getItem(key);
}
if (cookiesEnabled) {
return Cookies.get(key);
}
},
setItem(key, value) {
if (localStorageEnabled) {
return localStorage.setItem(key, value);
}
if (cookiesEnabled) {
return Cookies.set(key, value);
}
},
};
export default store;