Provides in-memory implementation of localStorage or sessionStorage when they aren't available
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
__tests__
example
src
.editorconfig
.gitignore
README.md
package-lock.json
package.json
rollup.config.js
tsconfig.json
tslint.json
yarn.lock

README.md

Safe storage

Maintainability

npm version

Test Coverage

Safe implementations for localStorage and sessionStorage, which fallbacks to in-memory implementation if default browser implementations aren't available. For example, when in private mode in Safari, storages are available, but an attempt to write to them throws an error.

Installation

npm i -S safe-storage

Usage

safe-storage has the same signature as localStorage and sessionStorage, so you can use it as you would use normal localStorage or sessionStorage. Example:

import { safeLocalStorage } from 'safe-storage';

safeLocalStorage.setItem('token', '<123456789>');

console.log(safeLocalStorage.getItem('token'));
// output: <123456789>

console.log(safeLocalStorage.length);
// output: 1 

safeLocalStorage.removeItem('token');

console.log(safeLocalStorage.getItem('token'));
// output: null

For sessionStorage:

import { safeSessionStorage } from 'safe-storage';