has-localstorage

hasLocalStorage

function
hoodie.store.hasLocalStorage()

hasLocalStorage()

returns true or false depending on whether localStorage is supported or not.
Beware that some browsers like Safari do not support localStorage in private mode.

inspired by this cappuccino commit
https://github.com/cappuccino/cappuccino/commit/063b05d9643c35b303568a28809e4eb3224f71ec

function hasLocalStorage() {
  try {

    // we've to put this in here. I've seen Firefox throwing `Security error: 1000`
    // when cookies have been disabled
    if (!global.localStorage) {
      return false;
    }

    // Just because localStorage exists does not mean it works. In particular it might be disabled
    // as it is when Safari's private browsing mode is active.
    global.localStorage.setItem('Storage-Test', '1');

    // that should not happen ...
    if (global.localStorage.getItem('Storage-Test') !== '1') {
      return false;
    }

    // okay, let's clean up if we got here.
    global.localStorage.removeItem('Storage-Test');
  } catch (_error) {

    // in case of an error, like Safari's Private Mode, return false
    return false;
  }

  // we're good.
  return true;
};

module.exports = hasLocalStorage;