fallbackstorage

A fallback chain of localStorage/sesisonStorage/memoryStorage

Usage no npm install needed!

<script type="module">
  import fallbackstorage from 'https://cdn.skypack.dev/fallbackstorage';
</script>

README

Fallback Storage Build Status

NPM

A fallback chain for localStorage/sessionStorage/memoryStorage.

Usage

import {getSafeStorage} from 'fallbackstorage'

getSafeStorage check for availability and returns window.localStorage, or window.sessionStorage if they are available. Otherwise, it returns the manually implemented MemoryStorage instance.

The MemoryStorage implementation follows the specification defined at whatwg.

Why this package

In safari's private mode, or when the user has disabled local storage, the window.localStorage object is available (i.e., not null) but does not allow using storage.setItem. Therefore, the availability check is not straightforward as it seems to be.