# local-storage-fallback

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

Latest version **5.0.0** (published 2024-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install local-storage-fallback
pnpm add local-storage-fallback
yarn add local-storage-fallback
bun add local-storage-fallback
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2024-10-25 |
| First published | 2016-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 112 |
| Author | Mike Kruk |
| Maintainers | tamagokun |
| Keywords | localStorage |

## Links

- npm: https://www.npmjs.com/package/local-storage-fallback
- Repository: https://github.com/ripeworks/local-storage-fallback
- Homepage: https://github.com/ripeworks/local-storage-fallback#readme
- Issues: https://github.com/ripeworks/local-storage-fallback/issues
- npm.io page: https://npm.io/package/local-storage-fallback

## Dependencies (1)

- [cookie](https://npm.io/package/cookie.md) ^1.0.1

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2024-10-25
- 4.1.3 — 2024-10-25
- 4.1.2 — 2021-02-17
- 4.1.1 — 2018-05-25
- 4.1.0 — 2018-03-15
- 4.0.2 — 2017-10-27
- 4.0.1 — 2017-10-27
- 4.0.0 — 2017-07-27
- 3.0.0 — 2017-07-07
- 2.0.0 — 2017-06-22
- 1.4.0 — 2017-06-22
- 1.3.1 — 2017-05-10
- 1.3.0 — 2016-07-06
- 1.2.0 — 2016-05-19
- 1.1.0 — 2016-05-19
- … 1 more at https://npm.io/package/local-storage-fallback/versions

## README

# local-storage-fallback
Check and use appropriate storage adapter for browser (localStorage, sessionStorage, cookies, memory)

[![npm version](https://badge.fury.io/js/local-storage-fallback.svg)](https://badge.fury.io/js/local-storage-fallback)

## Installation

```
$ npm install local-storage-fallback
```

## Usage

```js
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

```html
<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.

---
_Source: https://npm.io/package/local-storage-fallback · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
