Gathering detailed insights and metrics for localforage
Gathering detailed insights and metrics for localforage
Gathering detailed insights and metrics for localforage
Gathering detailed insights and metrics for localforage
💾 Offline storage, improved. Wraps IndexedDB, WebSQL, or localStorage using a simple but powerful API.
npm install localforage
Avoid dropInstance uncaught errors
Published on 18 Aug 2021
Improved TypeScript definition
Published on 31 Jul 2020
Added ESM support
Published on 20 Jul 2020
Improvement to key() retrieval
Published on 05 Jun 2020
Add npmignore
Published on 26 Oct 2018
Revert module support in package.json (for now)
Published on 30 Mar 2018
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
24,958 Stars
1,126 Commits
1,272 Forks
309 Watching
10 Branches
103 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
JavaScript (91.98%)
HTML (4.87%)
TypeScript (3.15%)
Cumulative downloads
Total Downloads
Last day
-8.1%
786,684
Compared to previous day
Last week
3.3%
4,379,494
Compared to previous week
Last month
8.1%
18,202,423
Compared to previous month
Last year
43.9%
184,317,032
Compared to previous year
1
38
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.
To use localForage, just drop a single JavaScript file into your page:
1<script src="localforage/dist/localforage.js"></script> 2<script>localforage.getItem('something', myCallback);</script>
Try the live example.
Download the latest localForage from GitHub, or install with npm:
1npm install localforage
Lost? Need help? Try the localForage API documentation. localForage API文档也有ä¸æ–‡ç‰ˆã€‚
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.
Because localForage uses async storage, it has an async API. It's otherwise exactly the same as the localStorage API.
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:
1localforage.setItem('key', 'value', function (err) {
2 // if err is non-null, we got an error
3 localforage.getItem('key', function (err, value) {
4 // if err is non-null, we got an error. otherwise, value is the value
5 });
6});
And the Promise form:
1localforage.setItem('key', 'value').then(function () { 2 return localforage.getItem('key'); 3}).then(function (value) { 4 // we got our value 5}).catch(function (err) { 6 // we got an error 7});
Or, use async
/await
:
1try { 2 const value = await localforage.getItem('somekey'); 3 // This code runs once the value has been loaded 4 // from the offline store. 5 console.log(value); 6} catch (err) { 7 // This code runs if there were any errors. 8 console.log(err); 9}
For more examples, please visit the API docs.
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 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.
You can set database information with the config()
method.
Available options are driver
, name
, storeName
, version
, size
, and
description
.
Example:
1localforage.config({ 2 driver : localforage.WEBSQL, // Force WebSQL; same as using setDriver() 3 name : 'myApp', 4 version : 1.0, 5 size : 4980736, // Size of database, in bytes. WebSQL-only for now. 6 storeName : 'keyvaluepairs', // Should be alphanumeric, with underscores. 7 description : 'some description' 8});
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()
.
You can create multiple instances of localForage that point to different stores
using createInstance
. All the configuration options used by
config
are supported.
1var store = localforage.createInstance({ 2 name: "nameHere" 3}); 4 5var otherStore = localforage.createInstance({ 6 name: "otherName" 7}); 8 9// Setting the key on one of these doesn't affect the other. 10store.setItem("key", "value"); 11otherStore.setItem("key", "value2");
You can use localForage with RequireJS:
1define(['localforage'], function(localforage) { 2 // As a callback: 3 localforage.setItem('mykey', 'myvalue', console.log); 4 5 // With a Promise: 6 localforage.setItem('mykey', 'myvalue').then(console.log); 7});
If you have the allowSyntheticDefaultImports
compiler option set to true
in your tsconfig.json (supported in TypeScript v1.8+), you should use:
1import localForage from "localforage";
Otherwise you should use one of the following:
1import * as localForage from "localforage"; 2// or, in case that the typescript version that you are using 3// doesn't support ES6 style imports for UMD modules like localForage 4import localForage = require("localforage");
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.
You can create your own driver if you want; see the
defineDriver
API docs.
There is a list of custom drivers on the wiki.
You'll need node/npm and bower.
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# Install bower globally if you don't have it: 2npm install -g bower 3 4# Replace USERNAME with your GitHub username: 5git clone git@github.com:USERNAME/localForage.git 6cd localForage 7npm install 8bower install
Omitting the bower dependencies will cause the tests to fail!
You need PhantomJS installed to run local tests. Run npm test
(or,
directly: grunt test
). Your code must also pass the
linter.
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 PhantomJS).
When you submit a pull request, tests will be run against all browsers that localForage supports on Travis CI using Sauce Labs.
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:
This program is free software; it is distributed under an Apache License.
Copyright (c) 2013-2016 Mozilla (Contributors).
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 10/19 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More