Skip to content

comicrelief/session-storage-fallback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Session Storage Fallback

semantic-release

Check and use appropriate storage adapter for browser (sessionStorage, cookies, memory)

This repository was cloned from github.com/ripeworks/local-storage-fallback due to a need to fallback from session storage rather than local storage.

Installation

$ npm install session-storage-fallback

Usage

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

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

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

Purpose

With browser settings like "Private Browsing" it has become a problem to rely on a working window.sessionStorage, 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 sessionStorage 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

NPM module to fallback from session storage to cookies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published