Skip to content

Updates/improvements to original local-storage-fallback library. This library checks and uses appropriate storage adapters for browser (localStorage, sessionStorage, cookies, memory), with a unified API for them, and better handling of errors and requests.

License

Notifications You must be signed in to change notification settings

rw3iss/better-local-storage-fallback

 
 

Repository files navigation

better-local-storage-fallback

Forked/originally from @ripeworks/local-storage-fallback.

npm version

Installation

$ npm install rw3iss/better-local-storage-fallback

Usage

import storage from 'better-local-storage-fallback' // if you use es6 or typescript
// use object destructuring when using require()
// const {storage} = require('better-local-storage-fallback')

// Use storage directly
storage.setItem('foo', 'bar');
storage.getItem('foo'); // bar

// Or shim window.localStorage
if (!('localStorage' in window)) {
  window.localStorage = storage;
}

Browser Bundle

(TODO)

<script src="https://unpkg.com/better-local-storage-fallback/lib/dist.min.js"></script>
<script>
  window.localStorageFallback.setItem('foo', 'bar')
</script>

Purpose

With browser settings like "Private Browsing" it has become a problem to rely on a working window.localStorage, even in newer browsers. Even though it may exist, it will throw exceptions when trying to use setItem or getItem. This module will run appropriate checks to see what browser storage mechanism might be available, and then expose it. It uses the same API as localStorage so it should work as a drop-in replacement in most cases.

Gotchas

  • CookieStorage has storage limits. Be careful here.
  • MemoryStorage will not persist between page loads. This is more or less a stop-gap to prevent page crashes, but may be sufficient for websites that don't do full page loads.

About

Updates/improvements to original local-storage-fallback library. This library checks and uses appropriate storage adapters for browser (localStorage, sessionStorage, cookies, memory), with a unified API for them, and better handling of errors and requests.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%