pinafore/src/routes/_utils/testStorage.js
Nolan Lawson 7c04b86405
fix: use smooth scroll polyfill in Chrome for scroll-to-top (#1601)
* fix: use smooth scroll polyfill in Chrome for scroll-to-top

* rename thunk to __thunk__ for safety
2019-10-24 19:03:10 -07:00

42 lines
944 B
JavaScript

// LocalStorage and IDB may be disabled in private mode, when "blocking cookies" in Safari,
// or other cases
import { __thunk__ } from './thunk'
const testKey = '__test__'
export const testHasLocalStorage = __thunk__(() => {
try {
localStorage.setItem(testKey, testKey)
if (!localStorage.length || localStorage.getItem(testKey) !== testKey) {
return false
}
localStorage.removeItem(testKey)
} catch (e) {
return false
}
return true
})
export const testHasIndexedDB = __thunk__(async () => {
if (typeof indexedDB === 'undefined') {
return false
}
try {
const idbFailed = await new Promise(resolve => {
const db = indexedDB.open(testKey)
db.onerror = () => resolve(true)
db.onsuccess = () => {
indexedDB.deleteDatabase(testKey)
resolve(false)
}
})
if (idbFailed) {
return false
}
} catch (e) {
return false
}
return true
})