Socket
Socket
Sign inDemoInstall

local-storage-fallback

Package Overview
Dependencies
1
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    local-storage-fallback

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


Version published
Weekly downloads
47K
increased by14.13%
Maintainers
1
Install size
34.7 kB
Created
Weekly downloads
 

Readme

Source

local-storage-fallback

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

npm version

Installation

$ npm install local-storage-fallback

Usage

import storage from 'local-storage-fallback' // if you use es6 or typescript
// use object destructuring when using require()
// const {storage} = require('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

<script src="https://unpkg.com/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.

Keywords

FAQs

Last updated on 17 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc