This is a
playground to test code. It runs a full
Node.js environment and already has all of
npm’s 400,000 packages pre-installed, including
safer-web-storage with all
npm packages installed. Try it out:
This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.
Browsers that support
sessionStorage will have a property on the window object named respectively. However, for various reasons, just asserting that property exists may throw exceptions. If it does exist, that is still no guarantee that localStorage is actually available, as various browsers offer settings that disable storages. So a browser may support localStorage, but not make it available to the scripts on the page. One example of that is Safari, which in Private Browsing mode gives us an empty localStorage object with a quota of zero, effectively making it unusable.
npm install safer-web-storage
import * as SafeStorage from 'safer-web-storage' const safeLocalStorage = SafeStorage.createSafeLocalStorage() const safeSessionStorage = SafeStorage.createSafeSessionStorage() safeLocalStorage.getItem('apples') safeSessionStorage.seItem('pineapples', 20)
window.sessionStorage or its methods are not accessible, it swaps to in-memory storage.
This wrapper supports all methods and properties of Web Storage API:
You can pass the following properties to
|String||Looks like you've disabled <StorageType>. Enable it to avoid this warning.||Error message printed to the browser console when storage is not available|
To test the library in a browser run:
npm install npm start
npm ci npm test npm run build npm version [patch|minor|major] npm publish