LocalForage

Offline storage, improved. Wraps IndexedDB, WebSQL, or localStorage using a...

README

localForage NPM version npm jsDelivr Hits minzipped size


localForage is a fast and simple storage library for JavaScript. localForage
improves the offline experience of your web app by using asynchronous storage
(IndexedDB or WebSQL) with a simple, localStorage-like API.

localForage uses localStorage in browsers with no IndexedDB or
WebSQL support. See [the wiki for detailed compatibility info][supported browsers].

To use localForage, just drop a single JavaScript file into your page:

  1. ``` html
  2. <script src="localforage/dist/localforage.js"></script>
  3. <script>localforage.getItem('something', myCallback);</script>
  4. ```
Try the live example.

Download the latest localForage from GitHub, or install with

  1. ``` sh
  2. npm install localforage
  3. ```

[supported browsers]: https://github.com/localForage/localForage/wiki/Supported-Browsers-Platforms

Support


Lost? Need help? Try the

If you're having trouble using the library, running the tests, or want to contribute to localForage, please look through the existing issues for your problem first before creating a new one. If you still need help, feel free to file an issue.

How to use localForage


Callbacks vs Promises


Because localForage uses async storage, it has an async API.
It's otherwise exactly the same as the

localForage has a dual API that allows you to either use Node-style callbacks
or Promises. If you are unsure which one is right for you, it's recommended to use Promises.

Here's an example of the Node-style callback form:

  1. ``` js
  2. localforage.setItem('key', 'value', function (err) {
  3.   // if err is non-null, we got an error
  4.   localforage.getItem('key', function (err, value) {
  5.     // if err is non-null, we got an error. otherwise, value is the value
  6.   });
  7. });
  8. ```

And the Promise form:

  1. ``` js
  2. localforage.setItem('key', 'value').then(function () {
  3.   return localforage.getItem('key');
  4. }).then(function (value) {
  5.   // we got our value
  6. }).catch(function (err) {
  7.   // we got an error
  8. });
  9. ```

Or, use async/await:

  1. ``` js
  2. try {
  3.     const value = await localforage.getItem('somekey');
  4.     // This code runs once the value has been loaded
  5.     // from the offline store.
  6.     console.log(value);
  7. } catch (err) {
  8.     // This code runs if there were any errors.
  9.     console.log(err);
  10. }
  11. ```

For more examples, please visit the API docs.

Storing Blobs, TypedArrays, and other JS objects


You can store any type in localForage; you aren't limited to strings like in
localStorage. Even if localStorage is your storage backend, localForage
automatically does JSON.parse() and JSON.stringify() when getting/setting
values.

localForage supports storing all native JS objects that can be serialized to
JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the
[API docs][api] for a full list of types supported by localForage.

All types are supported in every storage backend, though storage limits in
localStorage make storing many large Blobs impossible.

[api]: https://localforage.github.io/localForage/#data-api-setitem

Configuration


You can set database information with the config() method.
Available options are driver, name, storeName, version, size, and
description.

Example:
  1. ``` js
  2. localforage.config({
  3.     driver      : localforage.WEBSQL, // Force WebSQL; same as using setDriver()
  4.     name        : 'myApp',
  5.     version     : 1.0,
  6.     size        : 4980736, // Size of database, in bytes. WebSQL-only for now.
  7.     storeName   : 'keyvaluepairs', // Should be alphanumeric, with underscores.
  8.     description : 'some description'
  9. });
  10. ```

Note: you must call config() _before_ you interact with your data. This
means calling config() before using getItem(), setItem(), removeItem(),
clear(), key(), keys() or length().

Multiple instances


You can create multiple instances of localForage that point to different stores
using createInstance. All the configuration options used by
[config](#configuration) are supported.

  1. ``` javascript
  2. var store = localforage.createInstance({
  3.   name: "nameHere"
  4. });

  5. var otherStore = localforage.createInstance({
  6.   name: "otherName"
  7. });

  8. // Setting the key on one of these doesn't affect the other.
  9. store.setItem("key", "value");
  10. otherStore.setItem("key", "value2");
  11. ```

RequireJS


You can use localForage with RequireJS:

  1. ``` js
  2. define(['localforage'], function(localforage) {
  3.     // As a callback:
  4.     localforage.setItem('mykey', 'myvalue', console.log);

  5.     // With a Promise:
  6.     localforage.setItem('mykey', 'myvalue').then(console.log);
  7. });
  8. ```

TypeScript


If you have the [allowSyntheticDefaultImports compiler option](https://www.typescriptlang.org/docs/handbook/compiler-options.html) set to true in your tsconfig.json (supported in TypeScript v1.8+), you should use:

  1. ``` js
  2. import localForage from "localforage";
  3. ```

Otherwise you should use one of the following:

  1. ``` js
  2. import * as localForage from "localforage";
  3. // or, in case that the typescript version that you are using
  4. // doesn't support ES6 style imports for UMD modules like localForage
  5. import localForage = require("localforage");
  6. ```

Framework Support


If you use a framework listed, there's a localForage storage driver for the
models in your framework so you can store data offline with localForage. We
have drivers for the following frameworks:


If you have a driver you'd like listed, please
open an issue to have it
added to this list.

Custom Drivers


You can create your own driver if you want; see the
[defineDriver](https://localforage.github.io/localForage/#driver-api-definedriver) API docs.

There is a [list of custom drivers on the wiki][custom drivers].

[custom drivers]: https://github.com/localForage/localForage/wiki/Custom-Drivers

Working on localForage


You'll need node/npm and

To work on localForage, you should start by
forking it and installing its
dependencies. Replace USERNAME with your GitHub username and run the
following:

  1. ``` sh
  2. # Install bower globally if you don't have it:
  3. npm install -g bower

  4. # Replace USERNAME with your GitHub username:
  5. git clone git@github.com:USERNAME/localForage.git
  6. cd localForage
  7. npm install
  8. bower install
  9. ```

Omitting the bower dependencies will cause the tests to fail!

Running Tests


You need PhantomJS installed to run local tests. Run npm test (or,
directly: grunt test). Your code must also pass the

localForage is designed to run in the browser, so the tests explicitly require
a browser environment. Local tests are run on a headless WebKit (using

When you submit a pull request, tests will be run against all browsers that
localForage supports on Travis CI using Sauce Labs.

Library Size

As of version 1.7.3 the payload added to your app is rather small. Served using gzip compression, localForage will add less than 10k to your total bundle size:

minified
`~29kB`
gzipped
`~8.8kB`
brotli'd
`~7.8kB`

License


This program is free software; it is distributed under an


Copyright (c) 2013-2016 Mozilla