LucasBassetti / react-simple-chatbot

:speech_balloon: Easy way to create conversation chats
https://lucasbassetti.com.br/react-simple-chatbot/
MIT License
1.72k stars 596 forks source link

SetupBot not working in safari private window #296

Open elixir14 opened 3 years ago

elixir14 commented 3 years ago

The setupbot doesn't work in the safari private window. It throws storage not available error. Is there any way to work for setupbot storin data in session storage and local storage?

jeznag commented 3 years ago

Adding to this: this library does not work in chrome incognito windows or Brave Browser with shields down. It would ideally check whether localStorage is available, perhaps using code like the below. I don't think local storage/session storage should be required for the library to work. I assume it's a convenience feature for offline caching or similar?


function isStorageAvailable() {
  let storage;
  try {
    storage = window['localStorage'];
    const x = '__storage_test__';
    storage.setItem(x, x);
    storage.removeItem(x);
    return true;
  }
  catch(e) {
    return e instanceof DOMException && (
      // everything except Firefox
      e.code === 22 ||
      // Firefox
      e.code === 1014 ||
      // test name field too, because code might not be present
      // everything except Firefox
      e.name === 'QuotaExceededError' ||
      // Firefox
      e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
      // acknowledge QuotaExceededError only if there's something already stored
      storage && storage.length !== 0;
  }
}

export function safelyRetrieveLocalStorageItem(storageKey) {
  if (!isStorageAvailable()) {
    return;
  }
  try {
    return localStorage.getItem(storageKey);
  } catch (e) {
  }
}

export function safelySetLocalStorageItem(storageKey, value) {
  if (!isStorageAvailable()) {
    return;
  }
  try {
    return localStorage.setItem(storageKey, value);
  } catch (e) {
  }
}
elixir14 commented 3 years ago

Even if the cache option is false by default, it uses local storage to save the bot data.